JuliaTime / TimeZones.jl

IANA time zone database access for the Julia programming language
Other
86 stars 52 forks source link

Constructing array of DateTimes from different time zones #322

Open jakewilliami opened 3 years ago

jakewilliami commented 3 years ago

I am trying to make an array of times from different time zones, similar to below:

example

But what is shown above is manually calculated based on ZonedDateTime(today(), tz"UTC"). I would really like to use the actual TimeZone functionality, like this:

tzs = Pair{String, ZonedDateTime}[
    "Lost Angeles" => ZonedDateTime(today(), TimeZone("America/Los_Angeles")),
    "Rio de Jeneiro" => ZonedDateTime(today(), TimeZone("Brazil/DeNoronha", TimeZones.Class(:LEGACY))),
    "Brussels/Potsdam" => ZonedDateTime(today(), TimeZone("Europe/Brussels")),
    "New Dehli" => ZonedDateTime(today(), TimeZone("Asia/Calcutta", TimeZones.Class(:LEGACY))),
    "Tokyo" => ZonedDateTime(today(), TimeZone("Japan", TimeZones.Class(:LEGACY))),
    "Auckland" => ZonedDateTime(today(), TimeZone("NZ", TimeZones.Class(:LEGACY)))
]

But today() does not do what I want. Can you think of a way to do this using TimeZone? FYI, to make the actual table, I increment the DateTimes by 1 hour.

omus commented 3 years ago

Sorry for the slow response. I started a new job and have been focused on that lately.

There are a couple functions that may be of use to you here: today(::TimeZone) and todayat(::Time, ::TimeZone). You definitely don't want to use today() as that is today for your local system and not the TimeZone you specified (see the docstring of today(::TimeZone) for a good example).

The way I would probably go about doing this is:

julia> using Dates, DataFrames, TimeZones

julia> zdt = todayat(Time(0), tz"UTC")
2021-04-07T00:00:00+00:00

julia> hours = Hour(0):Hour(1):Hour(23)
Hour(0):Hour(1):Hour(23)

julia> DataFrame(
           "UTC-time" => astimezone(zdt, tz"UTC") .+ hours,
           "Los Angeles" => astimezone(zdt, tz"America/Los_Angeles") .+ hours,
       )
24×2 DataFrame
 Row │ UTC-time                   Los Angeles
     │ ZonedDat…                  ZonedDateT…
─────┼──────────────────────────────────────────────────────
   1 │ 2021-04-07T00:00:00+00:00  2021-04-06T17:00:00-07:00
   2 │ 2021-04-07T01:00:00+00:00  2021-04-06T18:00:00-07:00
   3 │ 2021-04-07T02:00:00+00:00  2021-04-06T19:00:00-07:00
   4 │ 2021-04-07T03:00:00+00:00  2021-04-06T20:00:00-07:00
   5 │ 2021-04-07T04:00:00+00:00  2021-04-06T21:00:00-07:00
   6 │ 2021-04-07T05:00:00+00:00  2021-04-06T22:00:00-07:00
   7 │ 2021-04-07T06:00:00+00:00  2021-04-06T23:00:00-07:00
   8 │ 2021-04-07T07:00:00+00:00  2021-04-07T00:00:00-07:00
   9 │ 2021-04-07T08:00:00+00:00  2021-04-07T01:00:00-07:00
  10 │ 2021-04-07T09:00:00+00:00  2021-04-07T02:00:00-07:00
  11 │ 2021-04-07T10:00:00+00:00  2021-04-07T03:00:00-07:00
  12 │ 2021-04-07T11:00:00+00:00  2021-04-07T04:00:00-07:00
  13 │ 2021-04-07T12:00:00+00:00  2021-04-07T05:00:00-07:00
  14 │ 2021-04-07T13:00:00+00:00  2021-04-07T06:00:00-07:00
  15 │ 2021-04-07T14:00:00+00:00  2021-04-07T07:00:00-07:00
  16 │ 2021-04-07T15:00:00+00:00  2021-04-07T08:00:00-07:00
  17 │ 2021-04-07T16:00:00+00:00  2021-04-07T09:00:00-07:00
  18 │ 2021-04-07T17:00:00+00:00  2021-04-07T10:00:00-07:00
  19 │ 2021-04-07T18:00:00+00:00  2021-04-07T11:00:00-07:00
  20 │ 2021-04-07T19:00:00+00:00  2021-04-07T12:00:00-07:00
  21 │ 2021-04-07T20:00:00+00:00  2021-04-07T13:00:00-07:00
  22 │ 2021-04-07T21:00:00+00:00  2021-04-07T14:00:00-07:00
  23 │ 2021-04-07T22:00:00+00:00  2021-04-07T15:00:00-07:00
  24 │ 2021-04-07T23:00:00+00:00  2021-04-07T16:00:00-07:00

