Difference between revisions of "Date manipulation"
(Creation of page) |
(→Solution) |
||
Line 17: | Line 17: | ||
Fōrmulæ is highly internationalized software. The task requires to parse a time from a string in very localized and specific format. We prefer to get a lower score for this task in favor to show a more general form: | Fōrmulæ is highly internationalized software. The task requires to parse a time from a string in very localized and specific format. We prefer to get a lower score for this task in favor to show a more general form: | ||
− | The initial time can be created as follows: | + | The initial time can be created with the [[Time.CreateTime|CreateTime]] expression as follows: (note than the hour is in 24-hour format) |
[[File:DateManiulation01.png|border]] | [[File:DateManiulation01.png|border]] | ||
Line 23: | Line 23: | ||
Explanation: | Explanation: | ||
− | [[Time.Time|Time]] expressions in Fōrmulæ are never related to | + | [[Time.Time|Time]] expressions in Fōrmulæ are never related to any time zone. When creating the expression, one is defined in order to provide the time zone the rest of time components are referred to. |
− | + | On the other hand, visualizing [[Time.Time|Time]] expressions needs to be always related to a time zone. It is always the current time zone (the time zone of the computer where the program is running on). For the example, it was run in a computer in [https://en.wikipedia.org/wiki/Pacific_Time_Zone pacific time zone], so the [[Time.Time|Time]] expression is shown (the '''blue''' rectangle) referred to it. | |
==== Step 2. Adding 12 hours ==== | ==== Step 2. Adding 12 hours ==== | ||
+ | |||
+ | We can get an new [[Time.Time|Time]] expression that is an offset of the first one, by the using of the [[ime.OffsetTime|OffsetTime]] expression: | ||
[[File:DateManiulation02.png|border]] | [[File:DateManiulation02.png|border]] | ||
+ | |||
+ | Is there any bug ? No. The daylight saving time (in the pacific time zone), for 2009 started March 8 at 2:00 AM<ref>[https://www.timeanddate.com/time/change/usa/los-angeles?year=2009 Clock changes in Los Angeles, California, USA in 2009]</ref>, at which time clock is adjusted 1 hour later. It occurred between the 12 hour addition of the example. | ||
+ | |||
+ | Please note that the first expressions text ends with PST (Pacific Standart Time), while the second one ends with PDT, or Pacific Daylight (Saving) Time. | ||
==== Step 3. Showing results ==== | ==== Step 3. Showing results ==== | ||
+ | |||
+ | So, if the visualization of [[Time.Time|Time]] expressions are always referred to the same time zone, how to show the time they represent referred to other time zones ? | ||
+ | |||
+ | We can format a [[Time.Time|Time]] expression specifying a [[Localization.TimeZone.TimeZone|TimeZone]] expression in three types: short, medium and large using the [[Localization.Format.Time.Gregorian.FormatTimeShort|FormatTimeShort]], [[Localization.Format.Time.Gregorian.FormatTimeMedium|FormatTimeMedium]] and [[Localization.Format.Time.Gregorian.FormatTimeLong|FormatTimeLong]] expressions, respectively. | ||
+ | |||
+ | Let us use them for the [https://en.wikipedia.org/wiki/Eastern_Time_Zone Eastern time zone]: | ||
[[File:DateManiulation03_01.png|border]] | [[File:DateManiulation03_01.png|border]] | ||
+ | |||
+ | Now, let us show the same point of time, but related to another time zone, for example the [https://en.wikipedia.org/wiki/Mountain_Time_Zone Mountain time zone]: | ||
[[File:DateManiulation03_02.png|border]] | [[File:DateManiulation03_02.png|border]] | ||
+ | |||
+ | You can even specify not only the time zone, but also a [[Localization.Locale.Locale|Locale]] expression to get the time according to how is shown in such that [https://en.wikipedia.org/wiki/Locale_(computer_software) locale]: | ||
[[File:DateManiulation03_03.png|border]] | [[File:DateManiulation03_03.png|border]] | ||
+ | |||
+ | <references /> | ||
[[Category:Rosetta Code's tasks]] | [[Category:Rosetta Code's tasks]] |
Revision as of 15:10, 20 July 2019
This page is the answer to the task Date manipulation in the Rosetta Code.
Contents
Description (from Rosetta Code)
Task
Given the date string "March 7 2009 7:30pm EST", output the time 12 hours later in any human-readable format. As extra credit, display the resulting time in a time zone different from your own. |
Solution
Step 1. Creating the initial time
Fōrmulæ is highly internationalized software. The task requires to parse a time from a string in very localized and specific format. We prefer to get a lower score for this task in favor to show a more general form:
The initial time can be created with the CreateTime expression as follows: (note than the hour is in 24-hour format)
Explanation:
Time expressions in Fōrmulæ are never related to any time zone. When creating the expression, one is defined in order to provide the time zone the rest of time components are referred to.
On the other hand, visualizing Time expressions needs to be always related to a time zone. It is always the current time zone (the time zone of the computer where the program is running on). For the example, it was run in a computer in pacific time zone, so the Time expression is shown (the blue rectangle) referred to it.
Step 2. Adding 12 hours
We can get an new Time expression that is an offset of the first one, by the using of the OffsetTime expression:
Is there any bug ? No. The daylight saving time (in the pacific time zone), for 2009 started March 8 at 2:00 AM[1], at which time clock is adjusted 1 hour later. It occurred between the 12 hour addition of the example.
Please note that the first expressions text ends with PST (Pacific Standart Time), while the second one ends with PDT, or Pacific Daylight (Saving) Time.
Step 3. Showing results
So, if the visualization of Time expressions are always referred to the same time zone, how to show the time they represent referred to other time zones ?
We can format a Time expression specifying a TimeZone expression in three types: short, medium and large using the FormatTimeShort, FormatTimeMedium and FormatTimeLong expressions, respectively.
Let us use them for the Eastern time zone:
Now, let us show the same point of time, but related to another time zone, for example the Mountain time zone:
You can even specify not only the time zone, but also a Locale expression to get the time according to how is shown in such that locale: