0

so when I use

>strptime(paste(as.character(as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),origin="1970-01-01")),
+                substr(1438293919327731275,11,13),sep="."),"%Y-%m-%d %H:%M:%OS")
[1] "2015-07-30 17:05:19"

I want to know what happens to substr(14-19)? anyway we could report them say when we are using this number for a plot?

Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • ...you didn't write code for `substr(x, 14, 19)`. Not sure why you expect it to be there. – Joshua Ulrich Aug 06 '15 at 04:58
  • @JoshuaUlrich I get something rather absurd `>as.POSIXct(as.numeric(substr(1438293919327731275,14,19)),origin="1970-01-01") [1] "1970-01-09 05:06:40 CST"` – Mona Jalal Aug 06 '15 at 05:01
  • Why is that absurd? The output of that `substr` command yields `"731275"`. `"1970-01-09 05:06:40"` is 731,275 seconds from 1970-01-01 00:00:00. It's also not clear to me what you're actually trying to do. How a datetime is displayed on a plot will depend on the plotting function/device used and how you add the datetimes to the plot, neither of which are described in your question. – Joshua Ulrich Aug 06 '15 at 05:08
  • 1
    `as.POSIXct` is counting the amount of seconds since the origin date. That answer is the correct measurement (731,275 seconds since Jan 1, 1970). – Pierre L Aug 06 '15 at 05:10
  • what about the things that are smaller than a second ? like a milli second? how can you capture those with POSIXct @PierreLafortune – Mona Jalal Aug 06 '15 at 05:13
  • 3
    They are there. They're just not printed by default. – Joshua Ulrich Aug 06 '15 at 05:14
  • 3
    There are many questions on this already with great answers. http://stackoverflow.com/search?q=%5Br%5D+milliseconds – Pierre L Aug 06 '15 at 05:15

2 Answers2

2

To answer your query, yes, R plots can deal with POSIXct times that include sub-second fractions:

x <- 1:2
y <- as.POSIXct(c(1438293919.3277, 1438293919.4682),origin="1970-01-01")
#[1] "2015-07-31 08:05:19 EST" "2015-07-31 08:05:19 EST"
plot(y ~ x, yaxt="n")
axis.POSIXct(
  2,
  at=as.POSIXct(axTicks(2),origin="1970-01-01"), format="%OS2",
  las=2, cex.axis=0.7
)

enter image description here

thelatemail
  • 91,185
  • 12
  • 128
  • 188
2

As Joshua Ulrich already said, the fractional part of the seconds is not lost, but not displayed by default. If you want to see the fractional part, set the option "digit.secs" to the desired number of decimal places. But at least on my system I couldn't get more than 7 decimal places:

#============================================================
# With option "digit.secs"=NULL

options(digits.secs=NULL)

t1 <- as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),
                 origin="1970-01-01")

t2 <- strptime(paste(as.character(t),
                     substr(1438293919327731275,11,13),
                     sep="."),
               "%Y-%m-%d %H:%M:%OS")

t3 <- strptime(paste(as.character(t),
                     substr(1438293919327731275,11,19),
                     sep="."),
               "%Y-%m-%d %H:%M:%OS")

t1
t2
t3

t2-t1
t3-t1

#==============================================================
# With option "digit.secs"=9

options(digits.secs=9)

t1 <- as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),
                 origin="1970-01-01")

t2 <- strptime(paste(as.character(t),
                     substr(1438293919327731275,11,13),
                     sep="."),
               "%Y-%m-%d %H:%M:%OS")

t3 <- strptime(paste(as.character(t),
                     substr(1438293919327731275,11,19),
                     sep="."),
               "%Y-%m-%d %H:%M:%OS")

t1
t2
t3

t2-t1
t3-t1

Result:

> #============================================================
> # With option "digit.secs"=NULL
> 
> options(digits.secs=NULL)

> t1 <- as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),
+                  origin="1970-01-01")

> t2 <- strptime(paste(as.character(t),
+                      substr(1438293919327731275,11,13),
+                      sep="."),
+                "% ..." ... [TRUNCATED] 

> t3 <- strptime(paste(as.character(t),
+                      substr(1438293919327731275,11,19),
+                      sep="."),
+                "% ..." ... [TRUNCATED] 

> t1
[1] "2015-07-31 00:05:19 CEST"

> t2
[1] "2015-07-31 00:05:19 CEST"

> t3
[1] "2015-07-31 00:05:19 CEST"

> t2-t1
Time difference of 0.3269999 secs

> t3-t1
Time difference of 0.3277311 secs

> #==============================================================
> # With option "digit.secs"=9
> 
> options(digits.secs=9)

> t1 <- as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),
+                  origin="1970-01-01")

> t2 <- strptime(paste(as.character(t),
+                      substr(1438293919327731275,11,13),
+                      sep="."),
+                "% ..." ... [TRUNCATED] 

> t3 <- strptime(paste(as.character(t),
+                      substr(1438293919327731275,11,19),
+                      sep="."),
+                "% ..." ... [TRUNCATED] 

> t1
[1] "2015-07-31 00:05:19 CEST"

> t2
[1] "2015-07-31 00:05:19.327 CEST"

> t3
[1] "2015-07-31 00:05:19.327731 CEST"

> t2-t1
Time difference of 0.3269999 secs

> t3-t1
Time difference of 0.3277311 secs
mra68
  • 2,960
  • 1
  • 10
  • 17