]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/btime.c
- Fix cancel bug in FD on /lib/tls with zero pid in
[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-2004 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 UTC
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 for user display: dd-Mon-yyyy hh:mm */
45 char *bstrftime(char *dt, int maxlen, utime_t tim)
46 {
47    time_t ttime = (time_t)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    return dt;
54 }
55
56 /* Formatted time for user display: dd-Mon hh:mm */
57 char *bstrftime_ny(char *dt, int maxlen, utime_t tim)
58 {
59    time_t ttime = (time_t)tim;
60    struct tm tm;
61    
62    /* ***FIXME**** the format and localtime_r() should be user configurable */
63    localtime_r(&ttime, &tm);
64    strftime(dt, maxlen, "%d-%b %H:%M", &tm);
65    return dt;
66 }
67
68
69 /* Formatted time for user display: dd-Mon-yy hh:mm  (no century) */
70 char *bstrftime_nc(char *dt, int maxlen, utime_t tim)
71 {
72    time_t ttime = (time_t)tim;
73    struct tm tm;
74    
75    /* ***FIXME**** the format and localtime_r() should be user configurable */
76    localtime_r(&ttime, &tm);
77    /* NOTE! since the compiler complains about %y, I use %y and cut the century */
78    strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
79    strcpy(dt+7, dt+9);
80    return dt;
81 }
82
83
84 /* Unix time to standard time string yyyy-mm-dd hh:mm:ss */
85 char *bstrutime(char *dt, int maxlen, utime_t tim)
86 {
87    time_t ttime = (time_t)tim;
88    struct tm tm;
89    localtime_r(&ttime, &tm);
90    strftime(dt, maxlen, "%Y-%m-%d %H:%M:%S", &tm);
91    return dt;
92 }
93
94 /* Convert standard time string yyyy-mm-dd hh:mm:ss to Unix time */
95 utime_t str_to_utime(char *str) 
96 {
97    struct tm tm;
98    time_t ttime;
99
100    if (sscanf(str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
101                                         &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
102       return 0;
103    }
104    if (tm.tm_mon > 0) {
105       tm.tm_mon--;
106    } else { 
107       return 0;
108    }
109    if (tm.tm_year >= 1900) {
110       tm.tm_year -= 1900;
111    } else {
112       return 0;
113    }
114    tm.tm_wday = tm.tm_yday = 0;
115    tm.tm_isdst = -1;
116    ttime = mktime(&tm);
117    if (ttime == -1) {       
118       ttime = 0;
119    }
120    return (utime_t)ttime;
121 }
122
123
124 /*
125  * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
126  *   the number of microseconds since Epoch Time (1 Jan 1970) UTC.
127  */
128
129 btime_t get_current_btime()
130 {
131    struct timeval tv;
132    if (gettimeofday(&tv, NULL) != 0) {
133       tv.tv_sec = (long)time(NULL);   /* fall back to old method */
134       tv.tv_usec = 0;
135    }
136    return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
137 }
138
139 /* Convert btime to Unix time */
140 time_t btime_to_unix(btime_t bt)
141 {
142    return (time_t)(bt/1000000);                    
143 }
144
145 /* Convert btime to utime */
146 utime_t btime_to_utime(btime_t bt)
147 {
148    return (utime_t)(bt/1000000);
149 }
150
151 /*
152  * Return the week of the month, base 0 (wom)
153  *   given tm_mday and tm_wday. Value returned
154  *   can be from 0 to 4 => week1, ... week5
155  */
156 int tm_wom(int mday, int wday)
157 {
158    int fs;                       /* first sunday */
159    fs = (mday%7) - wday;
160    if (fs <= 0) {
161       fs += 7;
162    }
163    if (mday <= fs) {
164 //    Dmsg3(100, "mday=%d wom=0 wday=%d <= fs=%d\n", mday, wday, fs);
165       return 0;
166    }
167    int wom = 1 + (mday - fs - 1) / 7;
168 // Dmsg4(100, "mday=%d wom=%d wday=%d fs=%d\n", mday, wom, wday, fs);
169    return wom;
170 }  
171
172 /*
173  * Given a Unix date return the week of the year.
174  * The returned value can be 0-53.  Officially
175  * the weeks are numbered from 1 to 53 where week1
176  * is the week in which the first Thursday of the
177  * year occurs (alternatively, the week which contains
178  * the 4th of January).  We return 0, if the week of the
179  * year does not fall in the current year.
180  */
181 int tm_woy(time_t stime)
182 {
183    int woy, fty, tm_yday;
184    time_t time4;
185    struct tm tm;
186    memset(&tm, 0, sizeof(struct tm));
187    localtime_r(&stime, &tm);
188    tm_yday = tm.tm_yday;
189    tm.tm_mon = 0;
190    tm.tm_mday = 4;
191    time4 = mktime(&tm);
192    localtime_r(&time4, &tm);
193    fty = 1 - tm.tm_wday;
194    if (fty <= 0) {
195       fty += 7;
196    }
197    woy = tm_yday - fty + 4;
198    if (woy < 0) {
199       return 0;
200    }
201    return 1 + woy / 7;
202 }
203
204 /* Deprecated. Do not use. */
205 void get_current_time(struct date_time *dt)
206 {
207    struct tm tm;
208    time_t now;
209
210    now = time(NULL);
211    gmtime_r(&now, &tm);
212    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,
213       tm.tm_hour, tm.tm_min, tm.tm_sec);
214    tm_encode(dt, &tm);
215 #ifdef DEBUG
216    Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
217    tm_decode(dt, &tm);
218    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,
219       tm.tm_hour, tm.tm_min, tm.tm_sec);
220 #endif
221 }
222
223
224 /*  date_encode  --  Encode civil date as a Julian day number.  */
225
226 /* Deprecated. Do not use. */
227 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
228 {
229
230     /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
231
232     int32_t a, b, m;
233     uint32_t y;
234
235     ASSERT(month < 13);
236     ASSERT(day > 0 && day < 32);
237
238     m = month;
239     y = year;
240
241     if (m <= 2) {
242         y--;
243         m += 12;
244     }
245
246     /* Determine whether date is in Julian or Gregorian calendar based on
247        canonical date of calendar reform. */
248
249     if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
250         b = 0;
251     } else {
252         a = ((int) (y / 100));
253         b = 2 - a + (a / 4);
254     }
255
256     return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
257                 day + b - 1524.5);
258 }
259
260 /*  time_encode  --  Encode time from hours, minutes, and seconds
261                      into a fraction of a day.  */
262
263 /* Deprecated. Do not use. */
264 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
265                    float32_t second_fraction)
266 {
267     ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
268     return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
269                      second_fraction;
270 }
271
272 /*  date_time_encode  --  Set day number and fraction from date
273                           and time.  */
274
275 /* Deprecated. Do not use. */
276 void date_time_encode(struct date_time *dt,
277                       uint32_t year, uint8_t month, uint8_t day,
278                       uint8_t hour, uint8_t minute, uint8_t second,
279                       float32_t second_fraction)
280 {
281     dt->julian_day_number = date_encode(year, month, day);
282     dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
283 }
284
285 /*  date_decode  --  Decode a Julian day number into civil date.  */
286
287 /* Deprecated. Do not use. */
288 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
289                  uint8_t *day)
290 {
291     fdate_t z, f, a, alpha, b, c, d, e;
292
293     date += 0.5;
294     z = floor(date);
295     f = date - z;
296
297     if (z < 2299161.0) {
298         a = z;
299     } else {
300         alpha = floor((z - 1867216.25) / 36524.25);
301         a = z + 1 + alpha - floor(alpha / 4);
302     }
303
304     b = a + 1524;
305     c = floor((b - 122.1) / 365.25);
306     d = floor(365.25 * c);
307     e = floor((b - d) / 30.6001);
308
309     *day = (uint8_t) (b - d - floor(30.6001 * e) + f);
310     *month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
311     *year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
312 }
313
314 /*  time_decode  --  Decode a day fraction into civil time.  */
315
316 /* Deprecated. Do not use. */
317 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
318                  uint8_t *second, float32_t *second_fraction)
319 {
320     uint32_t ij;
321
322     ij = (uint32_t) ((time - floor(time)) * 86400.0);
323     *hour = (uint8_t) (ij / 3600L);
324     *minute = (uint8_t) ((ij / 60L) % 60L);
325     *second = (uint8_t) (ij % 60L);
326     if (second_fraction != NULL) {
327         *second_fraction = (float32_t)(time - floor(time));
328     }
329 }
330
331 /*  date_time_decode  --  Decode a Julian day and day fraction
332                           into civil date and time.  */
333
334 /* Deprecated. Do not use. */
335 void date_time_decode(struct date_time *dt,
336                       uint32_t *year, uint8_t *month, uint8_t *day,
337                       uint8_t *hour, uint8_t *minute, uint8_t *second,
338                       float32_t *second_fraction)
339 {
340     date_decode(dt->julian_day_number, year, month, day);
341     time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
342 }
343
344 /*  tm_encode  --  Encode a civil date and time from a tm structure   
345  *                 to a Julian day and day fraction.
346  */
347
348 /* Deprecated. Do not use. */
349 void tm_encode(struct date_time *dt,
350                       struct tm *tm) 
351 {
352     uint32_t year;
353     uint8_t month, day, hour, minute, second;
354
355     year = tm->tm_year + 1900;
356     month = tm->tm_mon + 1;
357     day = tm->tm_mday;
358     hour = tm->tm_hour;
359     minute = tm->tm_min;
360     second = tm->tm_sec;
361     dt->julian_day_number = date_encode(year, month, day);
362     dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
363 }
364
365
366 /*  tm_decode  --  Decode a Julian day and day fraction
367                    into civil date and time in tm structure */
368
369 /* Deprecated. Do not use. */
370 void tm_decode(struct date_time *dt,
371                       struct tm *tm) 
372 {
373     uint32_t year;
374     uint8_t month, day, hour, minute, second;
375
376     date_decode(dt->julian_day_number, &year, &month, &day);
377     time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
378     tm->tm_year = year - 1900;
379     tm->tm_mon = month - 1;
380     tm->tm_mday = day;
381     tm->tm_hour = hour;
382     tm->tm_min = minute;
383     tm->tm_sec = second;
384 }
385
386
387 /*  date_time_compare  --  Compare two dates and times and return
388                            the relationship as follows:
389
390                                     -1    dt1 < dt2
391                                      0    dt1 = dt2
392                                      1    dt1 > dt2
393 */
394
395 /* Deprecated. Do not use. */
396 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
397 {
398     if (dt1->julian_day_number == dt2->julian_day_number) {
399         if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
400             return 0;
401         }
402         return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
403     }
404     return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;
405 }