]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/btime.c
Big reorganization of restore code into lib/attr.c
[bacula/bacula] / bacula / src / lib / btime.c
index f17f1de64cf9af9ce4ef75dd0191b727808310f3..fdeb2f09c30e15bae2791c820786321de9d9ee24 100644 (file)
@@ -4,7 +4,7 @@
  *   Version $Id$
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2003 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
@@ -30,7 +30,8 @@
  *  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 (time_t, ...) and is 1 Jan 1970 at 0:0
+ *  Epoch is the base of Unix time in seconds (time_t, ...) 
+ *     and is 1 Jan 1970 at 0:0
  *
  *  The major two times that should be left are:
  *     btime_t (64 bit integer in microseconds base Epoch)
@@ -40,6 +41,7 @@
 #include "bacula.h"
 #include <math.h>
 
+/* Formatted time */
 void bstrftime(char *dt, int maxlen, utime_t tim)
 {
    time_t ttime = tim;
@@ -50,19 +52,46 @@ void bstrftime(char *dt, int maxlen, utime_t tim)
    strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
 }
 
+/* Unix time to standard time string yyyy-mm-dd hh:mm:ss */
+char *bstrutime(char *dt, int maxlen, utime_t tim)
+{
+   time_t ttime = 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 -1;
+      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;
    }
-   tm.tm_mon--;
-   tm.tm_year -= 1900;
-   return (utime_t)mktime(&tm);
+   return (utime_t)ttime;
 }
 
+/* Deprecated. Do not use. */
 void get_current_time(struct date_time *dt)
 {
    struct tm tm;
@@ -112,6 +141,7 @@ utime_t btime_to_utime(btime_t bt)
 
 /*  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)
 {
 
@@ -148,6 +178,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)
 {
@@ -159,6 +190,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,
@@ -170,6 +202,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)
 {
@@ -198,6 +231,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)
 {
@@ -215,6 +249,7 @@ void time_decode(ftime_t time, uint8_t *hour, uint8_t *minute,
 /*  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,
@@ -228,6 +263,7 @@ void date_time_decode(struct date_time *dt,
  *                to a Julian day and day fraction.
  */
 
+/* Deprecated. Do not use. */
 void tm_encode(struct date_time *dt,
                      struct tm *tm) 
 {
@@ -248,6 +284,7 @@ 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) 
 {
@@ -273,6 +310,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) {