Add header.
[amitay/samba.git] / source4 / lib / appweb / ejs-2.0 / mpr / mprGenTime.c
1 /**
2  *      @file mprGenTime.c 
3  *      @brief Generic Time handling
4  *      @overview 
5  */
6
7 /*
8  *      @copy   default
9  *      
10  *      Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
11  *      
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.
17  *      
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
23  *      
24  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
25  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
26  *      
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 
32  *      
33  *      @end
34  */
35
36 /********************************* Includes ***********************************/
37
38 #include        "mpr.h"
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 /******************************************************************************/
45 /*
46  *      Return the number of milliseconds until the given timeout has expired.
47  */
48
49 int mprGetTimeRemaining(MprCtx ctx, MprTime mark, uint timeout)
50 {
51         MprTime         now;
52         uint            diff;
53
54         mprGetTime(ctx, &now);
55         diff = ((now.sec - mark.sec) * 1000) + (now.msec - mark.msec);
56
57         if (diff < 0) {
58                 /*
59                  *      Detect time going backwards
60                  */
61                 mprAssert(diff >= 0);
62                 diff = 0;
63         }       
64         return (int) (timeout - diff);
65 }
66  
67 /******************************************************************************/
68 /*
69  *      Return the number of milliseconds until the given timeout has expired.
70  */
71
72 int mprGetElapsedTime(MprCtx ctx, MprTime mark)
73 {
74         MprTime         now;
75
76         mprGetTime(ctx, &now);
77         return ((now.sec - mark.sec) * 1000) + (now.msec - mark.msec);
78 }
79  
80 /******************************************************************************/
81
82 void mprAddElapsedToTime(MprTime *time, uint elapsed)
83 {
84         time->sec += elapsed / 1000;
85         time->msec += elapsed % 1000;
86         if (time->msec > 1000) {
87                 time->msec -= 1000;
88                 time->sec++;
89         }
90 }
91
92 /******************************************************************************/
93
94 int mprCompareTime(MprTime *t1, MprTime *t2)
95 {
96         if (t1->sec < t2->sec) {
97                 return -1;
98         } else if (t1->sec == t2->sec) {
99                 if (t1->msec < t2->msec) {
100                         return -1;
101                 } else if (t1->msec == t2->msec) {
102                         return 0;
103                 }
104         }
105         return 1;
106 }
107
108 /******************************************************************************/
109
110 uint mprSubtractTime(MprTime *t1, MprTime *t2)
111 {
112         return ((t1->sec - t2->sec) * 1000) + (t1->msec - t2->msec);
113 }
114
115 /******************************************************************************/
116 #if !BREW
117 /*
118  *      Thread-safe RFC822 dates (Eg: "Fri, 07 Jan 2003 12:12:21 GMT")
119  */
120
121 int mprRfcTime(MprCtx ctx, char *buf, int bufsize, const struct tm *timep)
122 {
123         char months[12][4] = {
124                 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 
125                 "Oct", "Nov", "Dec"
126         };
127
128         char days[7][4] = {
129                 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
130         };
131
132     char        *dayp, *monthp;
133     int         year;
134
135         if (bufsize < 30) {
136                 return MPR_ERR_WONT_FIT;
137         }
138     dayp = &days[timep->tm_wday][0];
139     *buf++ = *dayp++;
140     *buf++ = *dayp++;
141     *buf++ = *dayp++;
142     *buf++ = ',';
143     *buf++ = ' ';
144
145     *buf++ = timep->tm_mday / 10 + '0';
146     *buf++ = timep->tm_mday % 10 + '0';
147     *buf++ = ' ';
148
149     monthp = &months[timep->tm_mon][0];
150     *buf++ = *monthp++;
151     *buf++ = *monthp++;
152     *buf++ = *monthp++;
153     *buf++ = ' ';
154
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';
161     *buf++ = ' ';
162
163     *buf++ = timep->tm_hour / 10 + '0';
164     *buf++ = timep->tm_hour % 10 + '0';
165     *buf++ = ':';
166     *buf++ = timep->tm_min / 10 + '0';
167     *buf++ = timep->tm_min % 10 + '0';
168     *buf++ = ':';
169     *buf++ = timep->tm_sec / 10 + '0';
170     *buf++ = timep->tm_sec % 10 + '0';
171     *buf++ = ' ';
172
173     *buf++ = 'G';
174     *buf++ = 'M';
175     *buf++ = 'T';
176     *buf++ = 0;
177     return 0;
178 }
179
180 #endif
181 /******************************************************************************/
182
183 #ifdef __cplusplus
184 }
185 #endif
186
187 /*
188  * Local variables:
189  * tab-width: 4
190  * c-basic-offset: 4
191  * End:
192  * vim:tw=78
193  * vim600: sw=4 ts=4 fdm=marker
194  * vim<600: sw=4 ts=4
195  */