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