blob: 5cd04268cfc45212dad32f951dff492603c82ce5 [file] [log] [blame]
Denis Vlasenko459be352007-06-17 19:09:05 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00005 * Copyright (C) 2007 Denys Vlasenko
Denis Vlasenko459be352007-06-17 19:09:05 +00006 *
7 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
8 */
Denis Vlasenko459be352007-06-17 19:09:05 +00009#include "libbb.h"
10
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010011void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
Denys Vlasenko73b71f32009-07-18 03:40:35 +020012{
13 char end = '\0';
14 const char *last_colon = strrchr(date_str, ':');
15
16 if (last_colon != NULL) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010017 /* Parse input and assign appropriately to ptm */
Alexander Shishkin85dbf192010-03-22 18:22:33 +010018#if ENABLE_DESKTOP
19 const char *endp;
20#endif
Denys Vlasenko73b71f32009-07-18 03:40:35 +020021
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020022 /* HH:MM */
Denys Vlasenko73b71f32009-07-18 03:40:35 +020023 if (sscanf(date_str, "%u:%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010024 &ptm->tm_hour,
25 &ptm->tm_min,
Denys Vlasenko73b71f32009-07-18 03:40:35 +020026 &end) >= 2) {
27 /* no adjustments needed */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020028 } else
29 /* mm.dd-HH:MM */
30 if (sscanf(date_str, "%u.%u-%u:%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010031 &ptm->tm_mon, &ptm->tm_mday,
32 &ptm->tm_hour, &ptm->tm_min,
Denys Vlasenko73b71f32009-07-18 03:40:35 +020033 &end) >= 4) {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020034 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010035 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020036 } else
37 /* yyyy.mm.dd-HH:MM */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010038 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,
Denys Vlasenko73b71f32009-07-18 03:40:35 +020041 &end) >= 5) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010042 ptm->tm_year -= 1900; /* Adjust years */
43 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020044 } else
45 /* yyyy-mm-dd HH:MM */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010046 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,
Denys Vlasenko73b71f32009-07-18 03:40:35 +020049 &end) >= 5) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010050 ptm->tm_year -= 1900; /* Adjust years */
51 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Alexander Shishkin85dbf192010-03-22 18:22:33 +010052 } 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
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020061//TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes)
Alexander Shishkin85dbf192010-03-22 18:22:33 +010062 {
Denys Vlasenko73b71f32009-07-18 03:40:35 +020063 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
64 }
65 if (end == ':') {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020066 /* xxx:SS */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010067 if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
Denys Vlasenko73b71f32009-07-18 03:40:35 +020068 end = '\0';
69 /* else end != NUL and we error out */
70 }
71 } else {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020072 /* 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
Denys Vlasenko4b624d02009-09-02 11:49:25 +020081 * .SS Seconds, a number from 0 to 61 (with leap seconds)
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020082 * 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] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +010089 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010090 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020091 &end) >= 1) {
92 } else
93 /* HHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +010094 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010095 &ptm->tm_hour,
96 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020097 &end) >= 2) {
98 } else
99 /* ddHHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100100 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100101 &ptm->tm_mday,
102 &ptm->tm_hour,
103 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200104 &end) >= 3) {
105 } else
106 /* mmddHHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100107 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100108 &ptm->tm_mon,
109 &ptm->tm_mday,
110 &ptm->tm_hour,
111 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200112 &end) >= 4) {
113 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100114 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200115 } else
116 /* yymmddHHMM[.SS] */
117 if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100118 &ptm->tm_year,
119 &ptm->tm_mon,
120 &ptm->tm_mday,
121 &ptm->tm_hour,
122 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200123 &end) >= 5) {
124 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100125 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200126 } else
Denys Vlasenko4b624d02009-09-02 11:49:25 +0200127 /* ccyymmddHHMM[.SS] */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200128 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100129 &ptm->tm_year,
130 &ptm->tm_mon,
131 &ptm->tm_mday,
132 &ptm->tm_hour,
133 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200134 &end) >= 5) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100135 ptm->tm_year -= 1900; /* Adjust years */
136 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200137 } else {
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200138 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200139 }
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200140 if (end == '.') {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200141 /* xxx.SS */
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200142 if (sscanf(strchr(date_str, '.') + 1, "%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100143 &ptm->tm_sec, &end) == 1)
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200144 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
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100153time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200154{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100155 time_t t = mktime(ptm);
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200156 if (t == (time_t) -1L) {
157 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
158 }
159 return t;
160}
161
Denis Vlasenko459be352007-06-17 19:09:05 +0000162#if ENABLE_MONOTONIC_SYSCALL
Denis Vlasenko459be352007-06-17 19:09:05 +0000163
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000164#include <sys/syscall.h>
Denis Vlasenko09c0a742008-06-07 23:43:43 +0000165/* 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
Denis Vlasenko459be352007-06-17 19:09:05 +0000171/* libc has incredibly messy way of doing this,
172 * typically requiring -lrt. We just skip all this mess */
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000173static 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}
178unsigned 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}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000184unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000185{
186 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000187 get_mono(&ts);
Denis Vlasenko459be352007-06-17 19:09:05 +0000188 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
189}
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100190unsigned 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}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000196unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000197{
198 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000199 get_mono(&ts);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000200 return ts.tv_sec;
201}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000202
Denis Vlasenko459be352007-06-17 19:09:05 +0000203#else
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000204
205unsigned 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}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000211unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000212{
213 struct timeval tv;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000214 gettimeofday(&tv, NULL);
Mike Frysingerebd27aa2007-06-18 07:12:31 +0000215 return tv.tv_sec * 1000000ULL + tv.tv_usec;
Denis Vlasenko459be352007-06-17 19:09:05 +0000216}
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100217unsigned 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}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000223unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000224{
225 return time(NULL);
226}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000227
Denis Vlasenko459be352007-06-17 19:09:05 +0000228#endif