Take note that adding Hours will work safely over DST transitions. For example:

julia> zdt = ZonedDateTime(2021,3,14,tz"America/Los_Angeles")  # Spring DST
2021-03-14T00:00:00-08:00

julia> DataFrame(
           "UTC-time" => astimezone(zdt, tz"UTC") .+ hours,
           "Los Angeles" => astimezone(zdt, tz"America/Los_Angeles") .+ hours,
       )
24×2 DataFrame
 Row │ UTC-time                   Los Angeles
     │ ZonedDat…                  ZonedDateT…
─────┼──────────────────────────────────────────────────────
   1 │ 2021-03-14T08:00:00+00:00  2021-03-14T00:00:00-08:00
   2 │ 2021-03-14T09:00:00+00:00  2021-03-14T01:00:00-08:00
   3 │ 2021-03-14T10:00:00+00:00  2021-03-14T03:00:00-07:00
   4 │ 2021-03-14T11:00:00+00:00  2021-03-14T04:00:00-07:00
   5 │ 2021-03-14T12:00:00+00:00  2021-03-14T05:00:00-07:00
   6 │ 2021-03-14T13:00:00+00:00  2021-03-14T06:00:00-07:00
   7 │ 2021-03-14T14:00:00+00:00  2021-03-14T07:00:00-07:00
   8 │ 2021-03-14T15:00:00+00:00  2021-03-14T08:00:00-07:00
   9 │ 2021-03-14T16:00:00+00:00  2021-03-14T09:00:00-07:00
  10 │ 2021-03-14T17:00:00+00:00  2021-03-14T10:00:00-07:00
  11 │ 2021-03-14T18:00:00+00:00  2021-03-14T11:00:00-07:00
  12 │ 2021-03-14T19:00:00+00:00  2021-03-14T12:00:00-07:00
  13 │ 2021-03-14T20:00:00+00:00  2021-03-14T13:00:00-07:00
  14 │ 2021-03-14T21:00:00+00:00  2021-03-14T14:00:00-07:00
  15 │ 2021-03-14T22:00:00+00:00  2021-03-14T15:00:00-07:00
  16 │ 2021-03-14T23:00:00+00:00  2021-03-14T16:00:00-07:00
  17 │ 2021-03-15T00:00:00+00:00  2021-03-14T17:00:00-07:00
  18 │ 2021-03-15T01:00:00+00:00  2021-03-14T18:00:00-07:00
  19 │ 2021-03-15T02:00:00+00:00  2021-03-14T19:00:00-07:00
  20 │ 2021-03-15T03:00:00+00:00  2021-03-14T20:00:00-07:00
  21 │ 2021-03-15T04:00:00+00:00  2021-03-14T21:00:00-07:00
  22 │ 2021-03-15T05:00:00+00:00  2021-03-14T22:00:00-07:00
  23 │ 2021-03-15T06:00:00+00:00  2021-03-14T23:00:00-07:00
  24 │ 2021-03-15T07:00:00+00:00  2021-03-15T00:00:00-07:00

Let me know if that solves your problem.