I often find myself seeking a concise solution to check if I am running artificial intelligence stock trading algorithms at the right time for various exchanges around the world. Most of the users of our analyses want to see an analysis in their specific local time zone, so our scheduling servers and programs often have various checks, such as is it 3:59 PM in New York or 11:29 AM in Tokyo?
Here are two simple functions, to_tz() and now_tz(), with no package dependencies beyond Base R to address this specific area.
*What time is it in New York? *now_tz(tz = “America/New_York”) # “2016-12-29 19:02:14.84 EST”
How about Tokyo time for this after-hours New York trade at 2016-12-29 19:02:14.85 EST? to_tz(tz_to = “Asia/Tokyo”, date_time = “2016-12-29 19:02:14.85”, tz_from = “America/New_York” ) # “2016-12-30 09:02:14.83 JST”
######################################################################
#
# Functions: to_tz.R and now_tz.R
# Author: Stephen McDaniel
# Organization: PowerTrip Analytics
# Date created: 2016/12/29
# Mod date: N/A
# Copyright: (C) 2016 by PowerTrip Analytics
# License: The MIT License at http://opensource.org/licenses/MIT
#
########## About this code
#
# 1) Obtain current DateTime for a specified
# timezone in POSIXct format (simply specify time zone)
# 2) Convert a POSIXct vector or string vector in standard
# POSIXct format from one time zone to another
# Hint: use command OlsonNames()
# to find your desired time zone name(s)
#
# Required packages: none (base R packages)
# Sanity checked on: Windows 10 and Ubuntu 16.04 with R 3.3.1
#
########## Input
#
# tz_to = desired output time zone as character string
# date_time = (only for to_tz function), a POSIXct vector
# tz_from = originating time zone when passing in a string DateTime
#
########## Sample returned values
#
# Returns a POSIXct vector with desired tz_to time zone
# Returns a NULL if invalid data is passed in
#
# For example, prints as "2016-12-29 19:02:14.84 EST"
#
######################################################################
to_tz <- function(tz_to = "UTC",
date_time = Sys.time(),
tz_from = "UTC") {
if(is.character(date_time)) {
date_time <- as.POSIXct(date_time, tz = tz_from)
}
if (!is(date_time, "POSIXct")) {
warning("date_time should be a POSIXct object or string convertable to a POSIXct object")
return(NULL)
} else return(
as.POSIXct(
format(date_time, tz = tz_to, usetz=TRUE),
tz = tz_to
)
)
}
now_tz <- function(tz_to = "UTC") {
return(to_tz(tz_to = tz_to, date_time = Sys.time()))
}
# A few examples
now_tz() # defaults to UTC
# "2016-12-30 00:02:14.83 UTC"
now_tz(tz_to = "America/New_York")
# "2016-12-29 19:02:14.84 EST"
nyc <- now_tz(tz_to = "America/New_York")
to_tz(tz_to = "US/Pacific", date_time = nyc)
# "2016-12-29 16:02:14.84 PST"
to_tz(tz_to = "US/Pacific", date_time = as.numeric(nyc))
# Failure, will only accept POSIXct or character DateTimes
# Returns NULL and a warning message
to_tz(tz_to = "Asia/Tokyo",
date_time = "2016-12-29 19:02:14.85",
tz_from = "America/New_York"
)
# Success, character DateTimes are accepted with
# an assumed origin of 1970-01-01
# "2016-12-30 04:02:14.83 JST"
# Look up your time zone, 589 values in Windows, 606 in Ubuntu
# [437] "Europe/Copenhagen" "Europe/Dublin"
# OlsonNames() # run manually