1

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?

Rodrigo
  • 4,706
  • 6
  • 51
  • 94
  • 1
    One possibility from the documentation of `?as.POSIXlt`: "If format is specified, remember that some of the format specifications are locale-specific, and you may need to set the LC_TIME category appropriately via Sys.setlocale. This most often affects the use of %b, %B (month names) and %p (AM/PM)." – joran May 10 '18 at 22:16
  • In *my* versions of R (3.3 and 3.4), your `as.POSIXlt` call produces several warnings. They might have something to do with the differences. (The second positional argument is different between the two functions.) – r2evans May 10 '18 at 22:16

1 Answers1

2

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.

MKR
  • 19,739
  • 4
  • 23
  • 33