]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/btime.c
Make out of freespace non-fatal for removable devices -- i.e. behaves like tape
[bacula/bacula] / bacula / src / lib / btime.c
index 2485d60d49fd17bd76a722a46e9ee21d4bcd4275..3f0f548e345153249536a7aaed599be89467101f 100644 (file)
@@ -1,40 +1,30 @@
 /*
-   Bacula® - The Network Backup Solution
-
-   Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
-
-   The main author of Bacula is Kern Sibbald, with contributions from
-   many others, a complete list can be found in the file AUTHORS.
-   This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
-   License as published by the Free Software Foundation and included
-   in the file LICENSE.
-
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA.
-
-   Bacula® is a registered trademark of Kern Sibbald.
-   The licensor of Bacula is the Free Software Foundation Europe
-   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
-   Switzerland, email:ftf@fsfeurope.org.
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2016 Kern Sibbald
+
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
+
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
+
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
 */
 /*
  * Bacula floating point time and date routines -- John Walker
  *
  * Later double precision integer time/date routines -- Kern Sibbald
  *
- *   Version $Id$
  */
 
-
-/* Concerning times. There are a number of differnt time standards
+/*
+ * Concerning times. There are a number of different 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,
 #include <math.h>
 
 /* Formatted time for user display: dd-Mon-yyyy hh:mm */
-char *bstrftime(char *dt, int maxlen, utime_t tim)
+char *bstrftime(char *dt, int maxlen, utime_t utime)
 {
-   time_t ttime = (time_t)tim;
+   time_t time = (time_t)utime;
    struct tm tm;
 
    /* ***FIXME**** the format and localtime_r() should be user configurable */
-   (void)localtime_r(&ttime, &tm);
+   (void)localtime_r(&time, &tm);
    strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
    return dt;
 }
 
 /* Formatted time for user display: dd-Mon-yyyy hh:mm:ss */
-char *bstrftimes(char *dt, int maxlen, utime_t tim)
+char *bstrftimes(char *dt, int maxlen, utime_t utime)
 {
-   time_t ttime = (time_t)tim;
+   time_t time = (time_t)utime;
    struct tm tm;
 
    /* ***FIXME**** the format and localtime_r() should be user configurable */
-   (void)localtime_r(&ttime, &tm);
+   (void)localtime_r(&time, &tm);
    strftime(dt, maxlen, "%d-%b-%Y %H:%M:%S", &tm);
    return dt;
 }
 
+/* Formatted time with day name for user display: dd-Mon hh:mm */
+char *bstrftime_dn(char *dt, int maxlen, utime_t utime)
+{
+   time_t time = (time_t)utime;
+   struct tm tm;
+
+   /* ***FIXME**** the format and localtime_r() should be user configurable */
+   (void)localtime_r(&time, &tm);
+   strftime(dt, maxlen, "%a %d-%b %H:%M", &tm);
+   return dt;
+}
 
-/* Formatted time for user display: dd-Mon hh:mm */
-char *bstrftime_ny(char *dt, int maxlen, utime_t tim)
+/* Formatted time (no year) for user display: dd-Mon hh:mm */
+char *bstrftime_ny(char *dt, int maxlen, utime_t utime)
 {
-   time_t ttime = (time_t)tim;
+   time_t time = (time_t)utime;
    struct tm tm;
 
    /* ***FIXME**** the format and localtime_r() should be user configurable */
-   (void)localtime_r(&ttime, &tm);
+   (void)localtime_r(&time, &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)
+char *bstrftime_nc(char *dt, int maxlen, utime_t utime)
 {
-   time_t ttime = (time_t)tim;
+   time_t time = (time_t)utime;
    struct tm tm;
    char *p, *q;
 
    /* ***FIXME**** the format and localtime_r() should be user configurable */
-   (void)localtime_r(&ttime, &tm);
+   (void)localtime_r(&time, &tm);
    /* NOTE! since the compiler complains about %y, I use %y and cut the century */
    strftime(dt, maxlen, "%d-%b-%Y %H:%M", &tm);
    /* overlay the century */
@@ -112,11 +113,11 @@ char *bstrftime_nc(char *dt, int maxlen, utime_t tim)
 
 
 /* Unix time to standard time string yyyy-mm-dd hh:mm:ss */
-char *bstrutime(char *dt, int maxlen, utime_t tim)
+char *bstrutime(char *dt, int maxlen, utime_t utime)
 {
-   time_t ttime = (time_t)tim;
+   time_t time = (time_t)utime;
    struct tm tm;
-   (void)localtime_r(&ttime, &tm);
+   (void)localtime_r(&time, &tm);
    strftime(dt, maxlen, "%Y-%m-%d %H:%M:%S", &tm);
    return dt;
 }
@@ -125,7 +126,7 @@ char *bstrutime(char *dt, int maxlen, utime_t tim)
 utime_t str_to_utime(char *str)
 {
    struct tm tm;
-   time_t ttime;
+   time_t time;
 
    /* Check for bad argument */
    if (!str || *str == 0) {
@@ -148,11 +149,11 @@ utime_t str_to_utime(char *str)
    }
    tm.tm_wday = tm.tm_yday = 0;
    tm.tm_isdst = -1;
-   ttime = mktime(&tm);
-   if (ttime == -1) {
-      ttime = 0;
+   time = mktime(&tm);
+   if (time == -1) {
+      time = 0;
    }
-   return (utime_t)ttime;
+   return (utime_t)time;
 }
 
 
@@ -183,10 +184,35 @@ utime_t btime_to_utime(btime_t bt)
    return (utime_t)(bt/1000000);
 }
 
+/*
+ * Definition of a leap year from Wikipedia.
+ *  I knew it anyway but better check.
+ */
+static bool is_leap_year(int year)
+{
+   if (year % 400 == 0) return true;
+   if (year % 100 == 0) return false;
+   if (year % 4 == 0) return true;
+   return false;
+}
+
+/*
+ * Return the last day of the month, base 0
+ *   month=0-11, year is actual year
+ *   ldom is base 0
+ */
+int tm_ldom(int month, int year)
+{                     /* jan feb mar apr may jun jul aug sep oct nov dec */
+   static int dom[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+
+   if (is_leap_year(year) && month == 1) return 28;
+   return dom[month] - 1;
+}
+
 /*
  * 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
+ *   can be from 0 to 5 => week1, ... week6
  */
 int tm_wom(int mday, int wday)
 {
@@ -218,12 +244,13 @@ int tm_woy(time_t stime)
    int woy, fty, tm_yday;
    time_t time4;
    struct tm tm;
+
    memset(&tm, 0, sizeof(struct tm));
    (void)localtime_r(&stime, &tm);
    tm_yday = tm.tm_yday;
    tm.tm_mon = 0;
    tm.tm_mday = 4;
-   tm.tm_isdst = 0;                   /* 4 Jan is not DST */ 
+   tm.tm_isdst = 0;                   /* 4 Jan is not DST */
    time4 = mktime(&tm);
    (void)localtime_r(&time4, &tm);
    fty = 1 - tm.tm_wday;
@@ -257,8 +284,8 @@ void get_current_time(struct date_time *dt)
 }
 
 
-/*  date_encode  --  Encode civil date as a Julian day number.  */
 
+/*  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)
 {