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