blob: 30b760f71a863de068c963ba7d25b0921fd7c25c [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 Vlasenko73b71f32009-07-18 03:40:35 +020011void FAST_FUNC parse_datestr(const char *date_str, struct tm *tm_time)
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 tm_time */
18
19 if (sscanf(date_str, "%u:%u%c",
20 &tm_time->tm_hour,
21 &tm_time->tm_min,
22 &end) >= 2) {
23 /* no adjustments needed */
24 } else if (sscanf(date_str, "%u.%u-%u:%u%c",
25 &tm_time->tm_mon, &tm_time->tm_mday,
26 &tm_time->tm_hour, &tm_time->tm_min,
27 &end) >= 4) {
28 /* Adjust dates from 1-12 to 0-11 */
29 tm_time->tm_mon -= 1;
30 } else if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &tm_time->tm_year,
31 &tm_time->tm_mon, &tm_time->tm_mday,
32 &tm_time->tm_hour, &tm_time->tm_min,
33 &end) >= 5) {
34 tm_time->tm_year -= 1900; /* Adjust years */
35 tm_time->tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
36 } else if (sscanf(date_str, "%u-%u-%u %u:%u%c", &tm_time->tm_year,
37 &tm_time->tm_mon, &tm_time->tm_mday,
38 &tm_time->tm_hour, &tm_time->tm_min,
39 &end) >= 5) {
40 tm_time->tm_year -= 1900; /* Adjust years */
41 tm_time->tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
42//TODO: coreutils 6.9 also accepts "YYYY-MM-DD HH" (no minutes)
43 } else {
44 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
45 }
46 if (end == ':') {
47 if (sscanf(last_colon + 1, "%u%c", &tm_time->tm_sec, &end) == 1)
48 end = '\0';
49 /* else end != NUL and we error out */
50 }
51 } else {
52 if (sscanf(date_str, "%2u%2u%2u%2u%u%c", &tm_time->tm_mon,
53 &tm_time->tm_mday, &tm_time->tm_hour, &tm_time->tm_min,
54 &tm_time->tm_year, &end) < 4)
55 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
56 /* correct for century - minor Y2K problem here? */
57 if (tm_time->tm_year >= 1900) {
58 tm_time->tm_year -= 1900;
59 }
60 /* adjust date */
61 tm_time->tm_mon -= 1;
62 if (end == '.') {
63 if (sscanf(strchr(date_str, '.') + 1, "%u%c",
64 &tm_time->tm_sec, &end) == 1)
65 end = '\0';
66 /* else end != NUL and we error out */
67 }
68 }
69 if (end != '\0') {
70 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
71 }
72}
73
Denis Vlasenko459be352007-06-17 19:09:05 +000074#if ENABLE_MONOTONIC_SYSCALL
Denis Vlasenko459be352007-06-17 19:09:05 +000075
Denis Vlasenkoce13b762008-06-29 02:25:53 +000076#include <sys/syscall.h>
Denis Vlasenko09c0a742008-06-07 23:43:43 +000077/* Old glibc (< 2.3.4) does not provide this constant. We use syscall
78 * directly so this definition is safe. */
79#ifndef CLOCK_MONOTONIC
80#define CLOCK_MONOTONIC 1
81#endif
82
Denis Vlasenko459be352007-06-17 19:09:05 +000083/* libc has incredibly messy way of doing this,
84 * typically requiring -lrt. We just skip all this mess */
Denis Vlasenkoce13b762008-06-29 02:25:53 +000085static void get_mono(struct timespec *ts)
86{
87 if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
88 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
89}
90unsigned long long FAST_FUNC monotonic_ns(void)
91{
92 struct timespec ts;
93 get_mono(&ts);
94 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
95}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000096unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +000097{
98 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +000099 get_mono(&ts);
Denis Vlasenko459be352007-06-17 19:09:05 +0000100 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
101}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000102unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000103{
104 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000105 get_mono(&ts);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000106 return ts.tv_sec;
107}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000108
Denis Vlasenko459be352007-06-17 19:09:05 +0000109#else
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000110
111unsigned long long FAST_FUNC monotonic_ns(void)
112{
113 struct timeval tv;
114 gettimeofday(&tv, NULL);
115 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
116}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000117unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000118{
119 struct timeval tv;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000120 gettimeofday(&tv, NULL);
Mike Frysingerebd27aa2007-06-18 07:12:31 +0000121 return tv.tv_sec * 1000000ULL + tv.tv_usec;
Denis Vlasenko459be352007-06-17 19:09:05 +0000122}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000123unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000124{
125 return time(NULL);
126}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000127
Denis Vlasenko459be352007-06-17 19:09:05 +0000128#endif