<?xml version="1.0"?>
<!DOCTYPE spec SYSTEM "xmlspec-i18n.dtd">
<spec w3c-doctype="wgnote" id="timezone" xml:lang="en-US"><header><title>Working with Time Zones</title><w3c-designation>timezone</w3c-designation><w3c-doctype>W3C Working Group Note</w3c-doctype><pubdate><day>13</day><month>October</month><year>2005</year></pubdate><publoc><loc href="http://www.w3.org/TR/2005/NOTE-timezone-20051013/">http://www.w3.org/TR/2005/NOTE-timezone-20051013/</loc></publoc>
    <altlocs>
      <loc href="http://www.w3.org/TR/2005/NOTE-timezone-20051013/timezone.xml">XML</loc>
    </altlocs>
    <latestloc><loc href="http://www.w3.org/TR/timezone">http://www.w3.org/TR/timezone</loc></latestloc>
    <!--<prevlocs><?xm-replace_text {prevlocs}?></prevlocs>-->
    <authlist><author>
        <name>Addison Phillips</name>
        <affiliation>(Invited Expert)</affiliation>
      </author><author><name>Felix Sasaki</name><affiliation>W3C</affiliation></author><author><name>Mark Davis</name><affiliation>IBM</affiliation></author><author><name>Martin Dürst</name><affiliation>Aoyama University</affiliation></author>
      
    </authlist>
    <status><p>
				<emph>This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the <loc href="http://www.w3.org/TR/">W3C technical reports index</loc> at http://www.w3.org/TR/.</emph>
			</p><p>This document discusses the topic of <code>date</code>, <code>time</code>, and <code>dateTime</code> values from <bibref ref="xsd"/> with and without time zone offsets. Examples are given mainly relying on <bibref ref="xsd"/> and <bibref ref="XPathFO"/>, since these are the basis for <bibref ref="XQuery"/> and <bibref ref="xslt20"/> processing of date/time values.</p><p>This document is a <loc href="http://www.w3.org/2004/02/Process-20040205/tr.html#q74">W3C Working Group Note</loc>. It has been produced by the <loc href="http://www.w3.org/International/core/">i18n Core Working Group</loc>, which is part of the <loc href="http://www.w3.org/International/">Internationalization Activity</loc>.</p><p>This document has been produced as a result of discussions between the i18n Core Working Group and the two Working Groups <loc href="http://www.w3.org/XML/Query/">XQuery Working Group</loc> and <loc href="http://www.w3.org/Style/XSL/">XSL Working Group</loc>. These Working Groups will  refer to this document from <bibref ref="XPathFO"/>. The i18n Core Working Group will use material from this document for FAQs or tutorials on the topic. Please send your comments on this document to the
        public mailing list <loc href="mailto:www-i18n-comments@w3.org">www-i18n-comments@w3.org</loc> (<loc href="http://lists.w3.org/Archives/Public/www-i18n-comments/">archive</loc>).</p><p>Publication as a Working Group Note does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.</p></status><abstract><p>This document discusses some of the problems encountered when working with the <code>date</code>, <code>time</code>, and <code>dateTime</code> values from <bibref ref="xsd"/> when those value include (or omit) time zone offsets. Many W3C technologies rely on date and time types. Examples include the <bibref ref="XPathFO"/> specification, since it is the basis for XQuery and XSLT processing of date/time values, but the concepts affect any date / time processing.</p></abstract><langusage><language id="en">en</language></langusage><revisiondesc><p>This is the first version of this document. </p></revisiondesc></header><body><div1><head>Working With Time Zones</head><p>Time-related data is a common requirement for many applications. <bibref ref="xsd"/> provides a variety of data types for dates and times, such as <code>date</code>, <code>time</code>, and <code>dateTime</code>. These data types follow internationally friendly formats defined by <bibref ref="ISO8601"/> and can be used to address a variety of differing date or time applications.

