From m|ch@e|ch|r|co4 @end|ng |rom gm@||@com  Tue Aug 13 11:16:24 2019
From: m|ch@e|ch|r|co4 @end|ng |rom gm@||@com (Michael Chirico)
Date: Tue, 13 Aug 2019 17:16:24 +0800
Subject: [Rd] Why no tz argument for format.POSIXlt?
Message-ID: <CAPRVBcyK=Rv9=y+SgaJN57Rgf1OAwk359spXhPyLccMm6OJOtA@mail.gmail.com>

Was a bit surprised to see

oldtz = Sys.getenv('TZ')
Sys.setenv(TZ = 'Asia/Jakarta')
format(Sys.time())
# [1] "2019-08-13 16:05:03"
format(Sys.time(), tz = 'UTC') # all is well
# [1] "2019-08-13 09:05:03"
format(trunc(Sys.time(), 'hours')) # correctly truncated in local time
# [1] "2019-08-13 16:00:00"
format(trunc(Sys.time(), 'hours'), tz = 'UTC') # no effect!
[1] "2019-08-13 16:00:00"
Sys.setenv(TZ = oldtz)

The reason for the discrepancy is that trunc.POSIXt returns a POSIXlt
object (not POSIXct), whereas Sys.time() is POSIXct. And while
format.POSIXct has a tz argument, format.POSIXlt does not:

names(formals(format.POSIXct))
# [1] "x"      "format" "tz"     "usetz"  "..."
names(formals(format.POSIXlt))
# [1] "x"      "format" "usetz"  "..."

Is there any reason not to accept a tz argument for format.POSIXlt? It's
quite convenient to be able to specify an output timezone format on the fly
with format.POSIXct; in the case at hand, I'm trying to force UTC time on
input. format(as.POSIXct(x), tz = 'UTC') seems to work just fine, is there
a reason why this wouldn't be done internally?

Michael Chirico

	[[alternative HTML version deleted]]



[Rd] Why no tz argument for format.POSIXlt?
Michael Chirico