]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/btime.c
10a2ff79251f12ea6eda347fc65c7164774d0aba
[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 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 /* Deprecated. Do not use. */
96 void get_current_time(struct date_time *dt)
97 {
98    struct tm tm;
99    time_t now;
100
101    now = time(NULL);
102    gmtime_r(&now, &tm);
103    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,
104       tm.tm_hour, tm.tm_min, tm.tm_sec);
105    tm_encode(dt, &tm);
106 #ifdef DEBUG
107    Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
108    tm_decode(dt, &tm);
109    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,
110       tm.tm_hour, tm.tm_min, tm.tm_sec);
111 #endif
112 }
113
114 /*
115  * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
116  *   the number of microseconds since Epoch Time (1 Jan 1970).
117  */
118
119 btime_t get_current_btime()
120 {
121    struct timeval tv;
122    if (gettimeofday(&tv, NULL) != 0) {
123       tv.tv_sec = (long)time(NULL);   /* fall back to old method */
124       tv.tv_usec = 0;
125    }
126    return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
127 }
128
129 /* Convert btime to Unix time */
130 time_t btime_to_unix(btime_t bt)
131 {
132    return (time_t)(bt/1000000);                    
133 }
134
135 /* Convert btime to utime */
136 utime_t btime_to_utime(btime_t bt)
137 {
138    return (utime_t)bt;
139 }
140
141
142
143 /*  date_encode  --  Encode civil date as a Julian day number.  */
144
145 /* Deprecated. Do not use. */
146 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
147 {
148
149     /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
150
151     int32_t a, b, m;
152     uint32_t y;
153
154     ASSERT(month < 13);
155     ASSERT(day > 0 && day < 32);
156
157     m = month;
158     y = year;
159
160     if (m <= 2) {
161         y--;
162         m += 12;
163     }
164
165     /* Determine whether date is in Julian or Gregorian calendar based on
166        canonical date of calendar reform. */
167
168     if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
169         b = 0;
170     } else {
171         a = ((int) (y / 100));
172         b = 2 - a + (a / 4);
173     }
174
175     return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
176                 day + b - 1524.5);
177 }
178
179 /*  time_encode  --  Encode time from hours, minutes, and seconds
180                      into a fraction of a day.  */
181
182 /* Deprecated. Do not use. */
183 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
184                    float32_t second_fraction)
185 {
186     ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
187     return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
188                      second_fraction;
189 }
190
191 /*  date_time_encode  --  Set day number and fraction from date
192                           and time.  */
193
194 /* Deprecated. Do not use. */
195 void date_time_encode(struct date_time *dt,
196                       uint32_t year, uint8_t month, uint8_t day,
197                       uint8_t hour, uint8_t minute, uint8_t second,
198                       float32_t second_fraction)
199 {
200     dt->julian_day_number = date_encode(year, month, day);
201     dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
202 }
203
204 /*  date_decode  --  Decode a Julian day number into civil date.  */
205
206 /* Deprecated. Do not use. */
207 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
208                  uint8_t *day)
209 {
210     fdate_t z, f, a, alpha, b, c, d, e;
211
212     date += 0.5;
213     z = floor(date);
214     f = date - z;
215
216     if (z < 2299161.0) {
217         a = z;
218     } else {
219         alpha = floor((z - 1867216.25) / 36524.25);
220         a = z + 1 + alpha - floor(alpha / 4);
221     }
222
223     b = a + 1524;
224     c = floor((b - 122.1) / 365.25);
225     d = floor(365.25 * c);
226     e = floor((b - d) / 30.6001);
227
228     *day = (uint8_t) (b - d - floor(30.6001 * e) + f);
229     *month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
230     *year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
231 }
232
233 /*  time_decode  --  Decode a day fraction into civil time.  */
234
235 /* Deprecated. Do not use. */
236 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
237                  uint8_t *second, float32_t *second_fraction)
238 {
239     uint32_t ij;
240
241     ij = (uint32_t) ((time - floor(time)) * 86400.0);
242     *hour = (uint8_t) (ij / 3600L);
243     *minute = (uint8_t) ((ij / 60L) % 60L);
244     *second = (uint8_t) (ij % 60L);
245     if (second_fraction != NULL) {
246         *second_fraction = time - floor(time);
247     }
248 }
249
250 /*  date_time_decode  --  Decode a Julian day and day fraction
251                           into civil date and time.  */
252
253 /* Deprecated. Do not use. */
254 void date_time_decode(struct date_time *dt,
255                       uint32_t *year, uint8_t *month, uint8_t *day,
256                       uint8_t *hour, uint8_t *minute, uint8_t *second,
257                       float32_t *second_fraction)
258 {
259     date_decode(dt->julian_day_number, year, month, day);
260     time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
261 }
262
263 /*  tm_encode  --  Encode a civil date and time from a tm structure   
264  *                 to a Julian day and day fraction.
265  */
266
267 /* Deprecated. Do not use. */
268 void tm_encode(struct date_time *dt,
269                       struct tm *tm) 
270 {
271     uint32_t year;
272     uint8_t month, day, hour, minute, second;
273
274     year = tm->tm_year + 1900;
275     month = tm->tm_mon + 1;
276     day = tm->tm_mday;
277     hour = tm->tm_hour;
278     minute = tm->tm_min;
279     second = tm->tm_sec;
280     dt->julian_day_number = date_encode(year, month, day);
281     dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
282 }
283
284
285 /*  tm_decode  --  Decode a Julian day and day fraction
286                    into civil date and time in tm structure */
287
288 /* Deprecated. Do not use. */
289 void tm_decode(struct date_time *dt,
290                       struct tm *tm) 
291 {
292     uint32_t year;
293     uint8_t month, day, hour, minute, second;
294
295     date_decode(dt->julian_day_number, &year, &month, &day);
296     time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
297     tm->tm_year = year - 1900;
298     tm->tm_mon = month - 1;
299     tm->tm_mday = day;
300     tm->tm_hour = hour;
301     tm->tm_min = minute;
302     tm->tm_sec = second;
303 }
304
305
306 /*  date_time_compare  --  Compare two dates and times and return
307                            the relationship as follows:
308
309                                     -1    dt1 < dt2
310                                      0    dt1 = dt2
311                                      1    dt1 > dt2
312 */
313
314 /* Deprecated. Do not use. */
315 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
316 {
317     if (dt1->julian_day_number == dt2->julian_day_number) {
318         if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
319             return 0;
320         }
321         return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
322     }
323     return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;
324 }