1

I want to convert number of days to date with time:

 > 15525.1+as.Date("1970-01-01")
 [1] "2012-07-04" ## correct but no time

I tried this:

> apollo.fmt <- "%B %d, %Y, %H:%M:%S"
> as.POSIXct((15525.1+as.Date("1970-01-01")), format=apollo.fmt, tz="UTC")
[1] "2012-07-04 04:24:00 CEST"

but as you see the results provide in CEST. But I need it it in UTC. Any hints on this?

krlzlx
  • 5,752
  • 14
  • 47
  • 55
bic ton
  • 1,284
  • 1
  • 9
  • 16

2 Answers2

0

For the original conversion, refer to this question: Converting numeric time to datetime POSIXct format in R and these pages: Date-times in R , Date-time conversions and Converting excel dates (number) to R date-time object. Bascially, it depends on your data source, the time origin for that data sources (Excel, Apache etc.) and the units. For example, you may have the total time elapsed in seconds, minutes, hours or days since the time origin for your data source which will be different for Excel or Apache. Once you have this information, you can use strptime or origin arguments and convert to R date-time objects.

If you are only concerned with changing the timezone, you can use attr:

> u <- Sys.time()
> u
[1] "2017-12-21 09:01:35 EST"
> attr(u, "tzone") <- "UTC"
> u
[1] "2017-12-21 14:01:35 UTC"

You may want to check up on the valid timezones for your machine though. A good way to get a time-zone that works with your machine would be googleway::google_timezone. To get the coordinates for your location (or the location from where you're importing data), you can either look those up online or use ggmap::geocode() - useful if converting time stamps in data from different time zones.

Gautam
  • 2,597
  • 1
  • 28
  • 51
0

I think the problem is as.POSIXct doesn't change anything if the time is already POSIXct, so the tz option has no effect.

Use attr as explained here