https://wiki.haskell.org/api.php?action=feedcontributions&user=EvanR&feedformat=atomHaskellWiki - User contributions [en]2020-04-09T20:46:09ZUser contributionsMediaWiki 1.27.4https://wiki.haskell.org/index.php?title=File:Time-diagram.png&diff=63145File:Time-diagram.png2019-11-21T19:37:58Z<p>EvanR: </p>
<hr />
<div>Map of types and transformations in Data.Time. https://github.com/evanrinehart/time-diagram</div>EvanRhttps://wiki.haskell.org/index.php?title=Time&diff=63140Time2019-11-21T18:56:51Z<p>EvanR: Add Time-diagram.png to page</p>
<hr />
<div>= Data Types for Measuring Time =<br />
<br />
[[File:Time-diagram.png]]<br />
<br />
The canonical time library for Haskell is the time package by Ashley Yakeley. There are several similar but different concepts associated with time and the time package keeps these different concepts separated by different data types with appropriate transformations between them to help keep your logic sound. However, if you are trying to use the wrong data type for the wrong task, you may find yourself frustrated with the time library.<br />
<br />
To help avoid frustration, this guide will describe the various data types available in the time library and what temporal concepts they represent. This will help make sure you are using the right data type for the right task. The first few data types in this guide are not widely used in practice, but it is important to understand their associated concepts before moving onto the more complex, widely used, data types. <br />
<br />
== AbsoluteTime ==<br />
<br />
<tt>AbsoluteTime</tt> is a data type is type for denoting when an event occurs. AbsoluteTime is a uniform time scale that simply counts the number of seconds since the TAI epoch. There are no leap seconds or timezones. You can compare two <tt>AbsoluteTime</tt>s directly to determine which one came first. You can reference time as far into the future or past as you like to within a resolution of 10<sup>-12</sup> seconds.<br />
<br />
Although <tt>AbsoluteTime</tt> is appropriate for many tasks, it is rarely used in applications and you will likely never use this data type. Unless you are a particularly pioneering person, you will probably want to use <tt>UTCTime</tt> which is more widely used in practice.<br />
<br />
== DiffTime ==<br />
<br />
<tt>DiffTime</tt> is used to represent an absolute duration of time measured in seconds. Subtracting two <tt>AbsoluteTime</tt>s with <tt>diffAbsoluteTime</tt> will produce a <tt>DiffTime</tt> and you can add a <tt>DiffTime</tt> to an <tt>AbsoluteTime</tt> to produce an offset <tt>AbsoluteTime</tt>.<br />
<br />
<tt>DiffTime</tt> is a member of the <tt>Num</tt> and <tt>Fractional</tt>, so can be generated by <tt>fromIntegral</tt> and <tt>realToFrac</tt>. <tt>DiffTime</tt> is also a member of <tt>RealFrac</tt> and <tt>Real</tt> so <tt>round</tt> and <tt>realToFrac</tt> can operate on it.<br />
<br />
<tt>DiffTime</tt> is appropriate in situations where precision time differences are required; however <tt>NominalDiffTime</tt> is more often used in practice.<br />
<br />
== UniversalTime ==<br />
<br />
The problem with <tt>AbsoluteTime</tt> is that Earth's rotation rate is not constant. Over the long term, solar noon would drift over time. <tt>UniversalTime</tt> measures time by counting the number of "solar" rotations, including fractions of a rotation, of the earth since the MJD (Modifed Julian Date) epoch.<br />
<br />
The variations in the Earth's rotation means that a difference in <tt>UniversalTime</tt> does not correspond to any particular amount of time. Your computer's hardware cannot track <tt>UniversalTime</tt> since it can only measure absolute durations. Converting between <tt>UniversalTime</tt> and <tt>AbsoluteTime</tt> would require a database build upon astronomical observations. No such conversion utilities are provided.<br />
<br />
<tt>UniversalTime</tt> is almost never used in practice.<br />
<br />
== UTCTime ==<br />
<br />
<tt>UTCTime</tt> measures "Coordinated Universal Time". One second of <tt>UTCTime</tt> is a constant amount of time equal to one second of <tt>AbsoluteTime</tt>. However, in order to keep "coordinated" with Universal Time, the number of seconds per day varies. Most days have 86400 seconds; however, on occasion a day will have a "leap second", meaning the day will consist of 86399 or 86401 seconds. <tt>UTCTime</tt> counts the number of days (each day consisting of between 86399-86401 seconds) since the MJD epoch plus an absolute time difference up to the number of seconds that occur in that day.<br />
<br />
It is unpredictable which days in the future will have a leap second. A leap second table is needed to convert between <tt>AbsoluteTime</tt> and <tt>UTCTime</tt>. This conversion can only be done reliably within recent history.<br />
<br />
<tt>UTCTime</tt> is the most popular way of referencing events in absolute time. Events occurring hundreds of years ago can be referenced with moderate precision because we may know approximately when during a particular day they occurred, even though we do not know the <tt>AbsoluteTime</tt> that they occurred (because we do not know the detailed historical changes in the rotation rate of the Earth). Events occurring in the recent past can be converted to <tt>AbsoluteTime</tt> with a leap second table. Future events are usually set relative to the day in which they are to occur, so the fact that the absolute time that they will occur at (that is exact the number seconds into the future that they will occur) is unknown is often not a problem.<br />
<br />
The major disadvantage with <tt>UTCTime</tt> is that one cannot compute the absolute difference between two <tt>UTCTime</tt>s without first converting them to <tt>AbsoluteTime</tt>, and that requires an accurate leap second tables. To circumvent this issue, one uses <tt>NominalDiffTime</tt>.<br />
<br />
== NominalDiffTime ==<br />
<br />
The <tt>diffUTCTime</tt> function computes a <tt>NominalDiffTime</tt> between two <tt>UTCTimes</tt> which is the number of seconds that would have occurred between two <tt>UTCTime</tt>s if no leap seconds had occurred between the two events. Therefore the <tt>diffUTCTime</tt> is not suitable for measuring the absolute time between two events to a high degree of precession. However a <tt>NominalDiffTime</tt> is often suitable for advancing an event by a week, a day, or an hour, since one would normally want said event to occur at the same time of day, or same minute of the hour, regardless of whether or not a leap second happens to be inserted in that duration.<br />
<br />
<tt>NominalDiffTime</tt> is a member of the <tt>Num</tt> and <tt>Fractional</tt>, so can be generated by <tt>fromIntegral</tt> and <tt>realToFrac</tt>. <tt>NominalDiffTime</tt> is also a member of <tt>RealFrac</tt> and <tt>Real</tt> so <tt>round</tt> and <tt>realToFrac</tt> can operate on it.<br />
<br />
<tt>NominialDiffTime</tt> is regularly used. In fact, <tt>POSIXTime</tt> is the <tt>NominalDiffTime</tt> since the UNIX epoch of Jan 1st, 1970, UTC.<br />
<br />
= Data Types for Denoting Time =<br />
<br />
So far our data have denoted occurrences of events by counting the number of days, or the number of seconds since some fixed reference epoch. However we usually tell time by using years, months, day of month, hours, minutes and seconds. The remain data types will assist with this more usual approach to telling time.<br />
<br />
== Day ==<br />
<br />
The <tt>Day</tt> data type is an abstract way of referring to a calender date. Due to time zone issues, the <tt>Day</tt> type does not necessarily refer to any specific 1-day long absolute time period.<br />
<br />
The standard calender used to reference a date is the Gregorian calendar. The <tt>toGregorian</tt> and <tt>fromGregorian</tt> functions will construct and deconstruct a <tt>Day</tt> from the usual year-month-day format. There are other calendars, such as the Julian calendar, which are only used in unusual situations.<br />
<br />
== TimeOfDay ==<br />
<br />
<tt>TimeOfDay</tt> represents "wall-clock" time. It consists of an hour of the day, a minute of the hour, and a second of the minute, including fractions of a second up to a precession of 10-12. The seconds could go up to 61 in order to accommodate leap seconds.<br />
<br />
== LocalTime ==<br />
<br />
A <tt>Day</tt> with a <tt>TimeOfDay</tt> forms a <tt>LocalTime</tt>. This is contains the data that we normally denote time with: a year, month, day, hour, minute, and second. However, it does not denote an absolute time because it contains no time zone information.<br />
<br />
<tt>LocalTime</tt> is used represent date and time for printing and parsing; however it should be avoided for computation. The biggest problem with <tt>LocalTime</tt> is that, due to daylight savings time, regularly the same time of day is repeated twice a day. This means you cannot even use <tt>LocalTime</tt>s to reliably determine the relative order of events.<br />
<br />
== TimeZone ==<br />
<br />
Somewhat confusingly named, a <tt>TimeZone</tt> represents an offset from UTC measured in minutes. It also contains ancillary data such as the timezone name and whether or not it is intended to be used for summer time only. Eastern Standard Time (EST) or Pacific Daylight Time (PDT) are examples of timezone that could be represented by a <tt>TimeZone</tt> value. <br />
<br />
It does not represent the time zone of a region, such as Eastern Time, because regions often vary their offsets from UTC depending on the time of year.<br />
<br />
The <tt>utc</tt> value is the value of the <tt>TimeZone</tt> with no offset from UTC.<br />
<br />
== ZonedTime == <br />
<br />
A <tt>ZonedTime</tt> is a <tt>LocalTime</tt> together with a <tt>TimeZone</tt>. This does refer to a specific event in absolute time and can be converted to a <tt>UTCTime</tt> with the <tt>zoneTimeToUTC</tt> function, and could be further converted into a <tt>AbsoluteTime</tt> if one has a leap second table.<br />
<br />
There are, of course, many different <tt>ZonedTime</tt> values corresponding to the same absolute, <tt>UTCTime</tt>. Given a <tt>TimeZone</tt> one can create a <tt>ZonedTime</tt> with that <tt>TimeZone</tt> by using the <tt>utcToZonedTime</tt> function.<br />
<br />
<tt>ZonedTime</tt> is a common stop when converting between common year-month-day-hour-minute-second human time format and absolute <tt>UTCTime</tt> time.<br />
<br />
== TimeZoneSeries ==<br />
<br />
Almost nothing in the time library deals with regional time zones such as "Eastern Time" or "Pacific time" For that we need to reach out to the timezone-series and timezone-olson packages. The difficulty with regional time zones is, like leap seconds, that they are unpredictable and subject to be changed. <br />
<br />
A <tt>TimeZoneSeries</tt> is a series of <tt>TimeZone</tt>s, that is offsets from UTC, along with the absolute times where those <tt>TimeZone</tt>s are in effect. This is a suitable structure for defining a regional time zone. The functions in the timezone-olson package will allow you to create a <tt>TimeZoneSeries</tt> from an Olson time zone database file.<br />
<br />
== getTimeZone ==<br />
<br />
There there is one set of functions in the time package that does deal with regional time zones. The <tt>getTimeZone</tt> function will compute the timezone for a given, <tt>UTCTime</tt>, but only the local region as set by the <tt>TZ</tt> environment variable. The <tt>getCurrentTimeZone</tt>, <tt>getZonedTime</tt>, and <tt>utcToLocalZonedTime</tt> are functions derived from <tt>getTimeZone</tt>.<br />
<br />
There is no functionality in the time package that will convert from a local regional time to <tt>UTCTime</tt>, owing to the fact that the same local regional time may refer to multiple different <tt>UTCTime</tt> due to daylight savings times. <tt>localTimeToUTC'</tt> from the <tt>timezone-series</tt> packages will do this conversion given a <tt>TimeZoneSeries</tt>.</div>EvanRhttps://wiki.haskell.org/index.php?title=File:Time-diagram.png&diff=63139File:Time-diagram.png2019-11-21T18:55:17Z<p>EvanR: Map of types and transformations in Data.Time</p>
<hr />
<div>Map of types and transformations in Data.Time</div>EvanRhttps://wiki.haskell.org/index.php?title=Time&diff=63138Time2019-11-21T18:48:04Z<p>EvanR: </p>
<hr />
<div>= Data Types for Measuring Time =<br />
<br />
The canonical time library for Haskell is the time package by Ashley Yakeley. There are several similar but different concepts associated with time and the time package keeps these different concepts separated by different data types with appropriate transformations between them to help keep your logic sound. However, if you are trying to use the wrong data type for the wrong task, you may find yourself frustrated with the time library.<br />
<br />
To help avoid frustration, this guide will describe the various data types available in the time library and what temporal concepts they represent. This will help make sure you are using the right data type for the right task. The first few data types in this guide are not widely used in practice, but it is important to understand their associated concepts before moving onto the more complex, widely used, data types. <br />
<br />
== AbsoluteTime ==<br />
<br />
<tt>AbsoluteTime</tt> is a data type is type for denoting when an event occurs. AbsoluteTime is a uniform time scale that simply counts the number of seconds since the TAI epoch. There are no leap seconds or timezones. You can compare two <tt>AbsoluteTime</tt>s directly to determine which one came first. You can reference time as far into the future or past as you like to within a resolution of 10<sup>-12</sup> seconds.<br />
<br />
Although <tt>AbsoluteTime</tt> is appropriate for many tasks, it is rarely used in applications and you will likely never use this data type. Unless you are a particularly pioneering person, you will probably want to use <tt>UTCTime</tt> which is more widely used in practice.<br />
<br />
== DiffTime ==<br />
<br />
<tt>DiffTime</tt> is used to represent an absolute duration of time measured in seconds. Subtracting two <tt>AbsoluteTime</tt>s with <tt>diffAbsoluteTime</tt> will produce a <tt>DiffTime</tt> and you can add a <tt>DiffTime</tt> to an <tt>AbsoluteTime</tt> to produce an offset <tt>AbsoluteTime</tt>.<br />
<br />
<tt>DiffTime</tt> is a member of the <tt>Num</tt> and <tt>Fractional</tt>, so can be generated by <tt>fromIntegral</tt> and <tt>realToFrac</tt>. <tt>DiffTime</tt> is also a member of <tt>RealFrac</tt> and <tt>Real</tt> so <tt>round</tt> and <tt>realToFrac</tt> can operate on it.<br />
<br />
<tt>DiffTime</tt> is appropriate in situations where precision time differences are required; however <tt>NominalDiffTime</tt> is more often used in practice.<br />
<br />
== UniversalTime ==<br />
<br />
The problem with <tt>AbsoluteTime</tt> is that Earth's rotation rate is not constant. Over the long term, solar noon would drift over time. <tt>UniversalTime</tt> measures time by counting the number of "solar" rotations, including fractions of a rotation, of the earth since the MJD (Modifed Julian Date) epoch.<br />
<br />
The variations in the Earth's rotation means that a difference in <tt>UniversalTime</tt> does not correspond to any particular amount of time. Your computer's hardware cannot track <tt>UniversalTime</tt> since it can only measure absolute durations. Converting between <tt>UniversalTime</tt> and <tt>AbsoluteTime</tt> would require a database build upon astronomical observations. No such conversion utilities are provided.<br />
<br />
<tt>UniversalTime</tt> is almost never used in practice.<br />
<br />
== UTCTime ==<br />
<br />
<tt>UTCTime</tt> measures "Coordinated Universal Time". One second of <tt>UTCTime</tt> is a constant amount of time equal to one second of <tt>AbsoluteTime</tt>. However, in order to keep "coordinated" with Universal Time, the number of seconds per day varies. Most days have 86400 seconds; however, on occasion a day will have a "leap second", meaning the day will consist of 86399 or 86401 seconds. <tt>UTCTime</tt> counts the number of days (each day consisting of between 86399-86401 seconds) since the MJD epoch plus an absolute time difference up to the number of seconds that occur in that day.<br />
<br />
It is unpredictable which days in the future will have a leap second. A leap second table is needed to convert between <tt>AbsoluteTime</tt> and <tt>UTCTime</tt>. This conversion can only be done reliably within recent history.<br />
<br />
<tt>UTCTime</tt> is the most popular way of referencing events in absolute time. Events occurring hundreds of years ago can be referenced with moderate precision because we may know approximately when during a particular day they occurred, even though we do not know the <tt>AbsoluteTime</tt> that they occurred (because we do not know the detailed historical changes in the rotation rate of the Earth). Events occurring in the recent past can be converted to <tt>AbsoluteTime</tt> with a leap second table. Future events are usually set relative to the day in which they are to occur, so the fact that the absolute time that they will occur at (that is exact the number seconds into the future that they will occur) is unknown is often not a problem.<br />
<br />
The major disadvantage with <tt>UTCTime</tt> is that one cannot compute the absolute difference between two <tt>UTCTime</tt>s without first converting them to <tt>AbsoluteTime</tt>, and that requires an accurate leap second tables. To circumvent this issue, one uses <tt>NominalDiffTime</tt>.<br />
<br />
== NominalDiffTime ==<br />
<br />
The <tt>diffUTCTime</tt> function computes a <tt>NominalDiffTime</tt> between two <tt>UTCTimes</tt> which is the number of seconds that would have occurred between two <tt>UTCTime</tt>s if no leap seconds had occurred between the two events. Therefore the <tt>diffUTCTime</tt> is not suitable for measuring the absolute time between two events to a high degree of precession. However a <tt>NominalDiffTime</tt> is often suitable for advancing an event by a week, a day, or an hour, since one would normally want said event to occur at the same time of day, or same minute of the hour, regardless of whether or not a leap second happens to be inserted in that duration.<br />
<br />
<tt>NominalDiffTime</tt> is a member of the <tt>Num</tt> and <tt>Fractional</tt>, so can be generated by <tt>fromIntegral</tt> and <tt>realToFrac</tt>. <tt>NominalDiffTime</tt> is also a member of <tt>RealFrac</tt> and <tt>Real</tt> so <tt>round</tt> and <tt>realToFrac</tt> can operate on it.<br />
<br />
<tt>NominialDiffTime</tt> is regularly used. In fact, <tt>POSIXTime</tt> is the <tt>NominalDiffTime</tt> since the UNIX epoch of Jan 1st, 1970, UTC.<br />
<br />
= Data Types for Denoting Time =<br />
<br />
So far our data have denoted occurrences of events by counting the number of days, or the number of seconds since some fixed reference epoch. However we usually tell time by using years, months, day of month, hours, minutes and seconds. The remain data types will assist with this more usual approach to telling time.<br />
<br />
== Day ==<br />
<br />
The <tt>Day</tt> data type is an abstract way of referring to a calender date. Due to time zone issues, the <tt>Day</tt> type does not necessarily refer to any specific 1-day long absolute time period.<br />
<br />
The standard calender used to reference a date is the Gregorian calendar. The <tt>toGregorian</tt> and <tt>fromGregorian</tt> functions will construct and deconstruct a <tt>Day</tt> from the usual year-month-day format. There are other calendars, such as the Julian calendar, which are only used in unusual situations.<br />
<br />
== TimeOfDay ==<br />
<br />
<tt>TimeOfDay</tt> represents "wall-clock" time. It consists of an hour of the day, a minute of the hour, and a second of the minute, including fractions of a second up to a precession of 10-12. The seconds could go up to 61 in order to accommodate leap seconds.<br />
<br />
== LocalTime ==<br />
<br />
A <tt>Day</tt> with a <tt>TimeOfDay</tt> forms a <tt>LocalTime</tt>. This is contains the data that we normally denote time with: a year, month, day, hour, minute, and second. However, it does not denote an absolute time because it contains no time zone information.<br />
<br />
<tt>LocalTime</tt> is used represent date and time for printing and parsing; however it should be avoided for computation. The biggest problem with <tt>LocalTime</tt> is that, due to daylight savings time, regularly the same time of day is repeated twice a day. This means you cannot even use <tt>LocalTime</tt>s to reliably determine the relative order of events.<br />
<br />
== TimeZone ==<br />
<br />
Somewhat confusingly named, a <tt>TimeZone</tt> represents an offset from UTC measured in minutes. It also contains ancillary data such as the timezone name and whether or not it is intended to be used for summer time only. Eastern Standard Time (EST) or Pacific Daylight Time (PDT) are examples of timezone that could be represented by a <tt>TimeZone</tt> value. <br />
<br />
It does not represent the time zone of a region, such as Eastern Time, because regions often vary their offsets from UTC depending on the time of year.<br />
<br />
The <tt>utc</tt> value is the value of the <tt>TimeZone</tt> with no offset from UTC.<br />
<br />
== ZonedTime == <br />
<br />
A <tt>ZonedTime</tt> is a <tt>LocalTime</tt> together with a <tt>TimeZone</tt>. This does refer to a specific event in absolute time and can be converted to a <tt>UTCTime</tt> with the <tt>zoneTimeToUTC</tt> function, and could be further converted into a <tt>AbsoluteTime</tt> if one has a leap second table.<br />
<br />
There are, of course, many different <tt>ZonedTime</tt> values corresponding to the same absolute, <tt>UTCTime</tt>. Given a <tt>TimeZone</tt> one can create a <tt>ZonedTime</tt> with that <tt>TimeZone</tt> by using the <tt>utcToZonedTime</tt> function.<br />
<br />
<tt>ZonedTime</tt> is a common stop when converting between common year-month-day-hour-minute-second human time format and absolute <tt>UTCTime</tt> time.<br />
<br />
== TimeZoneSeries ==<br />
<br />
Almost nothing in the time library deals with regional time zones such as "Eastern Time" or "Pacific time" For that we need to reach out to the timezone-series and timezone-olson packages. The difficulty with regional time zones is, like leap seconds, that they are unpredictable and subject to be changed. <br />
<br />
A <tt>TimeZoneSeries</tt> is a series of <tt>TimeZone</tt>s, that is offsets from UTC, along with the absolute times where those <tt>TimeZone</tt>s are in effect. This is a suitable structure for defining a regional time zone. The functions in the timezone-olson package will allow you to create a <tt>TimeZoneSeries</tt> from an Olson time zone database file.<br />
<br />
== getTimeZone ==<br />
<br />
There there is one set of functions in the time package that does deal with regional time zones. The <tt>getTimeZone</tt> function will compute the timezone for a given, <tt>UTCTime</tt>, but only the local region as set by the <tt>TZ</tt> environment variable. The <tt>getCurrentTimeZone</tt>, <tt>getZonedTime</tt>, and <tt>utcToLocalZonedTime</tt> are functions derived from <tt>getTimeZone</tt>.<br />
<br />
There is no functionality in the time package that will convert from a local regional time to <tt>UTCTime</tt>, owing to the fact that the same local regional time may refer to multiple different <tt>UTCTime</tt> due to daylight savings times. <tt>localTimeToUTC'</tt> from the <tt>timezone-series</tt> packages will do this conversion given a <tt>TimeZoneSeries</tt>.</div>EvanR