Imported Upstream version 3.3.2
[debian/amanda] / common-src / timestamp.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26
27 #include "amanda.h"
28 #include "timestamp.h"
29 #include "conffile.h"
30 #include <glib.h>
31
32 /*
33  * Construct a datestamp (YYYYMMDD) from a time_t.
34  */
35 char * get_datestamp_from_time(time_t when) {
36     struct tm *tm;
37     
38     if(when == (time_t)0) {
39         when = time((time_t *)NULL);
40     }
41     
42     tm = localtime(&when);
43     return g_strdup_printf("%04d%02d%02d",
44                            tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
45 }
46
47 /*
48  * Construct a timestamp (YYYYMMDDHHMMSS) from a time_t.
49  */
50 char * get_timestamp_from_time(time_t when) {
51     struct tm *tm;
52
53     if(when == (time_t)0) {
54         when = time((time_t *)NULL);
55     } 
56
57     tm = localtime(&when);
58     return g_strdup_printf("%04d%02d%02d%02d%02d%02d",
59                            tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
60                            tm->tm_hour, tm->tm_min, tm->tm_sec);
61 }
62
63 char * get_proper_stamp_from_time(time_t when) {
64     /* note that this is reimplemented in perl in perl/Amanda/Util.swg */
65     if (getconf_boolean(CNF_USETIMESTAMPS)) {
66         return get_timestamp_from_time(when);
67     } else {
68         return get_datestamp_from_time(when);
69     }
70 }
71
72 time_t get_time_from_timestamp(char *timestamp)
73 {
74     struct tm tm;
75     char t[5];
76     time_t tt;
77     tm.tm_year = 0;
78     tm.tm_mon = 0;
79     tm.tm_mday = 1;
80     tm.tm_hour = 0;
81     tm.tm_min = 0;
82     tm.tm_sec = 0;
83
84     if (strlen(timestamp) >= 4) {
85         memcpy(t, timestamp, 4);
86         t[4]='\0';
87         tm.tm_year = atoi(t) - 1900;
88     }
89
90     if (strlen(timestamp) >= 6) {
91         memcpy(t, timestamp+4, 2);
92         t[2]='\0';
93         tm.tm_mon = atoi(t) - 1;
94     }
95
96     if (strlen(timestamp) >= 8) {
97         memcpy(t, timestamp+6, 2);
98         t[2]='\0';
99         tm.tm_mday = atoi(t);
100     }
101
102     if (strlen(timestamp) >= 10) {
103         memcpy(t, timestamp+8, 2);
104         t[2]='\0';
105         tm.tm_hour = atoi(t);
106     }
107
108     if (strlen(timestamp) >= 12) {
109         memcpy(t, timestamp+10, 2);
110         t[2]='\0';
111         tm.tm_min = atoi(t);
112     }
113
114     if (strlen(timestamp) >= 14) {
115         memcpy(t, timestamp+12, 2);
116         t[2]='\0';
117         tm.tm_sec = atoi(t);
118     }
119     tm.tm_wday = 0;
120     tm.tm_yday = 0;
121     tm.tm_isdst = -1;
122
123     tt = mktime(&tm);
124
125     return(tt);
126 }
127
128 time_state_t get_timestamp_state(char * timestamp) {
129     if (timestamp == NULL || *timestamp == '\0') {
130         return TIME_STATE_REPLACE;
131     } else if (strcmp(timestamp, "X") == 0) {
132         return TIME_STATE_UNDEF;
133     } else {
134         return TIME_STATE_SET;
135     }
136 }
137
138 char * get_undef_timestamp(void) {
139     return strdup("X");
140 }