date,touch: treat 2-digit years better (fit them into +-50 yrs around today)
[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 GPLv2, see file LICENSE in this source tree.
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 if (date_str[0] == '@') {
72                 time_t t = bb_strtol(date_str + 1, NULL, 10);
73                 if (!errno) {
74                         struct tm *lt = localtime(&t);
75                         if (lt) {
76                                 *ptm = *lt;
77                                 return;
78                         }
79                 }
80                 end = '1';
81         } else {
82                 /* Googled the following on an old date manpage:
83                  *
84                  * The canonical representation for setting the date/time is:
85                  * cc   Century (either 19 or 20)
86                  * yy   Year in abbreviated form (e.g. 89, 06)
87                  * mm   Numeric month, a number from 1 to 12
88                  * dd   Day, a number from 1 to 31
89                  * HH   Hour, a number from 0 to 23
90                  * MM   Minutes, a number from 0 to 59
91                  * .SS  Seconds, a number from 0 to 61 (with leap seconds)
92                  * Everything but the minutes is optional
93                  *
94                  * This coincides with the format of "touch -t TIME"
95                  */
96                 unsigned cur_year = ptm->tm_year;
97                 int len = strchrnul(date_str, '.') - date_str;
98
99                 /* MM[.SS] */
100                 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
101                                         &ptm->tm_min,
102                                         &end) >= 1) {
103                 } else
104                 /* HHMM[.SS] */
105                 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
106                                         &ptm->tm_hour,
107                                         &ptm->tm_min,
108                                         &end) >= 2) {
109                 } else
110                 /* ddHHMM[.SS] */
111                 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
112                                         &ptm->tm_mday,
113                                         &ptm->tm_hour,
114                                         &ptm->tm_min,
115                                         &end) >= 3) {
116                 } else
117                 /* mmddHHMM[.SS] */
118                 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
119                                         &ptm->tm_mon,
120                                         &ptm->tm_mday,
121                                         &ptm->tm_hour,
122                                         &ptm->tm_min,
123                                         &end) >= 4) {
124                         /* Adjust month from 1-12 to 0-11 */
125                         ptm->tm_mon -= 1;
126                 } else
127                 /* yymmddHHMM[.SS] */
128                 if (len == 10 && sscanf(date_str, "%2u%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                         /* Adjust month from 1-12 to 0-11 */
136                         ptm->tm_mon -= 1;
137                         if ((int)cur_year >= 50) { /* >= 1950 */
138                                 /* Adjust year: */
139                                 /* 1. Put it in the current century */
140                                 ptm->tm_year += (cur_year / 100) * 100;
141                                 /* 2. If too far in the past, +100 years */
142                                 if (ptm->tm_year < cur_year - 50)
143                                         ptm->tm_year += 100;
144                                 /* 3. If too far in the future, -100 years */
145                                 if (ptm->tm_year > cur_year + 50)
146                                         ptm->tm_year -= 100;
147                         }
148                 } else
149                 /* ccyymmddHHMM[.SS] */
150                 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
151                                         &ptm->tm_year,
152                                         &ptm->tm_mon,
153                                         &ptm->tm_mday,
154                                         &ptm->tm_hour,
155                                         &ptm->tm_min,
156                                         &end) >= 5) {
157                         ptm->tm_year -= 1900; /* Adjust years */
158                         ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
159                 } else {
160                         bb_error_msg_and_die(bb_msg_invalid_date, date_str);
161                 }
162                 if (end == '.') {
163                         /* xxx.SS */
164                         if (sscanf(strchr(date_str, '.') + 1, "%u%c",
165                                         &ptm->tm_sec, &end) == 1)
166                                 end = '\0';
167                         /* else end != NUL and we error out */
168                 }
169         }
170         if (end != '\0') {
171                 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
172         }
173 }
174
175 time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
176 {
177         time_t t = mktime(ptm);
178         if (t == (time_t) -1L) {
179                 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
180         }
181         return t;
182 }
183
184 #if ENABLE_MONOTONIC_SYSCALL
185
186 #include <sys/syscall.h>
187 /* Old glibc (< 2.3.4) does not provide this constant. We use syscall
188  * directly so this definition is safe. */
189 #ifndef CLOCK_MONOTONIC
190 #define CLOCK_MONOTONIC 1
191 #endif
192
193 /* libc has incredibly messy way of doing this,
194  * typically requiring -lrt. We just skip all this mess */
195 static void get_mono(struct timespec *ts)
196 {
197         if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
198                 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
199 }
200 unsigned long long FAST_FUNC monotonic_ns(void)
201 {
202         struct timespec ts;
203         get_mono(&ts);
204         return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
205 }
206 unsigned long long FAST_FUNC monotonic_us(void)
207 {
208         struct timespec ts;
209         get_mono(&ts);
210         return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
211 }
212 unsigned long long FAST_FUNC monotonic_ms(void)
213 {
214         struct timespec ts;
215         get_mono(&ts);
216         return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
217 }
218 unsigned FAST_FUNC monotonic_sec(void)
219 {
220         struct timespec ts;
221         get_mono(&ts);
222         return ts.tv_sec;
223 }
224
225 #else
226
227 unsigned long long FAST_FUNC monotonic_ns(void)
228 {
229         struct timeval tv;
230         gettimeofday(&tv, NULL);
231         return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
232 }
233 unsigned long long FAST_FUNC monotonic_us(void)
234 {
235         struct timeval tv;
236         gettimeofday(&tv, NULL);
237         return tv.tv_sec * 1000000ULL + tv.tv_usec;
238 }
239 unsigned long long FAST_FUNC monotonic_ms(void)
240 {
241         struct timeval tv;
242         gettimeofday(&tv, NULL);
243         return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
244 }
245 unsigned FAST_FUNC monotonic_sec(void)
246 {
247         return time(NULL);
248 }
249
250 #endif