In R, why do I get one millisecond difference between POSIXct and POSIXlt? -


this snippet

options(digits.secs=3) s<-"12:00:00.188" fmt<-"%i:%m:%os" print(strptime(s,fmt)) print(as.posixct(strptime(s,fmt))) 

gives textual output:

[1] "2017-09-12 00:00:00.188 cest" [1] "2017-09-12 00:00:00.187 cest" 

while expect above result being same. missing?

my session info:

print(sessioninfo()) 

gives:

r version 3.4.0 (2017-04-21) platform: x86_64-w64-mingw32/x64 (64-bit) running under: windows 7 x64 (build 7601) service pack 1  matrix products: default  locale: [1] lc_collate=italian_italy.1252  lc_ctype=italian_italy.1252    lc_monetary=italian_italy.1252 lc_numeric=c                   [5] lc_time=italian_italy.1252      attached base packages: [1] stats     graphics  grdevices utils     datasets  methods   base       loaded via namespace (and not attached): [1] compiler_3.4.0 tools_3.4.0    

same result in linux:

r version 3.3.3 (2017-03-06) platform: x86_64-pc-linux-gnu (64-bit) running under: debian gnu/linux 9 (stretch)  locale:  [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     [7] lc_paper=en_us.utf-8       lc_name=c                  lc_address=c               lc_telephone=c             lc_measurement=en_us.utf-8 lc_identification=c         attached base packages: [1] stats     graphics  grdevices utils     datasets  methods   base       loaded via namespace (and not attached): [1] tools_3.3.3 

edit (after roland's comment)

maybe wrong, seems me 0.002 not representable in floating point, still 0.002 there no difference between posixct , posixlt:

options(digits.secs=3) s<-"12:00:00.002" fmt<-"%i:%m:%os" print(strptime(s,fmt)) print(as.posixct(strptime(s,fmt))) 

gives:

[1] "2017-09-12 00:00:00.002 cest" [1] "2017-09-12 00:00:00.002 cest" 

you can read in docs datetime: https://stat.ethz.ch/r-manual/r-devel/library/base/html/datetimeclasses.html

in particular:

class "posixct" represents (signed) number of seconds since beginning of 1970 (in utc time zone) numeric vector.

strptime , other class posixlt store datetimes differently.

so there issues sub-second accuracy:

sub-second accuracy

classes "posixct" , "posixlt" able express fractions of second. (conversion of fractions between 2 forms may not exact, have better microsecond accuracy.)

so, you'll see posixlt , strptime print these accurately:

strptime(s,fmt) as.posixlt(strptime(s,fmt), format = "%y-%m-%d %h:%m:%os") 

but, because posixct calculations represent date number, can have inaccuracies due floating-point precision, leap seconds, etc.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -