date,touch: accept Jan 7 00:00:00 2010 format
[oweals/busybox.git] / libbb / time.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 2007 Denys Vlasenko
6  *
7  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
8  */
9 #include "libbb.h"
10
11 void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
12 {
13         char end = '\0';
14         const char *last_colon = strrchr(date_str, ':');
15
16         if (last_colon != NULL) {
17                 /* Parse input and assign appropriately to ptm */
18 #if ENABLE_DESKTOP
19                 const char *endp;
20 #endif
21
22                 /* HH:MM */
23                 if (sscanf(date_str, "%u:%u%c",
24                                         &ptm->tm_hour,
25                                         &ptm->tm_min,
26                                         &end) >= 2) {
27                         /* no adjustments needed */
28                 } else
29                 /* mm.dd-HH:MM */
30                 if (sscanf(date_str, "%u.%u-%u:%u%c",
31                                         &ptm->tm_mon, &ptm->tm_mday,
32                                         &ptm->tm_hour, &ptm->tm_min,
33                                         &end) >= 4) {
34                         /* Adjust month from 1-12 to 0-11 */
35                         ptm->tm_mon -= 1;
36                 } else
37                 /* yyyy.mm.dd-HH:MM */
38                 if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
39                                         &ptm->tm_mon, &ptm->tm_mday,
40                                         &ptm->tm_hour, &ptm->tm_min,
41                                         &end) >= 5) {
42                         ptm->tm_year -= 1900; /* Adjust years */
43                         ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
44                 } else
45                 /* yyyy-mm-dd HH:MM */
46                 if (sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
47                                         &ptm->tm_mon, &ptm->tm_mday,
48                                         &ptm->tm_hour, &ptm->tm_min,
49                                         &end) >= 5) {
50                         ptm->tm_year -= 1900; /* Adjust years */
51                         ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
52                 } else
53 #if ENABLE_DESKTOP  /* strptime is BIG: ~1k in uclibc, ~10k in glibc */
54                 /* month_name d HH:MM:SS YYYY. Supported by GNU date */
55                 if ((endp = strptime(date_str, "%b %d %T %Y", ptm)) != NULL
56                  && *endp == '\0'
57                 ) {
58                         return; /* don't fall through to end == ":" check */
59                 } else
60 #endif
61 //TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes)
62                 {
63                         bb_error_msg_and_die(bb_msg_invalid_date, date_str);
64                 }
65                 if (end == ':') {
66                         /* xxx:SS */
67                         if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
68                                 end = '\0';
69                         /* else end != NUL and we error out */
70                 }
71         } else {
72                 /* Googled the following on an old date manpage:
73                  *
74                  * The canonical representation for setting the date/time is:
75                  * cc   Century (either 19 or 20)
76                  * yy   Year in abbreviated form (e.g. 89, 06)
77                  * mm   Numeric month, a number from 1 to 12
78                  * dd   Day, a number from 1 to 31
79                  * HH   Hour, a number from 0 to 23
80                  * MM   Minutes, a number from 0 to 59
81                  * .SS  Seconds, a number from 0 to 61 (with leap seconds)
82                  * Everything but the minutes is optional
83                  *
84                  * This coincides with the format of "touch -t TIME"
85                  */
86                 int len = strchrnul(date_str, '.') - date_str;
87
88                 /* MM[.SS] */
89                 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
90                                         &ptm->tm_min,
91                                         &end) >= 1) {
92                 } else
93                 /* HHMM[.SS] */
94                 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
95                                         &ptm->tm_hour,
96                                         &ptm->tm_min,
97                                         &end) >= 2) {
98                 } else
99                 /* ddHHMM[.SS] */
100                 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
101                                         &ptm->tm_mday,
102                                         &ptm->tm_hour,
103                                         &ptm->tm_min,
104                                         &end) >= 3) {
105                 } else
106                 /* mmddHHMM[.SS] */
107                 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
108                                         &ptm->tm_mon,
109                                         &ptm->tm_mday,
110                                         &ptm->tm_hour,
111                                         &ptm->tm_min,
112                                         &end) >= 4) {
113                         /* Adjust month from 1-12 to 0-11 */
114                         ptm->tm_mon -= 1;
115                 } else
116                 /* yymmddHHMM[.SS] */
117                 if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
118                                         &ptm->tm_year,
119                                         &ptm->tm_mon,
120                                         &ptm->tm_mday,
121                                         &ptm->tm_hour,
122                                         &ptm->tm_min,
123                                         &end) >= 5) {
124                         /* Adjust month from 1-12 to 0-11 */
125                         ptm->tm_mon -= 1;
126                 } else
127                 /* ccyymmddHHMM[.SS] */
128                 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
129                                         &ptm->tm_year,
130                                         &ptm->tm_mon,
131                                         &ptm->tm_mday,
132                                         &ptm->tm_hour,
133                                         &ptm->tm_min,
134                                         &end) >= 5) {
135                         ptm->tm_year -= 1900; /* Adjust years */
136                         ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
137                 } else {
138                         bb_error_msg_and_die(bb_msg_invalid_date, date_str);
139                 }
140                 if (end == '.') {
141                         /* xxx.SS */
142                         if (sscanf(strchr(date_str, '.') + 1, "%u%c",
143                                         &ptm->tm_sec, &end) == 1)
144                                 end = '\0';
145                         /* else end != NUL and we error out */
146                 }
147         }
148         if (end != '\0') {
149                 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
150         }
151 }
152
153 time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
154 {
155         time_t t = mktime(ptm);
156         if (t == (time_t) -1L) {
157                 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
158         }
159         return t;
160 }
161
162 #if ENABLE_MONOTONIC_SYSCALL
163
164 #include <sys/syscall.h>
165 /* Old glibc (< 2.3.4) does not provide this constant. We use syscall
166  * directly so this definition is safe. */
167 #ifndef CLOCK_MONOTONIC
168 #define CLOCK_MONOTONIC 1
169 #endif
170
171 /* libc has incredibly messy way of doing this,
172  * typically requiring -lrt. We just skip all this mess */
173 static void get_mono(struct timespec *ts)
174 {
175         if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
176                 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
177 }
178 unsigned long long FAST_FUNC monotonic_ns(void)
179 {
180         struct timespec ts;
181         get_mono(&ts);
182         return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
183 }
184 unsigned long long FAST_FUNC monotonic_us(void)
185 {
186         struct timespec ts;
187         get_mono(&ts);
188         return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
189 }
190 unsigned long long FAST_FUNC monotonic_ms(void)
191 {
192         struct timespec ts;
193         get_mono(&ts);
194         return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
195 }
196 unsigned FAST_FUNC monotonic_sec(void)
197 {
198         struct timespec ts;
199         get_mono(&ts);
200         return ts.tv_sec;
201 }
202
203 #else
204
205 unsigned long long FAST_FUNC monotonic_ns(void)
206 {
207         struct timeval tv;
208         gettimeofday(&tv, NULL);
209         return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
210 }
211 unsigned long long FAST_FUNC monotonic_us(void)
212 {
213         struct timeval tv;
214         gettimeofday(&tv, NULL);
215         return tv.tv_sec * 1000000ULL + tv.tv_usec;
216 }
217 unsigned long long FAST_FUNC monotonic_ms(void)
218 {
219         struct timeval tv;
220         gettimeofday(&tv, NULL);
221         return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
222 }
223 unsigned FAST_FUNC monotonic_sec(void)
224 {
225         return time(NULL);
226 }
227
228 #endif