]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/btime.c
Fix problem of accents with new Win32 code.
[bacula/bacula] / bacula / src / lib / btime.c
index a0325e6cc4c373bb712ee291b340c27cbad7c1e8..943a8860883b67356e7c00545a855fa840af7c73 100644 (file)
@@ -4,7 +4,7 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
  */
 
 
+/* Concerning times. There are a number of differnt time standards
+ * in Bacula (fdate_t, ftime_t, time_t (Unix standard), btime_t, and
+ *  utime_t).  fdate_t and ftime_t are deprecated and should no longer
+ *  be used, and in general, Unix time time_t should no longer be used,
+ *  it is being phased out.
+ *
+ *  Epoch is the base of Unix time in seconds (time_t, ...)
+ *     and is 1 Jan 1970 at 0:0 UTC
+ *
+ *  The major two times that should be left are:
+ *     btime_t (64 bit integer in microseconds base Epoch)
+ *     utime_t (64 bit integer in seconds base Epoch)
+ */
+
 #include "bacula.h"
 #include <math.h>
 
-void bstrftime(char *dt, int maxlen, uint32_t tim)
+/* Formatted time for user display: dd-Mon-yyyy hh:mm */
+char *bstrftime(char *dt, int maxlen, utime_t tim)
 {
-   time_t ttime = tim;
+   time_t ttime = (time_t)tim;
    struct tm tm;
-   
+
    /* ***FIXME**** the format and localtime_r() should be user configurable */
    localtime_r(&ttime, &tm);
    strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
+   return dt;
 }
 
-void get_current_time(struct date_time *dt)
+/* Formatted time for user display: dd-Mon-yyyy hh:mm:ss */
+char *bstrftimes(char *dt, int maxlen, utime_t tim)
 {
+   time_t ttime = (time_t)tim;
    struct tm tm;
-   time_t now;
 
-   now = time(NULL);
-   gmtime_r(&now, &tm);
-   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,
-      tm.tm_hour, tm.tm_min, tm.tm_sec);
-   tm_encode(dt, &tm);
-#ifdef DEBUG
-   Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
-   tm_decode(dt, &tm);
-   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,
-      tm.tm_hour, tm.tm_min, tm.tm_sec);
-#endif
+   /* ***FIXME**** the format and localtime_r() should be user configurable */
+   localtime_r(&ttime, &tm);
+   strftime(dt, maxlen, "%d-%b-%Y %H:%M:%S", &tm);
+   return dt;
 }
 
+
+/* Formatted time for user display: dd-Mon hh:mm */
+char *bstrftime_ny(char *dt, int maxlen, utime_t tim)
+{
+   time_t ttime = (time_t)tim;
+   struct tm tm;
+
+   /* ***FIXME**** the format and localtime_r() should be user configurable */
+   localtime_r(&ttime, &tm);
+   strftime(dt, maxlen, "%d-%b %H:%M", &tm);
+   return dt;
+}
+
+
+/* Formatted time for user display: dd-Mon-yy hh:mm  (no century) */
+char *bstrftime_nc(char *dt, int maxlen, utime_t tim)
+{
+   time_t ttime = (time_t)tim;
+   struct tm tm;
+
+   /* ***FIXME**** the format and localtime_r() should be user configurable */
+   localtime_r(&ttime, &tm);
+   /* NOTE! since the compiler complains about %y, I use %y and cut the century */
+   strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
+   strcpy(dt+7, dt+9);
+   return dt;
+}
+
+
+/* Unix time to standard time string yyyy-mm-dd hh:mm:ss */
+char *bstrutime(char *dt, int maxlen, utime_t tim)
+{
+   time_t ttime = (time_t)tim;
+   struct tm tm;
+   localtime_r(&ttime, &tm);
+   strftime(dt, maxlen, "%Y-%m-%d %H:%M:%S", &tm);
+   return dt;
+}
+
+/* Convert standard time string yyyy-mm-dd hh:mm:ss to Unix time */
+utime_t str_to_utime(char *str)
+{
+   struct tm tm;
+   time_t ttime;
+
+   if (sscanf(str, "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+                                       &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
+      return 0;
+   }
+   if (tm.tm_mon > 0) {
+      tm.tm_mon--;
+   } else {
+      return 0;
+   }
+   if (tm.tm_year >= 1900) {
+      tm.tm_year -= 1900;
+   } else {
+      return 0;
+   }
+   tm.tm_wday = tm.tm_yday = 0;
+   tm.tm_isdst = -1;
+   ttime = mktime(&tm);
+   if (ttime == -1) {
+      ttime = 0;
+   }
+   return (utime_t)ttime;
+}
+
+
 /*
  * Bacula's time (btime_t) is an unsigned 64 bit integer that contains
- *   the number of microseconds since Epoch Time (1 Jan 1970).
+ *   the number of microseconds since Epoch Time (1 Jan 1970) UTC.
  */
 
 btime_t get_current_btime()
@@ -71,14 +150,93 @@ btime_t get_current_btime()
 }
 
 /* Convert btime to Unix time */
