summaryrefslogtreecommitdiffstats
path: root/lib/timeutils.c
diff options
context:
space:
mode:
authorJ William Piggott2017-10-15 02:37:11 +0200
committerJ William Piggott2017-11-10 22:34:55 +0100
commit4111bb3ab5f406ee381a3807385af59fe33b28f3 (patch)
treed03f85ab061bfade57c03c716e5226048fcdc8af /lib/timeutils.c
parentlib/timeutils: add get_gmtoff() (diff)
downloadkernel-qcow2-util-linux-4111bb3ab5f406ee381a3807385af59fe33b28f3.tar.gz
kernel-qcow2-util-linux-4111bb3ab5f406ee381a3807385af59fe33b28f3.tar.xz
kernel-qcow2-util-linux-4111bb3ab5f406ee381a3807385af59fe33b28f3.zip
lib/timeutils: add common ISO timestamp masks
* Start the ISO format flags at bit 0 instead of bit 1. * Remove unnecessary _8601 from ISO format flag names to avoid line wrapping and to ease readability. * ISO timestamps have date-time-timzone in common, so move the TIMEZONE flag to bit 2 causing all timestamp masks to have the first three bits set and the last four bits as timestamp 'options'. * Change the 'SPACE' flag to a 'T' flag, because it makes the code and comments more concise. * Add common ISO timestamp masks. * Implement the ISO timestamp masks in all applicable code using the strxxx_iso() functions. Signed-off-by: J William Piggott <elseifthen@gmx.com>
Diffstat (limited to 'lib/timeutils.c')
-rw-r--r--lib/timeutils.c32
1 files changed, 15 insertions, 17 deletions
diff --git a/lib/timeutils.c b/lib/timeutils.c
index adc255c33..fdaa2d4a9 100644
--- a/lib/timeutils.c
+++ b/lib/timeutils.c
@@ -405,7 +405,7 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
char *p = buf;
int len;
- if (flags & ISO_8601_DATE) {
+ if (flags & ISO_DATE) {
len = snprintf(p, bufsz, "%4d-%.2d-%.2d", tm->tm_year + 1900,
tm->tm_mon + 1, tm->tm_mday);
if (len < 0 || (size_t) len > bufsz)
@@ -414,14 +414,14 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
p += len;
}
- if ((flags & ISO_8601_DATE) && (flags & ISO_8601_TIME)) {
+ if ((flags & ISO_DATE) && (flags & ISO_TIME)) {
if (bufsz < 1)
return -1;
- *p++ = (flags & ISO_8601_SPACE) ? ' ' : 'T';
+ *p++ = (flags & ISO_T) ? 'T' : ' ';
bufsz--;
}
- if (flags & ISO_8601_TIME) {
+ if (flags & ISO_TIME) {
len = snprintf(p, bufsz, "%02d:%02d:%02d", tm->tm_hour,
tm->tm_min, tm->tm_sec);
if (len < 0 || (size_t) len > bufsz)
@@ -430,14 +430,14 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
p += len;
}
- if (flags & ISO_8601_DOTUSEC) {
+ if (flags & ISO_DOTUSEC) {
len = snprintf(p, bufsz, ".%06ld", (long) usec);
if (len < 0 || (size_t) len > bufsz)
return -1;
bufsz -= len;
p += len;
- } else if (flags & ISO_8601_COMMAUSEC) {
+ } else if (flags & ISO_COMMAUSEC) {
len = snprintf(p, bufsz, ",%06ld", (long) usec);
if (len < 0 || (size_t) len > bufsz)
return -1;
@@ -445,7 +445,7 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
p += len;
}
- if (flags & ISO_8601_TIMEZONE) {
+ if (flags & ISO_TIMEZONE) {
int tmin = get_gmtoff(tm) / 60;
int zhour = tmin / 60;
int zmin = abs(tmin % 60);
@@ -461,7 +461,7 @@ int strtimeval_iso(struct timeval *tv, int flags, char *buf, size_t bufsz)
{
struct tm tm;
- if (flags & ISO_8601_GMTIME)
+ if (flags & ISO_GMTIME)
tm = *gmtime(&tv->tv_sec);
else
tm = *localtime(&tv->tv_sec);
@@ -479,7 +479,7 @@ int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz)
{
struct tm tm;
- if (flags & ISO_8601_GMTIME)
+ if (flags & ISO_GMTIME)
tm = *gmtime(t);
else
tm = *localtime(t);
@@ -548,7 +548,7 @@ time_t timegm(struct tm *tm)
int main(int argc, char *argv[])
{
struct timeval tv = { 0 };
- char buf[ISO_8601_BUFSIZ];
+ char buf[ISO_BUFSIZ];
if (argc < 2) {
fprintf(stderr, "usage: %s <time> [<usec>]\n", argv[0]);
@@ -559,19 +559,17 @@ int main(int argc, char *argv[])
if (argc == 3)
tv.tv_usec = strtos64_or_err(argv[2], "failed to parse <usec>");
- strtimeval_iso(&tv, ISO_8601_DATE, buf, sizeof(buf));
+ strtimeval_iso(&tv, ISO_DATE, buf, sizeof(buf));
printf("Date: '%s'\n", buf);
- strtimeval_iso(&tv, ISO_8601_TIME, buf, sizeof(buf));
+ strtimeval_iso(&tv, ISO_TIME, buf, sizeof(buf));
printf("Time: '%s'\n", buf);
- strtimeval_iso(&tv, ISO_8601_DATE | ISO_8601_TIME | ISO_8601_COMMAUSEC,
- buf, sizeof(buf));
+ strtimeval_iso(&tv, ISO_DATE | ISO_TIME | ISO_COMMAUSEC | ISO_T,
+ buf, sizeof(buf));
printf("Full: '%s'\n", buf);
- strtimeval_iso(&tv, ISO_8601_DATE | ISO_8601_TIME | ISO_8601_DOTUSEC |
- ISO_8601_TIMEZONE | ISO_8601_SPACE,
- buf, sizeof(buf));
+ strtimeval_iso(&tv, ISO_TIMESTAMP_DOT, buf, sizeof(buf));
printf("Zone: '%s'\n", buf);
return EXIT_SUCCESS;