</p><p>The <code>date</code>, <code>time</code>, and <code>dateTime</code> types can either include or omit the time zone offset. The presence (or absence) of the offset means that the data value must be handled differently for certain kinds of operations. In addition, the particular application and source of the date and time values affects how dates and times with different time zones or zone offsets should be handled, as well as how to handle values that lack any time zone or zone offset indication.</p><note><p>Users and implementers of languages or specifications which handle time-related data 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. Some examples of these include XQuery, XPath, and XSLT.</p></note><div2><head>Background</head><p>There are three main applications of date, time, or dateTime data types in applications.</p><olist><item><p><term>Incremental or Computer Time</term> Most programming languages and development environments provide data types for handling time which are based on a numeric value: units of some specific length measured from a specific point in time (called the epoch). For example, the Java type <code>java.util.Date</code> is a long (integer) value for the number of milliseconds since 00:00 (midnight) on January 1, 1970 in UTC (Universal Coordinated Time, sometimes also called GMT). Other systems use other units and epochs. Date and time values based on a construct of this type (which we'll call <term>computer time</term>) are time-zone-independent, since at any given moment it is the same time in UTC everywhere on Earth: the values can be transformed for display for any particular time zone offset, but the value itself is not tied to a specific location. Values of this type are commonly used in applications as "time stamps", showing when an event occurred. Some applications for these include:</p><ulist><item><p>    labeling log file entries with a timestamp</p></item><item><p>
    recording actual process start or stop times</p></item><item><p> measuring the duration of an event</p></item><item><p> comparing two time values</p></item></ulist></item><item><p><term>Time Zone Independent Field-Based Time</term> The human representation of computer times is more complicated, and represents time using various separate field values, such as hour, minute, month, or year. One application for this type of representation is for values that are time zone independent, representing a logical event divorced from a particular location on the Earth. For example, various kinds of <quote>anniversary date</quote> such as a person's birthdate or an employee hire date would normally fall into this category, partly because time is not expressed, and partly because the actual time of the start and end of the day for a given geographic location may not be considered important. Some other examples of this application of dates and times include:</p><ulist><item><p>last day of the quarter</p></item><item><p>holiday schedules</p></item><item><p>end date of a promotional offer</p></item><item><p>legally mandated start or end times</p></item></ulist></item><item><p><term>Time Zone Dependent Field-Based Time</term> In other cases, field-based dates and times are supposed to represent values linked to a particular location or time zone. For example, if you tell someone that you will make a telephone call to them at 14:00 from Paris, if that person is in London they'll expect the phone to ring at 13:00. As with incremental time, the event happens in the same instant around the globe and meaning of the value depends on the offset from UTC. Some other examples of this application of dates and times include:</p><ulist><item><p>purchase order date</p></item><item><p>tracking information for a package</p></item></ulist></item></olist></div2><div2><head>Identifying Time Zones and Zone Offsets</head><p><bibref ref="xsd"/> follows the <bibref ref="ISO8601"/> standard for its lexical representation. Date and time values in ISO 8601 are field-based using the definitions above and can indicate (or omit) the zone offset from UTC. A zone offset is not the same thing as a time zone, and the difference can be important. XML Schema only supports zone offset, but, confusingly, calls it time zone, see for example section 3.2.8.1, lexical representation in <bibref ref="xsd"/>.</p><p>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.

</p><p><emph>What is a "zone offset"?</emph> A <term>zone offset</term> is the difference in hours and minutes between a particular time zone and UTC. In ISO 8601, the particular zone offset can be indicated in a date or time value. The zone offset can be <code>Z</code> for UTC or it can be a value "+" or "-" from UTC. For example, the value <code>08:00-08:00</code> represents 8:00 AM in a time zone 8 hours behind UTC, which is the equivalent of 16:00Z (8:00 plus eight hours). The value <code>08:00+08:00</code> represents the opposite increment, or midnight (08:00 minus eight hours).

</p><p><emph>What is a "time zone"?</emph> A <term>time zone</term> is an identifier for a specific location or region which translates into a combination of rules for calculating the UTC offset. For example, when a website maintaining a group calendar in the United States schedules a recurring meeting for <code>08:00 Pacific Time</code>, it is referring to what is sometimes known as <term>wall time</term> (so called because that is the time shown <quote>on the clock (or calendar) on the wall</quote>). This is not equivalent to either <code>08:00-08:00</code> or <code>08:00-07:00</code>, because <code>Pacific Time</code> does not have a fixed offset from UTC; instead, the offset changes during the course of the year. As mentioned before, XML Schema only supports zone offsets, and it does not make the terminological distinction between zone offset and time zone. So a wall time expressed as an XML Schema <code>time</code> value, must choose which zone offset to use. This may have the unintended effect of causing a scheduled event to shift by an hour (or more) when wall time changes to or from Daylight/Summer time.</p><p>To complicate matters, the rules for computing when daylight savings takes effect may be somewhat complex and may change from year to year or  from location to location. In the United States, the state of Indiana, for example, does not follow daylight savings time, but this will change in April 2006. See: <loc href="http://www.mccsc.edu/time.html">http://www.mccsc.edu/time.html</loc> for further information. The Northern and Southern hemispheres perform Daylight/Summer Time adjustments during opposing times during the year (corresponding to seasonal differences in the two hemispheres).

</p><p>To capture these situations, a calendar system must use an <term>ID for the time zone</term>. The most definitive reference for dealing with wall time is the TZ database (also known as the "Olson time zone database" <bibref ref="tzinfo"/>), which is used by systems such as various commercial UNIX operating systems, Linux, Java, CLDR, ICU, and many other systems and libraries. In the TZ database, <quote>Pacific Time</quote> is denoted with the ID <code>America/Los_Angeles</code>. The TZ database also supplies aliases among different IDs; for example, <code>Asia/Ulan Bator</code> is equivalent to <code>Asia/Ulaanbaatar</code>. From these alias relations, a canonical identifier can be derived. The Common Locale Data Repository <bibref ref="CLDR"/> can be used to provide a localized form for the IDs: see Appendix J in <bibref ref="ldml"/>.</p></div2><div2><head>Incremental versus Field-Based Time</head><p>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—while 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 date forward or back), while incremental time requires a logical transformation. For example, to set the date <code>2005-08-30</code> forward by one day, an implementation can add 'one unit' to the "day" field and adjust the month and year as appropriate. In incremental time, a similar operation might be performed by incrementing the value by 24 hours * 60 minutes * 60 seconds * 1000 milliseconds, which is one logical day, but there may be errors when a particular day has more or fewer seconds in it (such as occur during daylight savings transitions).</p><p>The SQL data types <code>date</code>, <code>time</code>, and <code>timestamp</code> are field based time values which are intended to be zone offset independent. The data type <code>timestamp with time zone</code> 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, an Oracle 8 database treats a <code>timestamp</code> field as though it is in the local time of the database instance.</p><p>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 (<code>java.util.Date</code> objects) from a database, even though the actual field in the database is a (field-based) <code>timestamp</code> value.

