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