3 * @brief Generic Time handling
10 * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
12 * This software is distributed under commercial and open source licenses.
13 * You may use the GPL open source license described below or you may acquire
14 * a commercial license from Mbedthis Software. You agree to be fully bound
15 * by the terms of either license. Consult the LICENSE.TXT distributed with
16 * this software for full details.
18 * This software is open source; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2 of the License, or (at your
21 * option) any later version. See the GNU General Public License for more
22 * details at: http://www.mbedthis.com/downloads/gplLicense.html
24 * This program is distributed WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 * This GPL license does NOT permit incorporating this software into
28 * proprietary programs. If you are unable to comply with the GPL, you must
29 * acquire a commercial license to use this software. Commercial licenses
30 * for this software and support services are available from Mbedthis
31 * Software at http://www.mbedthis.com
36 /********************************* Includes ***********************************/
44 /******************************************************************************/
46 * Return the number of milliseconds until the given timeout has expired.
49 int mprGetTimeRemaining(MprCtx ctx, MprTime mark, uint timeout)
54 mprGetTime(ctx, &now);
55 diff = ((now.sec - mark.sec) * 1000) + (now.msec - mark.msec);
59 * Detect time going backwards
64 return (int) (timeout - diff);
67 /******************************************************************************/
69 * Return the number of milliseconds until the given timeout has expired.
72 int mprGetElapsedTime(MprCtx ctx, MprTime mark)
76 mprGetTime(ctx, &now);
77 return ((now.sec - mark.sec) * 1000) + (now.msec - mark.msec);
80 /******************************************************************************/
82 void mprAddElapsedToTime(MprTime *time, uint elapsed)
84 time->sec += elapsed / 1000;
85 time->msec += elapsed % 1000;
86 if (time->msec > 1000) {
92 /******************************************************************************/
94 int mprCompareTime(MprTime *t1, MprTime *t2)
96 if (t1->sec < t2->sec) {
98 } else if (t1->sec == t2->sec) {
99 if (t1->msec < t2->msec) {
101 } else if (t1->msec == t2->msec) {
108 /******************************************************************************/
110 uint mprSubtractTime(MprTime *t1, MprTime *t2)
112 return ((t1->sec - t2->sec) * 1000) + (t1->msec - t2->msec);
115 /******************************************************************************/
118 * Thread-safe RFC822 dates (Eg: "Fri, 07 Jan 2003 12:12:21 GMT")
121 int mprRfcTime(MprCtx ctx, char *buf, int bufsize, const struct tm *timep)
123 char months[12][4] = {
124 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
129 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
136 return MPR_ERR_WONT_FIT;
138 dayp = &days[timep->tm_wday][0];
145 *buf++ = timep->tm_mday / 10 + '0';
146 *buf++ = timep->tm_mday % 10 + '0';
149 monthp = &months[timep->tm_mon][0];
155 year = 1900 + timep->tm_year;
156 /* This routine isn't y10k ready. */
157 *buf++ = year / 1000 + '0';
158 *buf++ = year % 1000 / 100 + '0';
159 *buf++ = year % 100 / 10 + '0';
160 *buf++ = year % 10 + '0';
163 *buf++ = timep->tm_hour / 10 + '0';
164 *buf++ = timep->tm_hour % 10 + '0';
166 *buf++ = timep->tm_min / 10 + '0';
167 *buf++ = timep->tm_min % 10 + '0';
169 *buf++ = timep->tm_sec / 10 + '0';
170 *buf++ = timep->tm_sec % 10 + '0';
181 /******************************************************************************/
193 * vim600: sw=4 ts=4 fdm=marker