Date manipulation

From Fōrmulæ wiki
Jump to: navigation, search

This page is the answer to the task Date manipulation in the Rosetta Code.

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)

DateManiulation01.png

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:

DateManiulation02.png

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 text of the first expressions ends with PST (Pacific Standard 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:

DateManiulation03 01.png

Now, let us show the same point of time, but related to another time zone, for example the Mountain time zone:

DateManiulation03 02.png

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:

DateManiulation03 03.png

See also

References

  1. Clock changes in Los Angeles, California, USA in 2009