Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Convert UTC time to local time using strftime and zsh/datetime



On Feb 20,  9:02pm, Kurtis Rader wrote:
}
} TZ=UTC strftime -r "%Y-%m-%d %H:%M:%S +0000" "2015-02-11 16:42:30 +0000"
} 
} Which should yield 1423672950 as the seconds since the UNIX epoch.

Except it doesn't unless you have already done "export TZ", because
strftime is not one of the special builtins that really do alter the
process environment when you prefix them with a variable assignment.
So in my environment where TZ starts out not set, this still returns
the epoch in my local time zone, e.g. 1423701750.

This is guaranteed to fix the timezone issue:

    (){ local -x TZ=UTC;
	strftime -r "%Y-%m-%d %H:%M:%S %z" "2015-02-11 16:42:30 +0000" }

but note that %z to match the timezone is a glibc extension to strptime
and may not work everywhere.

Also note that strptime is not defined to perform timezone conversions;
even though glibc strptime can be told that a timezone is present, it
parses it and then throws it away.  In TJ's original formulation

} > > strftime -r "%Y-%m-%d %H:%M:%S +0000" "2015-02-11 16:42:30 +0000"

the +0000 is just a string which is matched but ignored.  Either way,
this is why the TZ=UTC environment setting is needed.

The next question is whether the strftime builtin should implicitly
perform "local -x TZ" to make Kurtis' example do the expected thing.



Messages sorted by: Reverse Date, Date, Thread, Author