mirror of
https://github.com/ericonr/util-linux.git
synced 2024-04-21 09:12:35 -05:00
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>
This commit is contained in:
parent
9fd0a7a96c
commit
4111bb3ab5
@ -55,18 +55,26 @@ typedef uint64_t nsec_t;
|
|||||||
int parse_timestamp(const char *t, usec_t *usec);
|
int parse_timestamp(const char *t, usec_t *usec);
|
||||||
int get_gmtoff(const struct tm *tp);
|
int get_gmtoff(const struct tm *tp);
|
||||||
|
|
||||||
/* flags for strxxx_iso() functions */
|
/* flags and masks for strxxx_iso() functions */
|
||||||
enum {
|
enum {
|
||||||
ISO_8601_DATE = (1 << 1),
|
ISO_DATE = (1 << 0),
|
||||||
ISO_8601_TIME = (1 << 2),
|
ISO_TIME = (1 << 1),
|
||||||
ISO_8601_DOTUSEC = (1 << 3),
|
ISO_TIMEZONE = (1 << 2),
|
||||||
ISO_8601_COMMAUSEC = (1 << 4),
|
ISO_DOTUSEC = (1 << 3),
|
||||||
ISO_8601_TIMEZONE = (1 << 5),
|
ISO_COMMAUSEC = (1 << 4),
|
||||||
ISO_8601_SPACE = (1 << 6),
|
ISO_T = (1 << 5),
|
||||||
ISO_8601_GMTIME = (1 << 7)
|
ISO_GMTIME = (1 << 6),
|
||||||
|
ISO_TIMESTAMP = ISO_DATE | ISO_TIME | ISO_TIMEZONE,
|
||||||
|
ISO_TIMESTAMP_T = ISO_TIMESTAMP | ISO_T,
|
||||||
|
ISO_TIMESTAMP_DOT = ISO_TIMESTAMP | ISO_DOTUSEC,
|
||||||
|
ISO_TIMESTAMP_DOT_T = ISO_TIMESTAMP_DOT | ISO_T,
|
||||||
|
ISO_TIMESTAMP_COMMA = ISO_TIMESTAMP | ISO_COMMAUSEC,
|
||||||
|
ISO_TIMESTAMP_COMMA_T = ISO_TIMESTAMP_COMMA | ISO_T,
|
||||||
|
ISO_TIMESTAMP_COMMA_G = ISO_TIMESTAMP_COMMA | ISO_GMTIME,
|
||||||
|
ISO_TIMESTAMP_COMMA_GT = ISO_TIMESTAMP_COMMA_G | ISO_T
|
||||||
};
|
};
|
||||||
|
|
||||||
#define ISO_8601_BUFSIZ 42
|
#define ISO_BUFSIZ 42
|
||||||
|
|
||||||
int strtimeval_iso(struct timeval *tv, int flags, char *buf, size_t bufsz);
|
int strtimeval_iso(struct timeval *tv, int flags, char *buf, size_t bufsz);
|
||||||
int strtm_iso(struct tm *tm, int flags, char *buf, size_t bufsz);
|
int strtm_iso(struct tm *tm, int flags, char *buf, size_t bufsz);
|
||||||
|
@ -405,7 +405,7 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
|
|||||||
char *p = buf;
|
char *p = buf;
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
if (flags & ISO_8601_DATE) {
|
if (flags & ISO_DATE) {
|
||||||
len = snprintf(p, bufsz, "%4d-%.2d-%.2d", tm->tm_year + 1900,
|
len = snprintf(p, bufsz, "%4d-%.2d-%.2d", tm->tm_year + 1900,
|
||||||
tm->tm_mon + 1, tm->tm_mday);
|
tm->tm_mon + 1, tm->tm_mday);
|
||||||
if (len < 0 || (size_t) len > bufsz)
|
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;
|
p += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((flags & ISO_8601_DATE) && (flags & ISO_8601_TIME)) {
|
if ((flags & ISO_DATE) && (flags & ISO_TIME)) {
|
||||||
if (bufsz < 1)
|
if (bufsz < 1)
|
||||||
return -1;
|
return -1;
|
||||||
*p++ = (flags & ISO_8601_SPACE) ? ' ' : 'T';
|
*p++ = (flags & ISO_T) ? 'T' : ' ';
|
||||||
bufsz--;
|
bufsz--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & ISO_8601_TIME) {
|
if (flags & ISO_TIME) {
|
||||||
len = snprintf(p, bufsz, "%02d:%02d:%02d", tm->tm_hour,
|
len = snprintf(p, bufsz, "%02d:%02d:%02d", tm->tm_hour,
|
||||||
tm->tm_min, tm->tm_sec);
|
tm->tm_min, tm->tm_sec);
|
||||||
if (len < 0 || (size_t) len > bufsz)
|
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;
|
p += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & ISO_8601_DOTUSEC) {
|
if (flags & ISO_DOTUSEC) {
|
||||||
len = snprintf(p, bufsz, ".%06ld", (long) usec);
|
len = snprintf(p, bufsz, ".%06ld", (long) usec);
|
||||||
if (len < 0 || (size_t) len > bufsz)
|
if (len < 0 || (size_t) len > bufsz)
|
||||||
return -1;
|
return -1;
|
||||||
bufsz -= len;
|
bufsz -= len;
|
||||||
p += len;
|
p += len;
|
||||||
|
|
||||||
} else if (flags & ISO_8601_COMMAUSEC) {
|
} else if (flags & ISO_COMMAUSEC) {
|
||||||
len = snprintf(p, bufsz, ",%06ld", (long) usec);
|
len = snprintf(p, bufsz, ",%06ld", (long) usec);
|
||||||
if (len < 0 || (size_t) len > bufsz)
|
if (len < 0 || (size_t) len > bufsz)
|
||||||
return -1;
|
return -1;
|
||||||
@ -445,7 +445,7 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
|
|||||||
p += len;
|
p += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & ISO_8601_TIMEZONE) {
|
if (flags & ISO_TIMEZONE) {
|
||||||
int tmin = get_gmtoff(tm) / 60;
|
int tmin = get_gmtoff(tm) / 60;
|
||||||
int zhour = tmin / 60;
|
int zhour = tmin / 60;
|
||||||
int zmin = abs(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;
|
struct tm tm;
|
||||||
|
|
||||||
if (flags & ISO_8601_GMTIME)
|
if (flags & ISO_GMTIME)
|
||||||
tm = *gmtime(&tv->tv_sec);
|
tm = *gmtime(&tv->tv_sec);
|
||||||
else
|
else
|
||||||
tm = *localtime(&tv->tv_sec);
|
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;
|
struct tm tm;
|
||||||
|
|
||||||
if (flags & ISO_8601_GMTIME)
|
if (flags & ISO_GMTIME)
|
||||||
tm = *gmtime(t);
|
tm = *gmtime(t);
|
||||||
else
|
else
|
||||||
tm = *localtime(t);
|
tm = *localtime(t);
|
||||||
@ -548,7 +548,7 @@ time_t timegm(struct tm *tm)
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
struct timeval tv = { 0 };
|
struct timeval tv = { 0 };
|
||||||
char buf[ISO_8601_BUFSIZ];
|
char buf[ISO_BUFSIZ];
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
fprintf(stderr, "usage: %s <time> [<usec>]\n", argv[0]);
|
fprintf(stderr, "usage: %s <time> [<usec>]\n", argv[0]);
|
||||||
@ -559,19 +559,17 @@ int main(int argc, char *argv[])
|
|||||||
if (argc == 3)
|
if (argc == 3)
|
||||||
tv.tv_usec = strtos64_or_err(argv[2], "failed to parse <usec>");
|
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);
|
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);
|
printf("Time: '%s'\n", buf);
|
||||||
|
|
||||||
strtimeval_iso(&tv, ISO_8601_DATE | ISO_8601_TIME | ISO_8601_COMMAUSEC,
|
strtimeval_iso(&tv, ISO_DATE | ISO_TIME | ISO_COMMAUSEC | ISO_T,
|
||||||
buf, sizeof(buf));
|
buf, sizeof(buf));
|
||||||
printf("Full: '%s'\n", buf);
|
printf("Full: '%s'\n", buf);
|
||||||
|
|
||||||
strtimeval_iso(&tv, ISO_8601_DATE | ISO_8601_TIME | ISO_8601_DOTUSEC |
|
strtimeval_iso(&tv, ISO_TIMESTAMP_DOT, buf, sizeof(buf));
|
||||||
ISO_8601_TIMEZONE | ISO_8601_SPACE,
|
|
||||||
buf, sizeof(buf));
|
|
||||||
printf("Zone: '%s'\n", buf);
|
printf("Zone: '%s'\n", buf);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
@ -349,7 +349,7 @@ static int time_formatter(int fmt, char *dst, size_t dlen, time_t *when)
|
|||||||
ret = rtrim_whitespace((unsigned char *) dst);
|
ret = rtrim_whitespace((unsigned char *) dst);
|
||||||
break;
|
break;
|
||||||
case LAST_TIMEFTM_ISO8601:
|
case LAST_TIMEFTM_ISO8601:
|
||||||
ret = strtime_iso(when, ISO_8601_DATE|ISO_8601_TIME|ISO_8601_TIMEZONE, dst, dlen);
|
ret = strtime_iso(when, ISO_TIMESTAMP_T, dst, dlen);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
abort();
|
abort();
|
||||||
|
@ -333,11 +333,10 @@ static char *make_time(int mode, time_t time)
|
|||||||
buf, sizeof(buf));
|
buf, sizeof(buf));
|
||||||
break;
|
break;
|
||||||
case TIME_ISO:
|
case TIME_ISO:
|
||||||
rc = strtime_iso(&time, ISO_8601_DATE|ISO_8601_TIME|ISO_8601_TIMEZONE,
|
rc = strtime_iso(&time, ISO_TIMESTAMP_T, buf, sizeof(buf));
|
||||||
buf, sizeof(buf));
|
|
||||||
break;
|
break;
|
||||||
case TIME_ISO_SHORT:
|
case TIME_ISO_SHORT:
|
||||||
rc = strtime_iso(&time, ISO_8601_DATE, buf, sizeof(buf));
|
rc = strtime_iso(&time, ISO_DATE, buf, sizeof(buf));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
errx(EXIT_FAILURE, _("unsupported time type"));
|
errx(EXIT_FAILURE, _("unsupported time type"));
|
||||||
|
@ -102,9 +102,7 @@ static void print_utline(struct utmpx *ut, FILE *out)
|
|||||||
tv.tv_sec = ut->ut_tv.tv_sec;
|
tv.tv_sec = ut->ut_tv.tv_sec;
|
||||||
tv.tv_usec = ut->ut_tv.tv_usec;
|
tv.tv_usec = ut->ut_tv.tv_usec;
|
||||||
|
|
||||||
if (strtimeval_iso(&tv,
|
if (strtimeval_iso(&tv, ISO_TIMESTAMP_COMMA_GT, time_string,
|
||||||
ISO_8601_DATE | ISO_8601_TIME | ISO_8601_COMMAUSEC |
|
|
||||||
ISO_8601_TIMEZONE | ISO_8601_GMTIME, time_string,
|
|
||||||
sizeof(time_string)) != 0)
|
sizeof(time_string)) != 0)
|
||||||
return;
|
return;
|
||||||
cleanse(ut->ut_id);
|
cleanse(ut->ut_id);
|
||||||
|
@ -224,17 +224,11 @@ static void fill_table_row(struct libscols_table *tb, char const *const uuid)
|
|||||||
}
|
}
|
||||||
if (variant == UUID_VARIANT_DCE && type == 1) {
|
if (variant == UUID_VARIANT_DCE && type == 1) {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
char date_buf[ISO_8601_BUFSIZ + 4];
|
char date_buf[ISO_BUFSIZ];
|
||||||
|
|
||||||
uuid_time(buf, &tv);
|
uuid_time(buf, &tv);
|
||||||
strtimeval_iso(&tv,
|
strtimeval_iso(&tv, ISO_TIMESTAMP_COMMA,
|
||||||
ISO_8601_DATE |
|
date_buf, sizeof(date_buf));
|
||||||
ISO_8601_TIME |
|
|
||||||
ISO_8601_COMMAUSEC |
|
|
||||||
ISO_8601_TIMEZONE |
|
|
||||||
ISO_8601_SPACE,
|
|
||||||
date_buf,
|
|
||||||
sizeof(date_buf));
|
|
||||||
str = xstrdup(date_buf);
|
str = xstrdup(date_buf);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -840,9 +840,7 @@ static char *iso_8601_time(struct dmesg_control *ctl, struct dmesg_record *rec,
|
|||||||
.tv_usec = rec->tv.tv_usec
|
.tv_usec = rec->tv.tv_usec
|
||||||
};
|
};
|
||||||
|
|
||||||
if (strtimeval_iso(&tv, ISO_8601_DATE|ISO_8601_TIME|ISO_8601_COMMAUSEC|
|
if (strtimeval_iso(&tv, ISO_TIMESTAMP_COMMA_T, buf, bufsz) != 0)
|
||||||
ISO_8601_TIMEZONE,
|
|
||||||
buf, bufsz) != 0)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
|
@ -554,11 +554,9 @@ set_hardware_clock_exact(const struct hwclock_control *ctl,
|
|||||||
static int
|
static int
|
||||||
display_time(struct timeval hwctime)
|
display_time(struct timeval hwctime)
|
||||||
{
|
{
|
||||||
char buf[ISO_8601_BUFSIZ];
|
char buf[ISO_BUFSIZ];
|
||||||
|
|
||||||
if (strtimeval_iso(&hwctime, ISO_8601_DATE|ISO_8601_TIME|ISO_8601_DOTUSEC|
|
if (strtimeval_iso(&hwctime, ISO_TIMESTAMP_DOT, buf, sizeof(buf))) {
|
||||||
ISO_8601_TIMEZONE|ISO_8601_SPACE,
|
|
||||||
buf, sizeof(buf))) {
|
|
||||||
warnx(_("iso-8601 format overflow"));
|
warnx(_("iso-8601 format overflow"));
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
@ -451,7 +451,7 @@ static char *make_time(int mode, time_t time)
|
|||||||
strtime_short(&time, &now, 0, buf, sizeof(buf));
|
strtime_short(&time, &now, 0, buf, sizeof(buf));
|
||||||
break;
|
break;
|
||||||
case TIME_ISO:
|
case TIME_ISO:
|
||||||
strtime_iso(&time, ISO_8601_DATE|ISO_8601_TIME|ISO_8601_TIMEZONE, buf, sizeof(buf));
|
strtime_iso(&time, ISO_TIMESTAMP_T, buf, sizeof(buf));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
errx(EXIT_FAILURE, _("unsupported time type"));
|
errx(EXIT_FAILURE, _("unsupported time type"));
|
||||||
|
@ -223,7 +223,7 @@ static int rfkill_event(void)
|
|||||||
{
|
{
|
||||||
struct rfkill_event event;
|
struct rfkill_event event;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
char date_buf[ISO_8601_BUFSIZ];
|
char date_buf[ISO_BUFSIZ];
|
||||||
struct pollfd p;
|
struct pollfd p;
|
||||||
int fd, n;
|
int fd, n;
|
||||||
|
|
||||||
@ -253,12 +253,8 @@ static int rfkill_event(void)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
strtimeval_iso(&tv,
|
strtimeval_iso(&tv, ISO_TIMESTAMP_COMMA, date_buf,
|
||||||
ISO_8601_DATE |
|
sizeof(date_buf));
|
||||||
ISO_8601_TIME |
|
|
||||||
ISO_8601_COMMAUSEC |
|
|
||||||
ISO_8601_TIMEZONE |
|
|
||||||
ISO_8601_SPACE, date_buf, sizeof(date_buf));
|
|
||||||
printf("%s: idx %u type %u op %u soft %u hard %u\n",
|
printf("%s: idx %u type %u op %u soft %u hard %u\n",
|
||||||
date_buf,
|
date_buf,
|
||||||
event.idx, event.type, event.op, event.soft, event.hard);
|
event.idx, event.type, event.op, event.soft, event.hard);
|
||||||
|
@ -472,9 +472,7 @@ static void do_io(struct script_control *ctl)
|
|||||||
|
|
||||||
|
|
||||||
if (ctl->typescriptfp) {
|
if (ctl->typescriptfp) {
|
||||||
strtime_iso(&tvec, ISO_8601_DATE | ISO_8601_TIME |
|
strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
|
||||||
ISO_8601_TIMEZONE | ISO_8601_SPACE,
|
|
||||||
buf, sizeof(buf));
|
|
||||||
fprintf(ctl->typescriptfp, _("Script started on %s\n"), buf);
|
fprintf(ctl->typescriptfp, _("Script started on %s\n"), buf);
|
||||||
}
|
}
|
||||||
gettime_monotonic(&ctl->oldtime);
|
gettime_monotonic(&ctl->oldtime);
|
||||||
@ -546,9 +544,7 @@ static void do_io(struct script_control *ctl)
|
|||||||
|
|
||||||
if (ctl->typescriptfp) {
|
if (ctl->typescriptfp) {
|
||||||
tvec = script_time((time_t *)NULL);
|
tvec = script_time((time_t *)NULL);
|
||||||
strtime_iso(&tvec, ISO_8601_DATE | ISO_8601_TIME |
|
strtime_iso(&tvec, ISO_TIMESTAMP, buf, sizeof(buf));
|
||||||
ISO_8601_TIMEZONE | ISO_8601_SPACE,
|
|
||||||
buf, sizeof(buf));
|
|
||||||
fprintf(ctl->typescriptfp, _("\nScript done on %s\n"), buf);
|
fprintf(ctl->typescriptfp, _("\nScript done on %s\n"), buf);
|
||||||
}
|
}
|
||||||
done(ctl);
|
done(ctl);
|
||||||
|
Loading…
Reference in New Issue
Block a user