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