</p><p>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 <code>08:00:00+01:00</code>). UTC is indicated by the letter <code>Z</code> (for example <code>08:00:00Z</code>). Or, the zone offset may be omitted completely.

</p><p>Properly speaking, an XML Schema <code>date</code> or <code>time</code> value with a zone offset is <term>field-based/zone offset dependent</term> and one without is <term>field-based/zone offset independent</term>.

</p><p>If the two types are mixed, then the interpretation of the zone offset is not adequately specified in <bibref ref="xsd"/>. In <bibref ref="XPathFO"/>, the interpretation is implementation-defined and is based on an <term>implicit zone offset</term>. 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.
</p></div2><div2><head>Guidelines</head><p>This section describes different guidelines that can be applied to various time and date comparisons.</p><div3><head>Working with Field-Based Dates and Times based on XML Schema</head><p>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 may use incremental time, as may the implementation processing the values. Each specific case requires specific handling.

    </p><ulist><item><p>If all of the data values are used to represent incremental time, then the user should always use a specific zone offset (and UTC is strongly recommended as this offset, since most incremental time systems are based on it) and should always specify that zone offset. Values that do not specify a zone offset should be treated as if they use the same offset. If UTC is used, this produces the least amount of modification in the data.</p></item><item><p>If all of the data values are used to represent time zone independent values (such as a list of employee's birth dates), then the zone offset should always be omitted. Any values that have a zone offset should probably ignore the zone offset (actually stripping it off, if possible), since zone changes are probably an artifact of other processing. If a zone offset must absolutely be applied to the data, then UTC should be used.</p></item><item><p>If all of the data values are used represent time zone dependent values, then the zone offset must always be supplied. Great care should be used to ensure that the correct offset is used and not just the current zone offset. For example, if a system in the U.S. Pacific time zone (<code>America/Los_Angeles</code>) generates a <code>dateTime</code> value <code>2005-02-11T11:23:04-07:00</code> on <code>2005-08-16</code>, it may be an error (since the offset from UTC during August in that time zone is UTC-7, but the zone offset in February is UTC-8).</p></item><item><p> If there are time values (with no date portion) with a fixed UTC offset, then the zone offset should always be indicated if and only if the time value really is fixed. That is, this would not apply to a meeting scheduled in Pacific Time, but would apply to a meeting that is always UTC-08:00 (and thus at 7:00 in the morning in Pacific time during parts of the year).</p></item></ulist></div3><div3><head>Working with Date and Time Values that Require a Time Zone (and not a zone offset)</head><p>Documents or systems can also choose to accompany a time value with the appropriate time zone identifier or <term>TZID</term> using a complex type. This is very important with recurring times, such as calendar meeting times. If a regular meeting is at <quote>08:00 Pacific Time</quote>, it is insufficient to store and interchange just a zone offset.</p><p>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.</p><p>There are different ways to compare two <code>&lt;datetime, TZID&gt;</code> 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 <code>datetime</code> is (absolutely) before another. </p><p>If the dates are not fixed (such as <code>&lt;T01:30, TZID&gt;</code>  Enotice 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 (see: <bibref ref="CLDR"/>).</p><p>(It is also possible to have a looser comparison, whereby <code>&lt;time0, TZID0&gt;</code> is compared to <code>&lt;time1, TZID1&gt;</code> 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.)</p></div3><div3><head>Comparing Times</head><p>Conversion between or operations on data sets that mix values with and without zone offsets present certain problems.</p><example><head>Values with and without zone offsets</head><eg>  &lt;aDateTime&gt;2005-06-07T13:14:27Z&lt;/aDateTime&gt;  &lt;!-- with a zone offset --&gt;
  &lt;bDateTime&gt;2005-06-07T11:00:00&lt;/bDateTime&gt;   &lt;!-- without --&gt;</eg></example><p>If one wishes to write a comparison between the value of &lt;aDateTime&gt; and &lt;bDateTime&gt;, then the two values must be reconciled to use the same reference point. &lt;aDateTime&gt; uses UTC and can easily be converted to computer time or shifted to another zone offset. &lt;bDateTime&gt; contains no indication of the zone offset. It may be UTC or any other value (currently up to 14 hours different in either direction from UTC).</p><p>It is good practice to use an explicit zone offset wherever possible. If one is not available, best practice is to use UTC as the implicit zone offset for conversions of this nature. This is because the values are exactly centered in the range of possibilities and because representation internally (as computer time) is usually based on UTC. Since a single reference point has been used it may be possible to unwind the change later even if erroneous conversion takes place. When working with multiple documents from various sources, the "implicit" offset of the document may vary widely from that of the implementation doing the processing. If UTC is widely used, the chances of error are reduced.</p><p>Content and query authors are warned that comparing or processing dateTimes with and without time offsets may produce odd results and such processing should be avoided whenever possible. Generating content that omits zone offset information (where it exists) is a recipe for errors later. Of course, data such as the SQL types cited earlier which is meant to represent wall time should continue to omit the zone offset. Query writers can check for the presence (or absence) of zone offset and should do so to modify dates and times explicitly (instead of allowing implicit conversion) whenever possible.</p></div3></div2><div2><head>Recommendations for XQuery / XSLT</head><p>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:</p><olist><item><p>If possible, make sure that data always contains an explicit zone offset.</p></item><item><p>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.</p></item><item><p>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 <bibref ref="XPathFO"/>.</p></item><item><p>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 <bibref ref="XPathFO"/>.</p></item></olist></div2></div1></body><back><inform-div1><head>References</head><blist><bibl key="CLDR" id="CLDR" href="http://unicode.org/cldr/"><loc href="http://unicode.org/cldr/">Common Locale Data Repository</loc>. Unicode Consortium.</bibl><bibl key="ISO 8601" id="ISO8601" href="http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html">ISO 8601:2004. <titleref>Data elments and interchange formats EInformation interchange  ERepresentation of dates and times</titleref>. ISO, 2004.</bibl><bibl key="NOTE-datetime" href="http://www.w3.org/TR/NOTE-datetime" id="NOTE-datetime">Misha Wolf, Charles Wicksteed. <titleref href="http://www.w3.org/TR/1998/NOTE-datetime-19980827">Date and Time Formats</titleref>. W3C Note 15 September 1997. Available at <xspecref href="http://www.w3.org/TR/1998/NOTE-datetime-19980827">http://www.w3.org/TR/1998/NOTE-datetime-19980827</xspecref>. The latest version of <xspecref href="http://www.w3.org/TR/NOTE-datetime">Note-datetime</xspecref> can be found at http://www.w3.org/TR/NOTE-datetime.</bibl>

<bibl key="RFC 3339" id="RFC3339" href="http://www.ietf.org/rfc/rfc3339.txt">G. Klyne, C. Newman. <titleref href="http://www.ietf.org/rfc/rfc3339.txt">Date and Time on the Internet: Timestamps</titleref>, IETF Standard, July 2002.</bibl><bibl href="ftp://elsie.nci.nih.gov/pub/" key="tzinfo" id="tzinfo"><loc href="http://www.twinsun.com/tz/tz-link.htm">Time Zone Information Database</loc>
. Available at 
<loc href="http://www.twinsun.com/tz/tz-link.htm">http://www.twinsun.com/tz/tz-link.htm</loc>.</bibl><bibl id="ldml" key="UAX 35">Mark Davis,
  <titleref href="http://unicode.org/reports/tr35/tr35-5.html">Locale Data Markup Language (LDML)</titleref>, Unicode Technical Standard #35.
Available at <xspecref href="http://unicode.org/reports/tr35/tr35-5.html">http://unicode.org/reports/tr35/tr35-5.html</xspecref>. The latest version of <xspecref href="http://unicode.org/reports/tr35/">LDML</xspecref> is available at http://unicode.org/reports/tr35/.
  </bibl><bibl id="xsd" key="XML Schema">Paul V. Biron, Ashok Malhotra, editors. <titleref href="http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/">XML Schema Part 2: Datatypes Second Edition</titleref>. W3C Recommendation 28 October 2004. Available at <xspecref href="http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/">http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/</xspecref>. The latest version of <xspecref href="http://www.w3.org/TR/xmlschema-2/">XML Schema</xspecref> is available at http://www.w3.org/TR/xmlschema-2/.
  </bibl><bibl key="XPath 2.0" id="xpath20">Anders Berglund, Mary F. Fernández, Scott Boag, et. al., editors. <titleref href="http://www.w3.org/TR/2005/WD-xpath20-20050915/">XPath 2.0</titleref>. W3C Working Draft 15 September 2005. Available at <xspecref href="http://www.w3.org/TR/2005/WD-xpath20-20050915/">http://www.w3.org/TR/2005/WD-xpath20-20050915/</xspecref>. The latest version of <xspecref href="http://www.w3.org/TR/xpath20/">XPath 2.0</xspecref> is available at http://www.w3.org/TR/xpath20/.
</bibl><bibl id="XPathFO" key="XPathFO">Ashok Malhotra, Jim Melton, Norman Walsh, editors. <titleref href="http://www.w3.org/TR/2005/WD-xpath-functions-20050915/">XQuery 1.0 and XPath 2.0 Functions and Operators</titleref>. W3C Working Draft 15 September 2005. Available at <xspecref href="http://www.w3.org/TR/2005/WD-xpath-functions-20050915/">http://www.w3.org/TR/2005/WD-xpath-functions-20050915/</xspecref>. The latest version of <xspecref href="http://www.w3.org/TR/xpath-functions/">XPathFO</xspecref> is available at http://www.w3.org/TR/xpath-functions/.
  </bibl><bibl id="XQuery" key="XQuery" href="http://www.w3.org/TR/query-semantics/">Scott Boag, Don Chamberlin, Mary F. Fernández, Daniela Florescu, Jonathan Robie, Jérôme Siméon, editors. <titleref href="http://www.w3.org/TR/2005/WD-xquery-20050915/">XQuery 1.0: An XML Query Language</titleref>. Available at <xspecref href="http://www.w3.org/TR/2005/WD-xquery-20050915/">http://www.w3.org/TR/2005/WD-xquery-20050915/</xspecref>. W3C Working Draft 15 September 2005. The latest version of <xspecref href="http://www.w3.org/TR/xquery/">XQuery</xspecref> is available at http://www.w3.org/TR/xquery/.
  </bibl><bibl key="XSLT 2.0" id="xslt20">Michael Kay, editor. <titleref href="http://www.w3.org/TR/2005/WD-xslt20-20050915/">XSL Transformations (XSLT) Version 2.0</titleref>. W3C Working Draft 15 September 2005. Available at <xspecref href="http://www.w3.org/TR/2005/WD-xslt20-20050915/">http://www.w3.org/TR/2005/WD-xslt20-20050915/</xspecref>. The latest version of <xspecref href="http://www.w3.org/TR/xslt20/">XSLT 2.0</xspecref> is available at http://www.w3.org/TR/xslt20/.</bibl></blist></inform-div1></back></spec>
