]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/btime.c
Add verbose, Uname info, better Device error msg
[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 void bstrftime(char *dt, int maxlen, utime_t tim)
45 {
46    time_t ttime = tim;
47    struct tm tm;
48    
49    /* ***FIXME**** the format and localtime_r() should be user configurable */
50    localtime_r(&ttime, &tm);
51    strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
52 }
53
54 utime_t str_to_utime(char *str) 
55 {
56    struct tm tm;
57    time_t ttime;
58
59    if (sscanf(str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
60                                         &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
61       return 0;
62    }
63    if (tm.tm_mon > 0) {
64       tm.tm_mon--;
65    } else { 
66       return 0;
67    }
68    if (tm.tm_year >= 1900) {
69       tm.tm_year -= 1900;
70    } else {
71       return 0;
72    }
73    ttime = mktime(&tm);
74    if (ttime == -1) {       
75       ttime = 0;
76    }
77    return (utime_t)ttime;
78 }
79
80 void get_current_time(struct date_time *dt)
81 {
82    struct tm tm;
83    time_t now;
84
85    now = time(NULL);
86    gmtime_r(&now, &tm);
87    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,
88       tm.tm_hour, tm.tm_min, tm.tm_sec);
89    tm_encode(dt, &tm);
90 #ifdef DEBUG
91    Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
92    tm_decode(dt, &tm);
93    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,
94       tm.tm_hour, tm.tm_min, tm.tm_sec);
95 #endif
96 }
97
98 /*
99  * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
100  *   the number of microseconds since Epoch Time (1 Jan 1970).
101  */
102
103 btime_t get_current_btime()
104 {
105    struct timeval tv;
106    if (gettimeofday(&tv, NULL) != 0) {
107       tv.tv_sec = (long)time(NULL);   /* fall back to old method */
108       tv.tv_usec = 0;
109    }
110    return ((btime_t)tv.tv_sec) * 1000000 + (btime_t)tv.tv_usec;
111 }
112
113 /* Convert btime to Unix time */
114 time_t btime_to_unix(btime_t bt)
115 {
116    return (time_t)(bt/1000000);                    
117 }
118
119 /* Convert btime to utime */
120 utime_t btime_to_utime(btime_t bt)
121 {
122    return (utime_t)bt;
123 }
124
125
126
127 /*  date_encode  --  Encode civil date as a Julian day number.  */
128
129 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
130 {
131
132     /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */
133
134     int32_t a, b, m;
135     uint32_t y;
136
137     ASSERT(month < 13);
138     ASSERT(day > 0 && day < 32);
139
140     m = month;
141     y = year;
142
143     if (m <= 2) {
144         y--;
145         m += 12;
146     }
147
148     /* Determine whether date is in Julian or Gregorian calendar based on
149        canonical date of calendar reform. */
150
151     if ((year < 1582) || ((year == 1582) && ((month < 9) || (month == 9 && day < 5)))) {
152         b = 0;
153     } else {
154         a = ((int) (y / 100));
155         b = 2 - a + (a / 4);
156     }
157
158     return (((int32_t) (365.25 * (y + 4716))) + ((int) (30.6001 * (m + 1))) +
159                 day + b - 1524.5);
160 }
161
162 /*  time_encode  --  Encode time from hours, minutes, and seconds
163                      into a fraction of a day.  */
164
165 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
166                    float32_t second_fraction)
167 {
168     ASSERT((second_fraction >= 0.0) || (second_fraction < 1.0));
169     return (ftime_t) (((second + 60L * (minute + 60L * hour)) / 86400.0)) +
170                      second_fraction;
171 }
172
173 /*  date_time_encode  --  Set day number and fraction from date
174                           and time.  */
175
176 void date_time_encode(struct date_time *dt,
177                       uint32_t year, uint8_t month, uint8_t day,
178                       uint8_t hour, uint8_t minute, uint8_t second,
179                       float32_t second_fraction)
180 {
181     dt->julian_day_number = date_encode(year, month, day);
182     dt->julian_day_fraction = time_encode(hour, minute, second, second_fraction);
183 }
184
185 /*  date_decode  --  Decode a Julian day number into civil date.  */
186
187 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
188                  uint8_t *day)
189 {
190     fdate_t z, f, a, alpha, b, c, d, e;
191
192     date += 0.5;
193     z = floor(date);
194     f = date - z;
195
196     if (z < 2299161.0) {
197         a = z;
198     } else {
199         alpha = floor((z - 1867216.25) / 36524.25);
200         a = z + 1 + alpha - floor(alpha / 4);
201     }
202
203     b = a + 1524;
204     c = floor((b - 122.1) / 365.25);
205     d = floor(365.25 * c);
206     e = floor((b - d) / 30.6001);
207
208     *day = (uint8_t) (b - d - floor(30.6001 * e) + f);
209     *month = (uint8_t) ((e < 14) ? (e - 1) : (e - 13));
210     *year = (uint32_t) ((*month > 2) ? (c - 4716) : (c - 4715));
211 }
212
213 /*  time_decode  --  Decode a day fraction into civil time.  */
214
215 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
216                  uint8_t *second, float32_t *second_fraction)
217 {
218     uint32_t ij;
219
220     ij = (uint32_t) ((time - floor(time)) * 86400.0);
221     *hour = (uint8_t) (ij / 3600L);
222     *minute = (uint8_t) ((ij / 60L) % 60L);
223     *second = (uint8_t) (ij % 60L);
224     if (second_fraction != NULL) {
225         *second_fraction = time - floor(time);
226     }
227 }
228
229 /*  date_time_decode  --  Decode a Julian day and day fraction
230                           into civil date and time.  */
231
232 void date_time_decode(struct date_time *dt,
233                       uint32_t *year, uint8_t *month, uint8_t *day,
234                       uint8_t *hour, uint8_t *minute, uint8_t *second,
235                       float32_t *second_fraction)
236 {
237     date_decode(dt->julian_day_number, year, month, day);
238     time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
239 }
240
241 /*  tm_encode  --  Encode a civil date and time from a tm structure   
242  *                 to a Julian day and day fraction.
243  */
244
245 void tm_encode(struct date_time *dt,
246                       struct tm *tm) 
247 {
248     uint32_t year;
249     uint8_t month, day, hour, minute, second;
250
251     year = tm->tm_year + 1900;
252     month = tm->tm_mon + 1;
253     day = tm->tm_mday;
254     hour = tm->tm_hour;
255     minute = tm->tm_min;
256     second = tm->tm_sec;
257     dt->julian_day_number = date_encode(year, month, day);
258     dt->julian_day_fraction = time_encode(hour, minute, second, 0.0);
259 }
260
261
262 /*  tm_decode  --  Decode a Julian day and day fraction
263                    into civil date and time in tm structure */
264
265 void tm_decode(struct date_time *dt,
266                       struct tm *tm) 
267 {
268     uint32_t year;
269     uint8_t month, day, hour, minute, second;
270
271     date_decode(dt->julian_day_number, &year, &month, &day);
272     time_decode(dt->julian_day_fraction, &hour, &minute, &second, NULL);
273     tm->tm_year = year - 1900;
274     tm->tm_mon = month - 1;
275     tm->tm_mday = day;
276     tm->tm_hour = hour;
277     tm->tm_min = minute;
278     tm->tm_sec = second;
279 }
280
281
282 /*  date_time_compare  --  Compare two dates and times and return
283                            the relationship as follows:
284
285                                     -1    dt1 < dt2
286                                      0    dt1 = dt2
287                                      1    dt1 > dt2
288 */
289
290 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
291 {
292     if (dt1->julian_day_number == dt2->julian_day_number) {
293         if (dt1->julian_day_fraction == dt2->julian_day_fraction) {
294             return 0;
295         }
296         return (dt1->julian_day_fraction < dt2->julian_day_fraction) ? -1 : 1;
297     }
298     return (dt1->julian_day_number - dt2->julian_day_number) ? -1 : 1;
299 }