]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/btime.c
0b4b6df2215cd1723f9eabaf4a1a3bfd1ef6c549
[bacula/bacula] / bacula / src / lib / btime.c
1 /*
2  * Bacula time and date routines -- John Walker
3  *
4  *   Version $Id$
5  */
6 /*
7    Copyright (C) 2000-2003 Kern Sibbald and John Walker
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2 of
12    the License, or (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public
20    License along with this program; if not, write to the Free
21    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22    MA 02111-1307, USA.
23
24  */
25
26
27 /* Concerning times. There are a number of differnt time standards
28  * in Bacula (fdate_t, ftime_t, time_t (Unix standard), btime_t, and
29  *  utime_t).  fdate_t and ftime_t are deprecated and should no longer
30  *  be used, and in general, Unix time time_t should no longer be used,
31  *  it is being phased out. 
32  *     
33  *  Epoch is the base of Unix time in seconds (time_t, ...) 
34  *     and is 1 Jan 1970 at 0:0
35  *
36  *  The major two times that should be left are:
37  *     btime_t  (64 bit integer in microseconds base Epoch)
38  *     utime_t  (64 bit integer in seconds base Epoch)
39  */
40
41 #include "bacula.h"
42 #include <math.h>
43
44 /* Formatted time */
45 void bstrftime(char *dt, int maxlen, utime_t tim)
46 {
47    time_t ttime = tim;
48    struct tm tm;
49    
50    /* ***FIXME**** the format and localtime_r() should be user configurable */
51    localtime_r(&ttime, &tm);
52    strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
53 }
54
55 /* Unix time to standard time string yyyy-mm-dd hh:mm:ss */
56 char *bstrutime(char *dt, int maxlen, utime_t tim)
57 {
58    time_t ttime = tim;
59    struct tm tm;
60    localtime_r(&ttime, &tm);
61    strftime(dt, maxlen, "%Y-%m-%d %H:%M:%S", &tm);
62    return dt;
63 }
64
65 /* Convert standard time string yyyy-mm-dd hh:mm:ss to Unix time */
66 utime_t str_to_utime(char *str) 
67 {
68    struct tm tm;
69    time_t ttime;
70
71    if (sscanf(str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
72                                         &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
73       return 0;
74    }
75    if (tm.tm_mon > 0) {
76       tm.tm_mon--;
77    } else { 
78       return 0;
79    }
80    if (tm.tm_year >= 1900) {
81       tm.tm_year -= 1900;
82    } else {
83       return 0;
84    }
85    ttime = mktime(&tm);
86    if (ttime == -1) {       
87       ttime = 0;
88    }
89    return (utime_t)ttime;
90 }
91
92 /* Deprecated. Do not use. */
93 void get_current_time(struct date_time *dt)
94 {
95    struct tm tm;
96    time_t now;
97
98    now = time(NULL);
99    gmtime_r(&now, &tm);
100    Dmsg6(200, "m=%d d=%d y=%d h=%d m=%d s=%d\n", tm.tm_mon+1, tm.tm_mday, tm.tm_year+1900,
101       tm.tm_hour, tm.tm_min, tm.tm_sec);
102    tm_encode(dt, &tm);
103 #ifdef DEBUG
104    Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
105    tm_decode(dt, &tm);
106    Dmsg6(200, "m=%d d=%d y=%d h=%d m=%d s=%d\n", tm.tm_mon+1, tm.tm_mday, tm.tm_year+1900,
107       tm.tm_hour, tm.tm_min, tm.tm_sec);
108 #endif
109 }
110
111 /*
112  * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
113  *   the number of microseconds since Epoch Time (1 Jan 1970).
114  */
115
116 btime_t get_current_btime()
117 {
118    struct timeval tv;
119    if (gettimeofday(&tv, NULL) != 0) {
120       tv.tv_sec = (long)time(NULL);   /* fall back to old method */
121       tv.tv_usec = 0;
122    }
123    return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
124 }
125
126 /* Convert btime to Unix time */
127 time_t btime_to_unix(btime_t bt)
128 {
129    return (time_t)(bt/1000000);                    
130 }
131
132 /* Convert btime to utime */
133 utime_t btime_to_utime(btime_t bt)
134 {
135    return (utime_t)bt;
136 }
137
138
139
140 /*  date_encode  --  Encode civil date as a Julian day number.  */
141
142 /* Deprecated. Do not use. */
143 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
144 {
145
146     /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
147
148     int32_t a, b, m;
149     uint32_t y;
150
151     ASSERT(month < 13);
152     ASSERT(day > 0 && day < 32);
153
154     m = month;
155     y = year;
156
157     if (m <= 2) {
158         y--;
159         m += 12;
160     }
161
162     /* Determine whether date is in Julian or Gregorian calendar based on
163        canonical date of calendar reform. */
164
165     if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
166         b = 0;
167     } else {
168         a = ((int) (y / 100));
169         b = 2 - a + (a / 4);
170     }
171
172     return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
173                 day + b - 1524.5);
174 }
175
176 /*  time_encode  --  Encode time from hours, minutes, and seconds
177                      into a fraction of a day.  */
178
179 /* Deprecated. Do not use. */
180 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
181                    float32_t second_fraction)
182 {
183     ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
184     return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
185                      second_fraction;
186 }
187
188 /*  date_time_encode  --  Set day number and fraction from date
189                           and time.  */
190
191 /* Deprecated. Do not use. */
192 void date_time_encode(struct date_time *dt,
193                       uint32_t year, uint8_t month, uint8_t day,
194                       uint8_t hour, uint8_t minute, uint8_t second,
195                       float32_t second_fraction)
196 {
197     dt->julian_day_number = date_encode(year, month, day);
198     dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
199 }
200
201 /*  date_decode  --  Decode a Julian day number into civil date.  */
202
203 /* Deprecated. Do not use. */
204 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
205                  uint8_t *day)
206 {
207     fdate_t z, f, a, alpha, b, c, d, e;
208
209     date += 0.5;
210     z = floor(date);
211     f = date - z;
212
213     if (z < 2299161.0) {
214         a = z;
215     } else {
216         alpha = floor((z - 1867216.25) / 36524.25);
217         a = z + 1 + alpha - floor(alpha / 4);
218     }
219
220     b = a + 1524;
221     c = floor((b - 122.1) / 365.25);
222     d = floor(365.25 * c);
223     e = floor((b - d) / 30.6001);
224
225     *day = (uint8_t) (b - d - floor(30.6001 * e) + f);
226     *month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
227     *year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
228 }
229
230 /*  time_decode  --  Decode a day fraction into civil time.  */
231
232 /* Deprecated. Do not use. */
233 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
234                  uint8_t *second, float32_t *second_fraction)
235 {
236     uint32_t ij;
237
238     ij = (uint32_t) ((time - floor(time)) * 86400.0);
239     *hour = (uint8_t) (ij / 3600L);
240     *minute = (uint8_t) ((ij / 60L) % 60L);
241     *second = (uint8_t) (ij % 60L);
242     if (second_fraction != NULL) {
243         *second_fraction = time - floor(time);
244     }
245 }
246
247 /*  date_time_decode  --  Decode a Julian day and day fraction
248                           into civil date and time.  */
249
250 /* Deprecated. Do not use. */
251 void date_time_decode(struct date_time *dt,
252                       uint32_t *year, uint8_t *month, uint8_t *day,
253                       uint8_t *hour, uint8_t *minute, uint8_t *second,
254                       float32_t *second_fraction)
255 {
256     date_decode(dt->julian_day_number, year, month, day);
257     time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
258 }
259
260 /*  tm_encode  --  Encode a civil date and time from a tm structure   
261  *                 to a Julian day and day fraction.
262  */
263
264 /* Deprecated. Do not use. */
265 void tm_encode(struct date_time *dt,
266                       struct tm *tm) 
267 {
268     uint32_t year;
269     uint8_t month, day, hour, minute, second;
270
271     year = tm->tm_year + 1900;
272     month = tm->tm_mon + 1;
273     day = tm->tm_mday;
274     hour = tm->tm_hour;
275     minute = tm->tm_min;
276     second = tm->tm_sec;
277     dt->julian_day_number = date_encode(year, month, day);
278     dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
279 }
280
281
282 /*  tm_decode  --  Decode a Julian day and day fraction
283                    into civil date and time in tm structure */
284
285 /* Deprecated. Do not use. */
286 void tm_decode(struct date_time *dt,
287                       struct tm *tm) 
288 {
289     uint32_t year;
290     uint8_t month, day, hour, minute, second;
291
292     date_decode(dt->julian_day_number, &year, &month, &day);
293     time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
294     tm->tm_year = year - 1900;
295     tm->tm_mon = month - 1;
296     tm->tm_mday = day;
297     tm->tm_hour = hour;
298     tm->tm_min = minute;
299     tm->tm_sec = second;
300 }
301
302
303 /*  date_time_compare  --  Compare two dates and times and return
304                            the relationship as follows:
305
306                                     -1    dt1 < dt2
307                                      0    dt1 = dt2
308                                      1    dt1 > dt2
309 */
310
311 /* Deprecated. Do not use. */
312 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
313 {
314     if (dt1->julian_day_number == dt2->julian_day_number) {
315         if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
316             return 0;
317         }
318         return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
319     }
320     return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;
321 }