RingDotNetfor Ring

Time zones

Two people in different cities cannot agree on "12 o'clock" without a zone. This lesson stores times in UTC, shows them in local time, handles daylight saving, and follows a flight across the ocean.

What you will learn#

The rule#

Note

Store and calculate in UTC. Convert to local time only when you show a time to a person.

UTC (coordinated universal time) is the same everywhere and has no daylight saving. A local time is UTC plus an offset that depends on the place and the date: New York is UTC−5 in winter and UTC−4 in summer. If you keep local times in a database you cannot even tell which of two events came first.

The program#

samples/UsingDotNet/tutorials/ch03_timezones.ring
# time zones - UTC, local time, daylight saving, a meeting for several cities and a flight
load "dotnet.ring"

using {
	"System"
}

func main

	oZones = new TimeZoneInfo()
	aCities = [["New York","Eastern Standard Time"],["London","GMT Standard Time"],["Cairo","Egypt Standard Time"],["Tokyo","Tokyo Standard Time"]]

	# 1. A meeting at 12:00 UTC. Store and compare times in UTC; convert to local time only to SHOW them.
	? "1. the meeting at 12:00 UTC on 1 July 2024:"
	oSummer = new DateTime(2024,7,1).AddHours(12)
	for aCity in aCities
		oLocal = localTime(oZones,oSummer,aCity[2])
		? "   " + padRight(aCity[1],10) + oLocal.Format("yyyy-MM-dd HH:mm") + "   (UTC" + offsetText(oZones,oSummer,aCity[2]) + ")"
	next

	# 2. The same meeting in winter: New York, London and Cairo are one hour different (daylight saving time), Tokyo does not have it
	? nl + "2. the same meeting at 12:00 UTC on 15 January 2024:"
	oWinter = new DateTime(2024,1,15).AddHours(12)
	for aCity in aCities
		oLocal = localTime(oZones,oWinter,aCity[2])
		? "   " + padRight(aCity[1],10) + oLocal.Format("yyyy-MM-dd HH:mm") + "   (UTC" + offsetText(oZones,oWinter,aCity[2]) + ")"
	next

	# 3. Is daylight saving time in force?
	oNewYork = oZones.FindSystemTimeZoneById("Eastern Standard Time")
	? nl + "3. New York in July: daylight saving? " + yesNo(oNewYork.IsDaylightSavingTime(oSummer)) + "; in January? " + yesNo(oNewYork.IsDaylightSavingTime(oWinter))

	# 4. A flight: leaves Cairo at 22:30 local time, flies 11 hours 15 minutes. When does it arrive in New York, in New York time?
	oDeparture = new DateTime(2024,7,1).AddHours(22).AddMinutes(30)                 # local time in Cairo
	oDepartureUtc = oZones.ConvertTimeToUtc([ :dateTime = oDeparture, :sourceTimeZone = oZones.FindSystemTimeZoneById("Egypt Standard Time") ])
	oArrivalUtc = oDepartureUtc.AddMinutes(11 * 60 + 15)
	oArrival = oZones.ConvertTimeFromUtc(oArrivalUtc,oNewYork)
	? nl + "4. departure Cairo " + oDeparture.Format("dd MMM HH:mm") + ", arrival New York " + oArrival.Format("dd MMM HH:mm")
	? "   in UTC: " + oDepartureUtc.Format("HH:mm") + " -> " + oArrivalUtc.Format("HH:mm") + " (the duration is the same in every zone)"

	# 5. A day does not always have 24 hours: on 10 March 2024 clocks in New York jump from 02:00 to 03:00
	oMissing = new DateTime(2024,3,10).AddHours(2).AddMinutes(30)
	? nl + "5. does 2:30 on 10 March 2024 exist in New York? " + yesNo(not oNewYork.IsInvalidTime(oMissing))
	oMidnight = new DateTime(2024,3,10)
	oNextMidnight = new DateTime(2024,3,11)
	nHours = (oZones.ConvertTimeToUtc([ :dateTime = oNextMidnight, :sourceTimeZone = oNewYork ]).Ticks - oZones.ConvertTimeToUtc([ :dateTime = oMidnight, :sourceTimeZone = oNewYork ]).Ticks) / 36000000000
	? "   hours between midnight and midnight that day: " + nHours