-time_t btime_to_etime(btime_t bt)
+time_t btime_to_unix(btime_t bt)
+{
+   return (time_t)(bt/1000000);
+}
+
+/* Convert btime to utime */
+utime_t btime_to_utime(btime_t bt)
+{
+   return (utime_t)(bt/1000000);
+}
+
+/*
+ * Return the week of the month, base 0 (wom)
+ *   given tm_mday and tm_wday. Value returned
+ *   can be from 0 to 4 => week1, ... week5
+ */
+int tm_wom(int mday, int wday)
+{
+   int fs;                      /* first sunday */
+   fs = (mday%7) - wday;
+   if (fs <= 0) {
+      fs += 7;
+   }
+   if (mday <= fs) {
+//    Dmsg3(100, "mday=%d wom=0 wday=%d <= fs=%d\n", mday, wday, fs);
+      return 0;
+   }
+   int wom = 1 + (mday - fs - 1) / 7;
+// Dmsg4(100, "mday=%d wom=%d wday=%d fs=%d\n", mday, wom, wday, fs);
+   return wom;
+}
+
+/*
+ * Given a Unix date return the week of the year.
+ * The returned value can be 0-53.  Officially
+ * the weeks are numbered from 1 to 53 where week1
+ * is the week in which the first Thursday of the
+ * year occurs (alternatively, the week which contains
+ * the 4th of January).  We return 0, if the week of the
+ * year does not fall in the current year.
+ */
+int tm_woy(time_t stime)
 {
-   return (time_t)(bt/1000000);                   
+   int woy, fty, tm_yday;
+   time_t time4;
+   struct tm tm;
+   memset(&tm, 0, sizeof(struct tm));
+   localtime_r(&stime, &tm);
+   tm_yday = tm.tm_yday;
+   tm.tm_mon = 0;
+   tm.tm_mday = 4;
+   time4 = mktime(&tm);
+   localtime_r(&time4, &tm);
+   fty = 1 - tm.tm_wday;
+   if (fty <= 0) {
+      fty += 7;
+   }
+   woy = tm_yday - fty + 4;
+   if (woy < 0) {
+      return 0;
+   }
+   return 1 + woy / 7;
+}
+
+/* Deprecated. Do not use. */
+void get_current_time(struct date_time *dt)
+{
+   struct tm tm;
+   time_t now;
+
+   now = time(NULL);
+   gmtime_r(&now, &tm);
+   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,
+      tm.tm_hour, tm.tm_min, tm.tm_sec);
+   tm_encode(dt, &tm);
+#ifdef DEBUG
+   Dmsg2(200, "jday=%f jmin=%f\n", dt->julian_day_number, dt->julian_day_fraction);
+   tm_decode(dt, &tm);
+   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,
+      tm.tm_hour, tm.tm_min, tm.tm_sec);
+#endif
 }
 
 
 /*  date_encode  --  Encode civil date as a Julian day number. */
 
+/* Deprecated. Do not use. */
 fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
 {
 
@@ -115,6 +273,7 @@ fdate_t date_encode(uint32_t year, uint8_t month, uint8_t day)
 /*  time_encode  --  Encode time from hours, minutes, and seconds
                     into a fraction of a day.  */
 
+/* Deprecated. Do not use. */
 ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
                   float32_t second_fraction)
 {
@@ -126,6 +285,7 @@ ftime_t time_encode(uint8_t hour, uint8_t minute, uint8_t second,
 /*  date_time_encode  --  Set day number and fraction from date
                          and time.  */
 
+/* Deprecated. Do not use. */
 void date_time_encode(struct date_time *dt,
                      uint32_t year, uint8_t month, uint8_t day,
                      uint8_t hour, uint8_t minute, uint8_t second,
@@ -137,6 +297,7 @@ void date_time_encode(struct date_time *dt,
 
 /*  date_decode  --  Decode a Julian day number into civil date.  */
 
+/* Deprecated. Do not use. */
 void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
                 uint8_t *day)
 {
@@ -165,6 +326,7 @@ void date_decode(fdate_t date, uint32_t *year, uint8_t *month,
 
 /*  time_decode  --  Decode a day fraction into civil time.  */
 
+/* Deprecated. Do not use. */
 void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
                 uint8_t *second, float32_t *second_fraction)
 {
@@ -175,13 +337,14 @@ void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
     *minute = (uint8_t) ((ij / 60L) % 60L);
     *second = (uint8_t) (ij % 60L);
     if (second_fraction != NULL) {
-       *second_fraction = time - floor(time);
+       *second_fraction = (float32_t)(time - floor(time));
     }
 }
 
 /*  date_time_decode  --  Decode a Julian day and day fraction
                          into civil date and time.  */
 
+/* Deprecated. Do not use. */
 void date_time_decode(struct date_time *dt,
                      uint32_t *year, uint8_t *month, uint8_t *day,
                      uint8_t *hour, uint8_t *minute, uint8_t *second,
@@ -191,12 +354,13 @@ void date_time_decode(struct date_time *dt,
     time_decode(dt->julian_day_fraction, hour, minute, second, second_fraction);
 }
 
-/*  tm_encode  --  Encode a civil date and time from a tm structure   
+/*  tm_encode  --  Encode a civil date and time from a tm structure
  *                to a Julian day and day fraction.
  */
 
+/* Deprecated. Do not use. */
 void tm_encode(struct date_time *dt,
-                     struct tm *tm) 
+                     struct tm *tm)
 {
     uint32_t year;
     uint8_t month, day, hour, minute, second;
@@ -215,8 +379,9 @@ void tm_encode(struct date_time *dt,
 /*  tm_decode  --  Decode a Julian day and day fraction
                   into civil date and time in tm structure */
 
+/* Deprecated. Do not use. */
 void tm_decode(struct date_time *dt,
-                     struct tm *tm) 
+                     struct tm *tm)
 {
     uint32_t year;
     uint8_t month, day, hour, minute, second;
@@ -240,6 +405,7 @@ void tm_decode(struct date_time *dt,
                                     1    dt1 > dt2
 */
 
+/* Deprecated. Do not use. */
 int date_time_compare(struct date_time *dt1, struct date_time *dt2)
 {
     if (dt1->julian_day_number == dt2->julian_day_number) {