Working With Time Zones: This Version: Latest Version: Previous Version: Editor: Additional Contributors
Working With Time Zones: This Version: Latest Version: Previous Version: Editor: Additional Contributors
This version:
http://www.w3.org/TR/2011/NOTE-timezone-20110705/
Latest version:
http://www.w3.org/TR/timezone/
Previous version:
http://www.w3.org/TR/2005/NOTE-timezone-20051013/
Editor:
Addison Phillips, Invited Expert
Additional Contributors:
Norbert Lindenberg, Yahoo!
Mark Davis, Google; Unicode Consortium
Martin J. Dürst, Aoyama Gakuin University; W3C Internationalization
Interest Group Chair
Felix Sasaki, DFKI GmbH
Richard Ishida, W3C
Abstract
This document contains guidelines and best practices for working with time
and time zones in applications and document formats. Use cases are
provided to help choose an approach that ensures that geographically
distributed applications work well with date and time values. This document
also aims to provide a basic understanding and vocabulary for talking about
time and time handling in software, a source of confusion for many developers
and content authors on the Web.
This document contains best practices and user scenarios for working with
time zones. Date and time values can be complex and the relationship
between computer and human timekeeping systems can lead to problems.
This document aims to provide a basic understanding and vocabulary for
talking about time and time handling in software. The working group has
updated this document to contain more comprehensive guidelines and best
practices for working with time and time zones in applications and document
formats. Use cases are provided to help choose an approach that ensures
that geographically distributed applications work well.
This document was produced by a group operating under the 5 February 2004
W3C Patent Policy. W3C maintains a public list of any patent
disclosures made in connection with the deliverables of the group; that page
also includes instructions for disclosing a patent. An individual who has actual
knowledge of a patent which the individual believes contains Essential
Claim(s) must disclose the information in accordance with section 6 of the
W3C Patent Policy.
Table of Contents
1 Introduction
2 A Brief History of Timekeeping
2.1 Observed Time
2.2 Incremental Time
2.3 What Is a Time Zone?
2.4 What about Daylight Saving (Summer) Time?
2.5 How is a Time Zone Defined?
3 Representing Time Values in Data Structures
3.1 Incremental Time
3.2 Field-Based Time
3.3 Floating Time
4 Use Cases: How Time Zones Affect Applications
4.1 Timestamps
4.2 Past Events
4.3 Past and Future Events
4.4 Recurring Events
4.5 Floating Time Events
5 Representing Time Zones and Zone Offsets
5.1 Time Zone Identifiers
5.2 Identifying Zone from Offset and Country
5.3 Incremental versus Field-Based Time
6 Guidelines
6.1 Guidelines Summary
6.2 Working with Future and Recurring Events
6.3 Negotiating Time Zone with the User
6.4 Working with Field-Based Dates and Times based on XML Schema
6.5 Working with Date and Time Values that Require a Time Zone (and not a
zone offset)
7 Comparing Times
7.1 Working with XQuery / XSLT
1 Introduction
One common requirement for applications is the need to deal with dates,
times, or durations. Working with time-related data can be complex because
values are related to calendars and timekeeping rules, which themselves may
be somewhat arcane. One of these complexities in working with time-related
data is the effect of time zone on the data.
This document contains guidelines and best practices for working with time
and time zones in applications and document formats. Use cases are
provided to help choose an approach that ensures that geographically
distributed applications work well with date and time values. This document
also aims to provide a basic understanding and vocabulary for talking about
time, a source of confusion for many developers and content authors on the
Web.
[HTML5] defines a variety of date and time string types that can be used for
input and display.
[XML Schema] provides a variety of data types for dates and times, such
as date, time, and dateTime. These data types follow internationally friendly
formats defined by ISO 8601 and can be used to address a variety of differing
date or time applications.
The various XML Schema time types can either include or omit the offset from
Universal Coordinated Time (UTC). The presence (or absence) of this offset
implies different things about the time value represented and presents
different processing issues for applications. The same is true of HTML5's
"local date and time" or "global date and time" types.
A time zone is a set of rules for determining the local observed time (wall time)
as it relates to incremental time (as used in most computing systems) for a
particular geographical region.
Before the adoption of time zones, local time was derived directly from
observation. Clocks might be set, for example, based on an observed event
such as local noon. Traveling fairly short distances across the Earth's surface
results in changes in local observed time: you only have to travel about 28
kilometers (17 miles) at the equator (and less distance the further north or
south you travel) to alter the observed local noon by one minute.
UTC Offset All time zones have, as their basis, an offset from UTC. It is
the difference (positive or negative) between when a given time event is
observed in UTC and in local time. If all time zones used one-hour
offsets, there would be 25 world-wide time zones, ranging between 12
hours before UTC to 12 hours following UTC. However, there are some
that use half-hour or even quarter-hour offsets (or even some odd
offsets). In addition, some time zones fall outside a single-day span.
Observation of DST Some times zones include rules for observing
DST, while others do not. The observation of DST is defined by a set of
rules that include:
o Amount of DST offset Amount of time the clock changes when
DST starts or ends. Nowadays this is one hour, but other values
are theoretically possible (and have been used historically).
o Starting and ending day and time of DST The day and time of day
when DST begins and the day and time of day when it ends,
which varies by locality. For example, most areas that use DST do
so in the summer time. That is, they change their UTC offset
forward by one hour when DST starts in the spring and the
reverse when DST ends in the autumn. Since "spring" and
"autumn" happen in opposite parts of the year in the northern and
southern hemispheres, the starting and ending days are very
different for zones in opposite hemispheres. But note that even
regions that share a UTC offset and are similar in latitude (or are
even share borders) may have differing DST start or stop rules.
Adoption Dates Regions that currently have the same UTC offset and
DST behavior may have had different rules in the past. Correct handling
of past time values requires treating such regions as separate time
zones. For example, Korea Standard Time and Japan Standard Time
currently use the same UTC offset and neither uses daylight saving.
However, Japan abandoned DST in 1951, while South Korea used it
last in 1988, so an application that tracks time values that reach back
that far might need to track these time zones separately.
Example 1:
Adoption dates can be applied to any of the values that define a time
zone, such as the amount of DST offset and the starting/ending dates or
times for DST. For example, prior to 2007, the United States started
DST at 2 a.m. on the first Sunday in April for each time zone observing
DST. In 2007, the USA changed the start date and time to 2 a.m. on the
second Sunday in March.
There are several ways to represent time data, which vary in suitability
according to application need:
Incremental time measures time using fixed integer units that increase
monotonically from a specific point in time (called the "epoch"). Most
programming languages and operating environments provide or use
incremental time for working with time values. Incremental time is not usually
seen directly by users, but is mapped to observed time for human
consumption.
Example 2:
Note: One quirk of timekeeping is the need for leap seconds. The Earth's
rotation is not even and is slowing down. To help address this problem, the
[International Earth Rotation Service] occasionally mandates a "leap second"
to keep the calendar and other field-based time values in sync with
incremental timekeeping systems such as atomic clocks. A leap second
occurs once or sometimes twice per year and always takes the form of an
additional second added to the last minute of the day. Usually the leap second
is added to December 31st or June 30th.
Field-based time divides observed or wall time into separate field values such
as year, month, day, hour, minute, second, etc. Field-based times may or may
not be tied to either UTC or the local time zone—or may be indeterminate.
Field-based times are also typically tied to a specific calendar (such as the
Gregorian calendar). The formats described by the ISO 8601 standard are
field-based.
Example 3:
#include <stdio.h>
int main(void)
{
struct tm t;
time_t t_of_day;
t.tm_hour = 0; /* midnight */
t.tm_min = 0; /* midnight */
t.tm_sec = 0; /* midnight */
printf(ctime(&t_of_day));
return 0;
}
A field-based time may or may not include information about the time zone
being used. In a purely numeric representation, such as time_t, sometimes
only the current UTC offset is provided.
Example 4:
Consider this bit of Java code which is attempting to handle one such date:
f.setTimeZone(TimeZone.getTimeZone("UTC"));
f.setTimeZone(TimeZone.getTimeZone("UTC"));
f.setTimeZone(TimeZone.getTimeZone("Pacific/Honolulu"));
f.setTimeZone(TimeZone.getTimeZone("Pacific/Kiritimati"));
This code parses a date string into a Date object, which is a type of
incremental time. This means that, instead of "floating" as needed between
time zones, it has a fixed relationship to UTC and might be used to display the
wrong value, depending on the local time offset. In fact, any floating date can
stretch over a period of up to fifty hours—beginning with the Pacific/Kiritimati
time zone (UTC+14:00) and extending up to the Pacific/Honolulu time zone
(UTC-11:00).
There are a number of different ways that time zones can affect how time
values are stored or processed in an application:
4.1 Timestamps
In fact, it is often desirable to normalize time values to UTC (or a specific UTC
offset) so that separate series of data can be easily compared and merged.
Information about local offset may be valuable in recovering the actual wall
time, but time zone rules are probably only rarely interesting.
For events that occurred in the past (with no future events) for which you need
to know what the wall time was, the time zone of the event may be necessary
additional data. Once an event is in the past, its relationship to incremental
time becomes fixed and the rules for generating wall time remain static
essentially forever. You might still need to know that an event occurred at
10:00 rather than at 14:00 local time. At a minimum, the zone offset from UTC
is necessary, although knowing the complete time zone is necessary for some
applications. Knowing the specific time zone allows one to reconstruct the
time and its relationship to other wall times.
If your application deals with both past and future events (for example, if you
have a calendar or a meeting schedule), you’ll need additional time zone
information to ensure proper time computation. At a minimum you will need
the time zone, not merely an offset from UTC. This is because a future event's
wall time depends on time zone related information, such as DST transitions.
One issue with future events is that time zone rules can change from time to
time and these may require an application to update affected data records in
order to meet user’s expectations. This is because many systems actually
store the time portion of the value as an incremental time and the incremental
time needs to be changed if the wall time offset from UTC has been altered.
4.4 Recurring Events
Floating time events are sometimes indicated by omitting the time zone or
UTC offset from a time value. It's generally important to know when a time
value represents a floating time, because your application's handling of the
value needs to be different.
Since floating time values are often dates without any associated hours,
minutes, or seconds, the resulting incremental time for these fields is often set
to zero, exacerbating the problem: all time zones west of the prime meridian
will consider a floating time to be the previous day.
Example 5:
Calendar c = new GregorianCalendar();
c.setTimeZone(TimeZone.getTimeZone("UTC"));
c.clear(); // clears all fields in the calendar
c.set(Calendar.YEAR, 2009);
c.set(Calendar.MONTH, 8); // recall that Java months are zero
based
c.set(Calendar.DATE, 1);
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long start = c.getTime().getTime();
c.roll(Calendar.DATE, true); // start of the next day
long end = c.getTime().getTime();
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println(Long.toHexString(start) + " " +
new Date(start) + "\n" +
Long.toHexString(end) + " " +
new Date(end));
You might expect to see 2009-09-01 consistently, but here's the actual output:
12372e6a000 Mon Aug 31 17:00:00 PDT 2009
123780cfc00 Tue Sep 01 17:00:00 PDT 2009
Depending on the types of data your application needs to represent, you may
need to alter or extend your data structures to handle the different
requirements laid out above.
Example 6:
[XML Schema] follows the ISO 8601 standard for its lexical representation.
ISO 8601 is a field-based way to represent time values and increments. A
time value can include (or omit) the zone offset from UTC. Howeve, a zone
offset is not the same thing as a time zone and the difference can be
important depending on your application.
Note that, although ISO 8601 is expressed in terms of the Gregorian calendar,
it can be used to represent values in any calendar system. The presentation
of date and time values to end users using different calendar and timekeeping
systems is separate from the lexical representation, as the time value can be
converted to an incremental time and then new field values computed for
some other calendar or time keeping system that uses alternate rules.
The most definitive reference for identifying sets of time zone rules is the TZ
database (also known as the "Olson time zone database" [tzinfo]), which is
used by systems such as various commercial UNIX operating systems, Linux,
Java, CLDR, ICU, and many other systems and libraries. Other systems exist:
for example, Microsoft Windows uses its own data set and identifiers.
In the TZ database, time zones are given IDs that typically consist of a region
and exemplar city. An exemplar city is a city in the time zone in question that
should be well-known to people using the time zone. For example, the U.S.
Pacific time zone has a TZ database ID of "America/Los_Angeles". The TZ
database also supplies aliases for many IDs; for example, "Asia/Ulan Bator" is
equivalent to "Asia/Ulaanbaatar". The Common Locale Data Repository [CLDR]
can be used to provide a localized form for the IDs: see Appendix J in [UAX
35]. Note: some systems, such as Apple Inc.'s MacOS, provide additional
exemplar cities.
Most countries are either small enough in area or, for practical reasons,
choose to observe only a single time zone for the entire country. This means
that knowing the country of the user is frequently sufficient to identify the time
zone of the user as well. At the time this document was published, only twenty
countries had more than one observed time zone. These countries are:
Argentina, Australia, Brazil, Canada, Chile, Democratic Republic of the
Congo, Ecuador, France, Greenland, Indonesia, Kazakhstan, Kiribati, Mexico,
Micronesia, Mongolia, New Zealand, Portugal, Russia, Spain, and the United
States.
Within each of the countries that observe multiple time zones, knowing the
current offset and current time will usually allow you to determine the time
zone accurately. An exception to this is the United States: there exist some
regions, such as Arizona, whose time zone cannot be determined strictly from
the current time, country and offset, although an inferred time zone will always
work for current time applications (not future and past times).
Incremental time and field-based time differ in the way certain operations
work. For example, incremental times can be directly compared: their integer
values determine which is earlier or later. Field based times must be
normalized and their individual fields compared. Field based times can have
certain kinds of logical operations performed on them (for example, rolling the
month forward or back), while incremental time requires a logical
transformation.
Bear in mind that rolling fields forwards or backwards in field-based times can
be tricky. For example, Feburary does not always have the same number of
days in it. Or consider the problem of incrementing the month forward by one
in the date 2011-01-30.
The SQL data types date, time, and timestamp are field based time values
which are intended to be zone offset independent: they are actually,
technically, floating time values! The data type timestamp with time zone is the
zone offset-dependent equivalent of timestamp in SQL. Programming
languages, by contrast, tend to use incremental time and convert to and from
a localized textual representation on demand. Databases may use
incremental time or either zone offset-dependent or independent field-based
structures internally. For example, Oracle databases treat a timestamp field as
though it is in the local time of the database instance. This can have unusual
effects on queries: a field based time value that represents a local time with
daylight saving needs to identify the UTC offset and whether daylight saving is
in effect or not in order to disambiguate the repeated time when transitioning
from daylight saving time (DST) to standard time. For example, in the United
States in 2009, any instant between 1 and 2 am on November 1st happens
twice, once in DST and a second time in standard time. Field based types
without a time zone field (such as an Oracle timestamp) cannot represent the
repeated times unambiguously without supplemental information provided
externally.
As a result, users may not be clear on the differences between these types or
may create a mixture of different representations. For example, a Java
programmer using JDBC will retrieve incremental times
(java.util.Date objects) from a database, even though the actual field in the
database is a (field-based) timestamp value.
In XML Schema, as with SQL, dates and times are always expressed using
field-based time. The date or time may express the zone offset from UTC (for
example using a format such as 08:00:00+01:00). UTC is indicated by the letter
Z (for example 08:00:00Z). Or, the zone offset may be omitted completely.
Properly speaking, an XML Schema date or time value with a zone offset is
field-based/zone offset dependent and one without is field-based/zone offset
independent.
If the two types are mixed, then the interpretation of the zone offset is not
adequately specified in XML Schema. In XQuery 1.0 and XPath 2.0 Functions
and Operators[XPathFO], the interpretation is implementation-defined and is
based on an implicit zone offset. This is usually either UTC or local time. The
presence or absence of the zone offset in the XML Schema representation
may not be indicative of the original data's intention because of the confusion
described above. Proper comparisons or processing rely on normalizing all
date and time values into zone offset-independent (or zone offset-dependent)
forms and never mixing the two in a particular operation.
6 Guidelines
This section describes different guidelines that can be applied to various time
and date comparisons.
Generally:
When creating content, use UTC for your time values whenever
possible so that values from discrete sources can be compared more
readily
Allow the user to choose a time zone associated with a user session or
profile if possible.
o Consider using exemplar cities to help users identify the time
zone.
o Use the country as a hint, since most countries have only a single
time zone
o Omit historical time zones if appropriate
use IP-geolocation, cellular radio country code, GPS data, or other
external data sources if available.
Use an explicit zone offset with date, time, and dateTime types, if
possible.
o Include an additional field indicating the time zone, if possible.
Avoid applying operations based on date or time types (such as
indexing) to collections of data in which some data items may have
zone offset information and other data items may not have zone offset
information.
If you have data that includes implicit and fixed explicit zone offsets,
before applying any date- or time-sensitive operations adjust the zone
offset of the implicit data to UTC with the functions for zone offset
adjustment, cf. sec. 10.7 in [XPathFO].
If you have data that contains both implicit and fixed explicit time zones
and you do not want to adjust the data subset which already has a zone
offset, make sure that you recognize this data subset, for example via
the component extraction functions, cf. sec. 10.5 in [XPathFO].
When creating an application that can store values in the future, including
recurring events, you'll need to make additional data fields to ensure that you
can reconstruct the user's intentions and adapt future time values to changes
in time zones and time zone rules (especially alteration of daylight
saving/summer time start and stop).
If the time should change based on the rules for a given time zone (such as
DST transition), store the originating time zone and original offset applied. If
the event is recurring, you must also store a flag indicating whether DST
transitions should be applied to future occurrences of the event or not: this
tells you whether the user intended a specific wall time or a specific
incremental time. For example, if you schedule a phone call for Friday, August
27, 2010 at 10:30 Pacific Daylight Time, then the field values stored might be:
2010-08-27T10:30:00-07:00 !-- -07:00 is the originating offset value
America/Los_Angeles !-- This is Pacific time
false !-- don't alter the time when DST changes or
rules change
If you then set up a recurrence rule, such as "weekly" for this call, you can
compute that "10:30 Pacific" becomes "9:30 Pacific" when the Pacific time
zone transitions back to standard time from summer time. If the rules change
for the Pacific time zone, you won't need to alter your data or search through
all records in you database to update the data (you will still have to recompute
the incremental time, though). When setting up recurrence:
Store the originating offset and whether to apply DST rules in addition to
time and time zone
Recompute future incremental times if time zone rules change (you'll
need the original offset to do this)
Users may sometimes need to specify time values, such as in an HTML form.
If the time is the current time, then ordinary incremental time may be used
("it's the same time everywhere in UTC"). However, if the user is specifying a
time or date in the future or past, the time zone being used becomes
important.
It may be possible to determine the user's current time zone from the browser.
For example, converting an array of date values to local time can be used to
determine the UTC offset and daylight saving transition (if any), which can
then be compared to known rules for time zones. In other cases, external data
such as IP-geolocation, cellular radio country code (ITU E.212 MCC), or GPS
data may be available. But for most systems, the user will need to choose a
time zone. There are always edge cases in which even very good external
data cannot resolve the time zone accurately.
Because there are many time zones in the world, one way to make choosing
time zone more accessible is to have the user to choose country first. For the
few ambiguous cases, the user can then be presented with a much smaller
number of specific choices. Note that the Olson time zone database also
defines a large number of zones that are of mainly historical interest (their
rules were different from current time zone rules in the past but the zones are
no longer distinct). These historical zones should usually be omitted from any
user choice for time zone.
Use incremental time in UTC if possible (HTML5: global date and time
with time zone of 'Z')
Allow the user to choose a time zone associated with a user session or
profile if possible.
o Consider using exemplar cities to help users identify the time
zone.
o Use the country as a hint, since most countries have only a single
time zone
o Omit historical time zones if appropriate
use IP-geolocation, cellular radio country code, GPS data, or other
external data sources if available.
6.4 Working with Field-Based Dates and Times based on XML Schema
Field-based time and date values require the user to determine whether to
use a fixed zone offset, a time zone, or nothing. While XML Schema times are
field-based in terms of the lexical representation, the underlying data or
implementation may use incremental time, as may the implementation
processing the values. Each specific case requires specific handling.
For incremental time values: use a specific zone offset, preferably always
UTC
Documents or systems can also choose to accompany a time value with the
appropriate time zone identifier or TZID using a complex type. This is very
important with recurring times, such as calendar meeting times. If a regular
meeting is at "08:00 Pacific Time", it is insufficient to store and interchange
just a zone offset.
Unfortunately, XML Schema date and time types do not provide for Olson IDs,
so most time operations cannot use TZIDs directly. Time zone identification in
the date and time types relies entirely on time zone offset from UTC. It is up to
the document designer to keep the TZID in a separate data field from the time
value.
There are different ways to compare two <datetime, TZID> pairs. If both the
date and time are fixed (2004-09-31T01:30), then this can be done by computing
the offsets on that date and at those times, using the TZ database. This order
then reflects whether one datetime is (absolutely) before another.
If the dates are not fixed (such as <T01:30, TZID> — notice that the date value
is omitted) then in some sense, neither is 'before' the other, since each refers
to a repeating, interleaved set of points in time. The simplest comparison
mechanism where the dates may not be fully specified is simply to put both in
canonical form, then order them first by time then by TZID (alphabetical,
caseless order). The Olson database does not maintain a fixed canonical
form; however, CLDR does provide such a form. (It is also possible to have a
looser comparison, whereby <time0, TZID0> is compared to <time1,
TZID1> over some interval of time: if one consistently has a smaller offset
during that period, it is considered to be less than the other value. However,
there are cases where this mechanism results in a partial ordering.)
Note that when you pass a dateTime and time zone ID together, you may
wish to supply the time zone's offset as part of the data (using local time
rather than UTC) so that applications have access to both sets of information.
For example <T01:30-08:00, "America/Los_Angeles"> tells you it is both the US
Pacific time zone and that the offset was 8 hours from UTC (making it
standard time, but also making it 9:30 UTC).
7 Comparing Times
Conversion between or operations on data sets that mix values with and
without zone offsets present certain problems.
Users of XQuery 1.0 and XSLT 2.0 and other standards should take the
following recommendations into account even if time-zone-sensitive data is
rarely used. Sooner or later some data will be affected by the issues
described:
Use an explicit zone offset with date, time, and dateTime types, if
possible.
o Include an additional field indicating the time zone, if possible.
Do not apply operations based on date or time types (such as indexing)
to collections of data in which some data items may have zone offset
information and other data items may not have zone offset information.
If you have data that includes implicit and fixed explicit zone offsets,
before applying any date- or time-sensitive operations adjust the zone
offset of the implicit data to UTC with the functions for zone offset
adjustment, cf. sec. 10.7 in [XPathFO].
If you have data that contains both implicit and fixed explicit time zones
and you do not want to adjust the data subset which already has a zone
offset, make sure that you recognize this data subset, for example via
the component extraction functions, cf. sec. 10.5 in [XPathFO].
Acknowledgments
This document is based on several previous documents. The original Working
Group Note (Working With Timezones) was written by Martin Dürst, Mark
Davis, Felix Sasaki, and Addison Phillips. Portions of this document, notably
the introduction, were taken from an older document ("It's about time") by
Addison Phillips. Information on time zone scenarios is based on work by
Norbert Lindenberg.