]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/timeutils.c
2 First set of functions in this file are part of systemd, and were
3 copied to util-linux at August 2013.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with util-linux; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/sysinfo.h>
31 #include "timeutils.h"
33 #define WHITESPACE " \t\n\r"
35 #define streq(a,b) (strcmp((a),(b)) == 0)
37 static int parse_sec(const char *t
, usec_t
*usec
)
43 { "seconds", USEC_PER_SEC
},
44 { "second", USEC_PER_SEC
},
45 { "sec", USEC_PER_SEC
},
46 { "s", USEC_PER_SEC
},
47 { "minutes", USEC_PER_MINUTE
},
48 { "minute", USEC_PER_MINUTE
},
49 { "min", USEC_PER_MINUTE
},
50 { "months", USEC_PER_MONTH
},
51 { "month", USEC_PER_MONTH
},
52 { "msec", USEC_PER_MSEC
},
53 { "ms", USEC_PER_MSEC
},
54 { "m", USEC_PER_MINUTE
},
55 { "hours", USEC_PER_HOUR
},
56 { "hour", USEC_PER_HOUR
},
57 { "hr", USEC_PER_HOUR
},
58 { "h", USEC_PER_HOUR
},
59 { "days", USEC_PER_DAY
},
60 { "day", USEC_PER_DAY
},
61 { "d", USEC_PER_DAY
},
62 { "weeks", USEC_PER_WEEK
},
63 { "week", USEC_PER_WEEK
},
64 { "w", USEC_PER_WEEK
},
65 { "years", USEC_PER_YEAR
},
66 { "year", USEC_PER_YEAR
},
67 { "y", USEC_PER_YEAR
},
70 { "", USEC_PER_SEC
}, /* default is sec */
75 int something
= FALSE
;
86 p
+= strspn(p
, WHITESPACE
);
96 l
= strtoll(p
, &e
, 10);
108 z
= strtoll(b
, &e
, 10);
123 e
+= strspn(e
, WHITESPACE
);
125 for (i
= 0; i
< ARRAY_SIZE(table
); i
++)
126 if (startswith(e
, table
[i
].suffix
)) {
127 usec_t k
= (usec_t
) z
* table
[i
].usec
;
132 r
+= (usec_t
) l
*table
[i
].usec
+ k
;
133 p
= e
+ strlen(table
[i
].suffix
);
139 if (i
>= ARRAY_SIZE(table
))
149 int parse_timestamp(const char *t
, usec_t
*usec
)
151 static const struct {
174 usec_t plus
= 0, minus
= 0, ret
;
181 * 2012-09-22 16:34:22
182 * 2012-09-22 16:34 (seconds will be set to 0)
183 * 2012-09-22 (time will be set to 00:00:00)
184 * 16:34:22 (date will be set to today)
185 * 16:34 (date will be set to today, seconds to 0)
187 * yesterday (time is set to 00:00:00)
188 * today (time is set to 00:00:00)
189 * tomorrow (time is set to 00:00:00)
199 localtime_r(&x
, &tm
);
205 else if (streq(t
, "today")) {
206 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
209 } else if (streq(t
, "yesterday")) {
211 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
214 } else if (streq(t
, "tomorrow")) {
216 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
219 } else if (t
[0] == '+') {
221 r
= parse_sec(t
+ 1, &plus
);
226 } else if (t
[0] == '-') {
228 r
= parse_sec(t
+ 1, &minus
);
234 } else if (endswith(t
, " ago")) {
237 z
= strndup(t
, strlen(t
) - 4);
241 r
= parse_sec(z
, &minus
);
249 for (i
= 0; i
< ARRAY_SIZE(day_nr
); i
++) {
252 if (!startswith_no_case(t
, day_nr
[i
].name
))
255 skip
= strlen(day_nr
[i
].name
);
259 weekday
= day_nr
[i
].nr
;
265 k
= strptime(t
, "%y-%m-%d %H:%M:%S", &tm
);
270 k
= strptime(t
, "%Y-%m-%d %H:%M:%S", &tm
);
275 k
= strptime(t
, "%y-%m-%d %H:%M", &tm
);
282 k
= strptime(t
, "%Y-%m-%d %H:%M", &tm
);
289 k
= strptime(t
, "%y-%m-%d", &tm
);
291 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
296 k
= strptime(t
, "%Y-%m-%d", &tm
);
298 tm
.tm_sec
= tm
.tm_min
= tm
.tm_hour
= 0;
303 k
= strptime(t
, "%H:%M:%S", &tm
);
308 k
= strptime(t
, "%H:%M", &tm
);
315 k
= strptime(t
, "%Y%m%d%H%M%S", &tm
);
328 if (weekday
>= 0 && tm
.tm_wday
!= weekday
)
331 ret
= (usec_t
) x
*USEC_PER_SEC
;