blob: 45ae6f3a7e9ddbc58a65227954900bc9a0b0e9a9 [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 */
Denys Vlasenko73b71f32009-07-18 03:40:35 +020018
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020019 /* HH:MM */
Denys Vlasenko73b71f32009-07-18 03:40:35 +020020 if (sscanf(date_str, "%u:%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010021 &ptm->tm_hour,
22 &ptm->tm_min,
Denys Vlasenko73b71f32009-07-18 03:40:35 +020023 &end) >= 2) {
24 /* no adjustments needed */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020025 } else
26 /* mm.dd-HH:MM */
27 if (sscanf(date_str, "%u.%u-%u:%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010028 &ptm->tm_mon, &ptm->tm_mday,
29 &ptm->tm_hour, &ptm->tm_min,
Denys Vlasenko73b71f32009-07-18 03:40:35 +020030 &end) >= 4) {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020031 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010032 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020033 } else
34 /* yyyy.mm.dd-HH:MM */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010035 if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
36 &ptm->tm_mon, &ptm->tm_mday,
37 &ptm->tm_hour, &ptm->tm_min,
Denys Vlasenko73b71f32009-07-18 03:40:35 +020038 &end) >= 5) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010039 ptm->tm_year -= 1900; /* Adjust years */
40 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020041 } else
42 /* yyyy-mm-dd HH:MM */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010043 if (sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
44 &ptm->tm_mon, &ptm->tm_mday,
45 &ptm->tm_hour, &ptm->tm_min,
Denys Vlasenko73b71f32009-07-18 03:40:35 +020046 &end) >= 5) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010047 ptm->tm_year -= 1900; /* Adjust years */
48 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020049//TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes)
Denys Vlasenko73b71f32009-07-18 03:40:35 +020050 } else {
51 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
52 }
53 if (end == ':') {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020054 /* xxx:SS */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010055 if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
Denys Vlasenko73b71f32009-07-18 03:40:35 +020056 end = '\0';
57 /* else end != NUL and we error out */
58 }
59 } else {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020060 /* Googled the following on an old date manpage:
61 *
62 * The canonical representation for setting the date/time is:
63 * cc Century (either 19 or 20)
64 * yy Year in abbreviated form (e.g. 89, 06)
65 * mm Numeric month, a number from 1 to 12
66 * dd Day, a number from 1 to 31
67 * HH Hour, a number from 0 to 23
68 * MM Minutes, a number from 0 to 59
Denys Vlasenko4b624d02009-09-02 11:49:25 +020069 * .SS Seconds, a number from 0 to 61 (with leap seconds)
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020070 * Everything but the minutes is optional
71 *
72 * This coincides with the format of "touch -t TIME"
73 */
74 int len = strchrnul(date_str, '.') - date_str;
75
76 /* MM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +010077 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010078 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020079 &end) >= 1) {
80 } else
81 /* HHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +010082 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010083 &ptm->tm_hour,
84 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020085 &end) >= 2) {
86 } else
87 /* ddHHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +010088 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010089 &ptm->tm_mday,
90 &ptm->tm_hour,
91 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020092 &end) >= 3) {
93 } else
94 /* mmddHHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +010095 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010096 &ptm->tm_mon,
97 &ptm->tm_mday,
98 &ptm->tm_hour,
99 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200100 &end) >= 4) {
101 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100102 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200103 } else
104 /* yymmddHHMM[.SS] */
105 if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100106 &ptm->tm_year,
107 &ptm->tm_mon,
108 &ptm->tm_mday,
109 &ptm->tm_hour,
110 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200111 &end) >= 5) {
112 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100113 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200114 } else
Denys Vlasenko4b624d02009-09-02 11:49:25 +0200115 /* ccyymmddHHMM[.SS] */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200116 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100117 &ptm->tm_year,
118 &ptm->tm_mon,
119 &ptm->tm_mday,
120 &ptm->tm_hour,
121 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200122 &end) >= 5) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100123 ptm->tm_year -= 1900; /* Adjust years */
124 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200125 } else {
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200126 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200127 }
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200128 if (end == '.') {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200129 /* xxx.SS */
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200130 if (sscanf(strchr(date_str, '.') + 1, "%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100131 &ptm->tm_sec, &end) == 1)
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200132 end = '\0';
133 /* else end != NUL and we error out */
134 }
135 }
136 if (end != '\0') {
137 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
138 }
139}
140
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100141time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200142{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100143 time_t t = mktime(ptm);
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200144 if (t == (time_t) -1L) {
145 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
146 }
147 return t;
148}
149
Denis Vlasenko459be352007-06-17 19:09:05 +0000150#if ENABLE_MONOTONIC_SYSCALL
Denis Vlasenko459be352007-06-17 19:09:05 +0000151
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000152#include <sys/syscall.h>
Denis Vlasenko09c0a742008-06-07 23:43:43 +0000153/* Old glibc (< 2.3.4) does not provide this constant. We use syscall
154 * directly so this definition is safe. */
155#ifndef CLOCK_MONOTONIC
156#define CLOCK_MONOTONIC 1
157#endif
158
Denis Vlasenko459be352007-06-17 19:09:05 +0000159/* libc has incredibly messy way of doing this,
160 * typically requiring -lrt. We just skip all this mess */
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000161static void get_mono(struct timespec *ts)
162{
163 if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
164 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
165}
166unsigned long long FAST_FUNC monotonic_ns(void)
167{
168 struct timespec ts;
169 get_mono(&ts);
170 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
171}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000172unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000173{
174 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000175 get_mono(&ts);
Denis Vlasenko459be352007-06-17 19:09:05 +0000176 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
177}
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100178unsigned long long FAST_FUNC monotonic_ms(void)
179{
180 struct timespec ts;
181 get_mono(&ts);
182 return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
183}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000184unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000185{
186 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000187 get_mono(&ts);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000188 return ts.tv_sec;
189}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000190
Denis Vlasenko459be352007-06-17 19:09:05 +0000191#else
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000192
193unsigned long long FAST_FUNC monotonic_ns(void)
194{
195 struct timeval tv;
196 gettimeofday(&tv, NULL);
197 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
198}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000199unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000200{
201 struct timeval tv;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000202 gettimeofday(&tv, NULL);
Mike Frysingerebd27aa2007-06-18 07:12:31 +0000203 return tv.tv_sec * 1000000ULL + tv.tv_usec;
Denis Vlasenko459be352007-06-17 19:09:05 +0000204}
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100205unsigned long long FAST_FUNC monotonic_ms(void)
206{
207 struct timeval tv;
208 gettimeofday(&tv, NULL);
209 return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
210}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000211unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000212{
213 return time(NULL);
214}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000215
Denis Vlasenko459be352007-06-17 19:09:05 +0000216#endif