func localTime oZones, oUtc, cZoneId

	return oZones.ConvertTimeFromUtc(oUtc,oZones.FindSystemTimeZoneById(cZoneId))

# "+02:00" or "-04:00": the offset from UTC at that moment (it includes daylight saving)
func offsetText oZones, oUtc, cZoneId

	oZone = oZones.FindSystemTimeZoneById(cZoneId)
	cOffset = oZone.GetUtcOffset(oUtc).ToString()
	if left(cOffset,1) != "-"
		cOffset = "+" + cOffset
	ok
	return left(cOffset,6)

func padRight cText, nWidth

	while len(cText) < nWidth
		cText += " "
	end
	return cText

func yesNo lValue

	if lValue
		return "yes"
	ok
	return "no"
Output
1. the meeting at 12:00 UTC on 1 July 2024:
   New York  2024-07-01 08:00   (UTC-04:00)
   London    2024-07-01 13:00   (UTC+01:00)
   Cairo     2024-07-01 15:00   (UTC+03:00)
   Tokyo     2024-07-01 21:00   (UTC+09:00)

2. the same meeting at 12:00 UTC on 15 January 2024:
   New York  2024-01-15 07:00   (UTC-05:00)
   London    2024-01-15 12:00   (UTC+00:00)
   Cairo     2024-01-15 14:00   (UTC+02:00)
   Tokyo     2024-01-15 21:00   (UTC+09:00)

3. New York in July: daylight saving? yes; in January? no

4. departure Cairo 01 Jul 22:30, arrival New York 02 Jul 02:45
   in UTC: 19:30 -> 06:45 (the duration is the same in every zone)

5. does 2:30 on 10 March 2024 exist in New York? no
   hours between midnight and midnight that day: 23

How it works#

Zones#

TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time") finds a zone by its identifier. On Windows the identifiers are names such as Eastern Standard Time, GMT Standard Time, Egypt Standard Time and Tokyo Standard Time (Linux and macOS use America/New_York). ConvertTimeFromUtc(utc, zone) gives the local time and ConvertTimeToUtc([ :dateTime = local, :sourceTimeZone = zone ]) goes back. GetUtcOffset(time) tells the offset at that moment, so it includes daylight saving.

Daylight saving#

Steps 1 and 2 show the same UTC moment in July and in January. New York goes from −04:00 to −05:00, London from +01:00 to +00:00 and Cairo from +03:00 to +02:00 (Egypt has used daylight saving time again since 2023); Tokyo is always +09:00. The rules are different in each country and they change: Egypt did in 2023. That is why you must use the system's time zone data and never add or subtract hours yourself.

The flight#

The flight leaves Cairo at 22:30 local time and lasts 11 h 15 min. The program converts the departure to UTC (19:30), adds the duration in UTC (06:45 the next day) and converts the result to New York time (02:45). Never add a duration to a local time that can cross a daylight saving change.

A day with 23 hours#

On 10 March 2024 clocks in New York jump from 02:00 to 03:00. The time 02:30 does not exist (IsInvalidTime is true), and the day between two midnights has only 23 hours. In autumn the opposite happens: one hour exists twice (IsAmbiguousTime). Programs that schedule things at fixed local times must think about this.

Tip

Meetings and alarms: store the UTC time plus the zone id of the person. Then you can show it correctly in every place and recalculate when the rules change.

What you learned#

Exercises#

  1. Show the current time in four cities (DateTime.UtcNow and ConvertTimeFromUtc).
  2. Find the hours of the day when offices in New York and Tokyo are both open (9–17).
  3. Find a time that is ambiguous in New York in November 2024 (IsAmbiguousTime).
  4. List all the zones with TimeZoneInfo.GetSystemTimeZones and print those with a +05:30 offset.

Next#

Calendars and dates

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.