Some code [Software]

posted by Helmut Homepage – Vienna, Austria, 2019-03-22 16:58  – Posting: # 20068
Views: 2,494

Hi Dr Anonymous :-D

» I agree, patience is the key :), I use R studio predominantly and have a GitHub account (need to be more active though).

So you are an advanced RUser!

» » ....This is the number of seconds since midnight, January 1, 1960…
»
» Woah, thanks for the explanation.

Welcome. Didn’t know it until yesterday either.

» […] I believed ISO8601 was majorly the standard time format

In many countries. In Austria it is not just a norm but legally binding. Rarely used in daily life.

» Thanks for working it out, I tried replicating this and this looks promising for me to try it out further.

I was curious myself. My latest code:

library(lubridate) # Makes job easier but has large footprint. See also:
# https://blog.revolutionanalytics.com/2009/06/converting-time-zones.html
# https://www.gormanalysis.com/blog/dates-and-times-in-r-without-losing-your-sanity/
# https://stackoverflow.com/questions/51236962/how-to-format-a-difftime-object-to-a-string-with-hhmmss
# For a list of abbreviated locations / time zones: OlsonNames()
# or https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
# I recommend to give the location; if applicable it will observe
# local daylight time switches. In case you are lost, retrieve the location
# R is running in: Sys.timezone()

lct       <- Sys.getlocale("LC_TIME") # Save original locale.
invisible(Sys.setlocale("LC_TIME", "C"))
my_tz     <- 'Europe/Vienna'
my_time   <- now(tzone=my_tz)
IBM_tz    <- 'US/Eastern'
IBM_time  <- with_tz(my_time, IBM_tz)
your_tz   <- 'Asia/Kolkata'
your_time <- with_tz(my_time, your_tz)
NZ_tz     <- 'Pacific/Auckland'
NZ_time   <- with_tz(my_time, NZ_tz)
tz        <- c(my_tz, IBM_tz, your_tz, NZ_tz)
dt        <- c(as.character(my_time), as.character(IBM_time),
               as.character(your_time), as.character(NZ_time))
UTC.dt    <- offset <- rep(NA, length(dt))
for (j in seq_along(dt)) { # Cannot be vectorized!
  dt[j]     <- format(as.POSIXct(dt[j], tz=tz[j]), tz=tz[j], usetz=TRUE)
  UTC.dt[j] <- format(as.POSIXct(dt[j], tz=tz[j]), tz='UTC', usetz=TRUE)
  x         <- as.numeric(difftime(dt[j], UTC.dt[j], units="hours"))
  offset[j] <- sprintf("%+02d:%02d", x %/% 1, (x - x %/% 1)*60)
}
df <- data.frame(location=tz, local.datetime=dt, offset, UTC.datetime=UTC.dt,
                 loc.SAS.datetime=format(as.POSIXct(strptime(dt,
                  format='%F %T')), format='%d%b%y:%H:%M:%S'),
                 UTC.SAS.datetime=format(as.POSIXct(strptime(UTC.dt,
                   format='%F %T')), format='%d%b%y:%H:%M:%S'),
                 row.names=NULL, stringsAsFactors=FALSE)
invisible(Sys.setlocale("LC_TIME", lct)) # Restore original locale.
print(df, row.names=FALSE)               # Here we are.


Which gave on my machine:

         location           local.datetime offset            UTC.datetime loc.SAS.datetime UTC.SAS.datetime
    Europe/Vienna  2019-03-22 18:32:57 CET  +1:00 2019-03-22 17:32:57 UTC 22Mar19:18:32:57 22Mar19:17:32:57
       US/Eastern  2019-03-22 13:32:57 EDT  -4:00 2019-03-22 17:32:57 UTC 22Mar19:13:32:57 22Mar19:17:32:57
     Asia/Kolkata  2019-03-22 23:02:57 IST  +5:30 2019-03-22 17:32:57 UTC 22Mar19:23:02:57 22Mar19:17:32:57
 Pacific/Auckland 2019-03-23 06:32:57 NZDT +13:00 2019-03-22 17:32:57 UTC 23Mar19:06:32:57 22Mar19:17:32:57


Note the different local times: Poughkeepsie changed EST ↑ EDT on March 10th, Austria will change CET ↑ CEST on March 31th, and New Zealand will change NZDT ↓ NZT on April 7th.
See this funny story.

Cheers,
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes

Complete thread:

Activity
 Admin contact
20,259 posts in 4,265 threads, 1,401 registered users;
online 30 (1 registered, 29 guests [including 13 identified bots]).
Forum time (Europe/Vienna): 10:17 CET

Science is simply common sense at its best that is,
rigidly accurate in observation, and
merciless to fallacy in logic.    Thomas Henry Huxley

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5