Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Convert UTC time to local time using strftime and zsh/datetime
- X-seq: zsh-users 19883
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh-Users List <zsh-users@xxxxxxx>
- Subject: Re: Convert UTC time to local time using strftime and zsh/datetime
- Date: Sat, 21 Feb 2015 10:23:05 -0800
- In-reply-to: <CABx2=D9e8KPMd9fxL5UY5BoVOdnNyMx-d2tR3v_4Gmd7V5m7DQ@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CADjGqHvaOkB-T_qGHDvt9=8TzSGiR8-FptXCX8VNFyfPkmJN-g@mail.gmail.com> <CABx2=D9e8KPMd9fxL5UY5BoVOdnNyMx-d2tR3v_4Gmd7V5m7DQ@mail.gmail.com>
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