The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,225 other subscribers

ESXi ash/dash/busybox shell getting current timestamp in UTC ISO8601 format without colons or dashes

Posted by jpluimers on 2022/01/17

On VMware ESXi, with the  Busybox ash/dash shell, I wanted to get the current UTC timestamp in ISO 8601 format without dashes (-) or especially colons (:) and plus-signs (+) you have to back-slash escape colons or double quote parameters, which is often can be a pain).

This is why we can’t have good things: Getting the UTC 8610 timestamp was far less easy than I hoped for.

First of all, Busybox only allows for a precision of seconds, not milliseconds, and the specification format needs better documentation as per [Wayback] embedded linux – How to get ISO8601 seconds format from “date” in busybox? – Stack Overflow:

The main and very subtle difference in one of my tries that I happened to miss:

# date -I 'seconds'
date: invalid date 'seconds'

Was off by a hair. It needed to remove the space:

# date -I'seconds'
2017-07-09T17:29:54-0400

Now busybox date is happy.

This gets you an ISO 8601 timestamp that is in the local time zone (in this case UTC-4 hours used in parts of the Americas and the Caribbean), which ahead of UTC will introduce a plus sign (+) that I don’t want.

I wanted the ISO 8601 UTC time zone designation, which is Z (and in some RFCs +00:00, +0000, -00:00 or -0000).

The Busybox date command allows to specify an -u or --utc option to make a timestamp UTC as per help:

# date --help
BusyBox v1.29.3 (2019-05-21 15:22:06 PDT) multi-call binary.

Usage: date [OPTIONS] [+FMT] [TIME]

Display time (using +FMT), or set time

    [-s,--set] TIME Set time to TIME
    -u,--utc    Work in UTC (don't convert to local time)
    -R,--rfc-2822   Output RFC-2822 compliant date string
    -I[SPEC]    Output ISO-8601 compliant date string
            SPEC='date' (default) for date only,
            'hours', 'minutes', or 'seconds' for date and
            time to the indicated precision
    -r,--reference FILE Display last modification time of FILE
    -d,--date TIME  Display TIME, not 'now'
    -D FMT      Use FMT for -d TIME conversion

Recognized TIME formats:
    hh:mm[:ss]
    [YYYY.]MM.DD-hh:mm[:ss]
    YYYY-MM-DD hh:mm[:ss]
    [[[[[YY]YY]MM]DD]hh]mm[.ss]
    'date TIME' form accepts MMDDhhmm[[YY]YY][.ss] instead

Well, this is in for a surprise, as instead of Z, (or even the time zones +00:00, +0000, -00:00 or -0000), the UTC time zone designator is used: clearly not ISO 8601 compliant.

# date -I'seconds'
2021-05-09T14:23:38+0000
# date --utc -I'seconds'
2021-05-09T14:23:38UTC

Like VMware ESXi console: viewing all VMs, suspending and waking them up: part 1, sed and regular expressions come to the recue again after a try at regex101.com/r/lzeXIP

# date --utc -I'seconds'
2021-05-09T15:45:04UTC
# date --utc -I'seconds' | sed -n -E -e "s/^([[:digit:]]{4})\-([[:digit:]]{2})\-([[:digit:]]{2}T[[:digit:]]{2})\:([[:digit:]]{2})\:([[:digit:]]{2})UTC$/\1\2\3\4\5Z/p"
20210509T154504Z

It relies heavily on grouping, but works pretty nicely.

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: