LocalTime arithmetic

2

In an attempt to calculate the time difference between two cities I enter:

LocalTime[Entity["City", {"Wheeling", "WestVirginia", "UnitedStates"}]] - LocalTime[Entity["City", {"LosAngeles", "California", "UnitedStates"}]]

The Output is

Quantity[-0.673196, "Seconds"]

This should be 3hrs

JEM

Posted 2020-10-22T13:59:48.400

Reputation: 991

Answers

1

It's easy to find the numeric time offset in hours between two cities with TimeZoneOffset.

city1 = Entity["City", {"Wheeling", "WestVirginia", "UnitedStates"}];
city2 = Entity["City", {"LosAngeles", "California", "UnitedStates"}];
TimeZoneOffset[city1, city2]
(* 3. *)

Use Quantity to get the offset in hours.

Quantity[TimeZoneOffset[city1, city2], "Hours"]
(* Quantity[3., "Hours"] *)

Local time difference

Subtracting local times doesn't work is because the two times are the same time but in different time zones. The fractional second result happens because LocalTime for Los Angeles runs a short time after LocalTime for Wheeling. The difference is the fraction of a second that's needed to get the time for Wheeling.

Demonstrate that the two times are the same by using LocalTime to get the Los Angeles time for the current Wheeling time. The time in Wheeling is the same as the time in Los Angeles (only the time zones are different), so the difference is zero.

tWheeling = LocalTime[city1];
tLosAngeles = LocalTime[city2, tWheeling];
tWheeling - tLosAngeles
(* Quantity[0., "Seconds"] )*

creidhne

Posted 2020-10-22T13:59:48.400

Reputation: 2 997

The documentation states that LocalTime[] returns a DateObject. If you extract the list using Part: LocalTime[ Entity["City", {"Wheeling", "WestVirginia", "UnitedStates"}]][[1]] you get {2020, 10, 22, 14, 59, 39.1685818} and for Los Angeles you get {2020, 10, 22, 12, 1, 22.4556520}. The format here is {y,m,d,h,min,sec}. Notice the difference in the h slot. When you subtract the lists you get the actual time difference contained in the resulting list. Just seems awkward to me that the times would internally be treated the same. I should dust my relativity text and review simultaneity. – JEM – 2020-10-22T19:07:37.220

When you use part, you ignore the time zone information. The local times, with the time zones, are the same instant in time so the time difference is zero. The numeric values for hours, minutes and seconds, etc., are different, but that's because the time zones are different. If we convert Wheeling time and Los Angles time to a common time zone (GMT for example), we would see that all the numeric values are the same. – creidhne – 2020-10-22T20:11:05.700

0

You could try LocalTimeZone like so:

LocalTimeZone[
  Entity["City", {"Wheeling", "WestVirginia", "UnitedStates"}], Now, 
  "Offset"] - 
 LocalTimeZone[
  Entity["City", {"LosAngeles", "California", "UnitedStates"}], Now, 
  "Offset"]
(* Quantity[MixedMagnitude[{3, 0, 0}], 
 MixedUnit[{"Hours", "Minutes", "Seconds"}]] *)

Tim Laska

Posted 2020-10-22T13:59:48.400

Reputation: 12 026