Reputation: 5119
Consider the following R console output.
> Sys.getlocale()
[1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C"
> dh <- '2018-05-08 07:42:34 PM'
> as.POSIXlt(dh,'%Y-%m-%d %I:%M:%S %p')
[1] "2018-05-08 07:42:34"
> strptime(dh,'%Y-%m-%d %I:%M:%S %p')
[1] "2018-05-08 19:42:34 -03"
If both formats are identical, why doesn't as.POSIXlt
recognize the 12-hour format properly? dh
gives a time at night (7 PM), but the function is returning a time in the morning! as.POSIXct
gives the same error.
What am I missing here?
Upvotes: 1
Views: 35
Reputation: 20085
The actual mistake seems to be in the line;
as.POSIXlt(dh, '%Y-%m-%d %I:%M:%S %p')
It should be as:
as.POSIXlt(dh, format= '%Y-%m-%d %I:%M:%S %p')
#[1] "2018-05-08 19:42:34 BST"
The 2nd argument for strptime
is format
. Hence, strptime(dh,'%Y-%m-%d %I:%M:%S %p')
works fine as it considers 2nd argument as format
.
But 2nd argument for as.POSIXlt
is tz
. Hence, the text provided to be considered as format
is taken up as tz
and default format
is used. That was resulting in unexpected value.
Upvotes: 2