Date manipulation: Difference between revisions

Content added Content deleted
m ({{out}})
Line 1: Line 1:
{{task|Text processing}}
{{task|Text processing}}
Given the date string "March 7 2009 7:30pm EST", output the time 12 hours later in any human-readable format.
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.
As extra credit, display the resulting time in a time zone different from your own.

=={{header|Ada}}==
=={{header|Ada}}==
The Ada way: long, type-based, clear, reliable.
The Ada way: long, type-based, clear, reliable.
Line 186: Line 188:
Return, Result
Return, Result
}</lang>
}</lang>
Message box shows:
{{out|Message box shows}}
<pre>Given: March 7 2009 7:30pm EST
<pre>Given: March 7 2009 7:30pm EST


Line 214: Line 216:
}
}
</lang>
</lang>
{{out}}
<p>output:</p>
<pre>
<pre>
time: Sat 2009-03-07 19:30:00 Eastern Standard Time
time: Sat 2009-03-07 19:30:00 Eastern Standard Time
+12 hrs: Sun 2009-03-08 08:30:00 Eastern Daylight Time
+12 hrs: Sun 2009-03-08 08:30:00 Eastern Daylight Time
</pre>
</pre>

=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
Line 254: Line 257:
ENDPROC
ENDPROC
</lang>
</lang>
{{out}}
Output:
<pre>March 8 2009 7:30am EST
<pre>March 8 2009 7:30am EST
March 8 2009 12:30pm GMT
March 8 2009 12:30pm GMT
Line 371: Line 374:
}
}
</lang>
</lang>
{{out}}
this produces the following output:
<pre>
<pre>
local time: 2009-Mar-07 19:30:00 EST
local time: 2009-Mar-07 19:30:00 EST
Line 477: Line 480:
</lang>
</lang>


{{out}}
'''The output of the Delphi program is:''' "03/08/2009 07:30 AM"
"03/08/2009 07:30 AM"


----
----
Line 543: Line 547:
printf(1, "%s EST\n", {format(dt, "%B %d %Y %I:%M %p")})
printf(1, "%s EST\n", {format(dt, "%B %d %Y %I:%M %p")})
</lang>
</lang>
{{out}}
Output
<pre>
<pre>
March 08 2009 07:30 AM EST
March 08 2009 07:30 AM EST
Line 566: Line 570:
main()</lang>
main()</lang>


Output (depends on locale settings):
{{out}} (depends on locale settings):
<pre>Original date in local time : 08.03.2009 01:30:00
<pre>Original date in local time : 08.03.2009 01:30:00
Original date in EST : 07.03.2009 19:30:00
Original date in EST : 07.03.2009 19:30:00
Line 587: Line 591:


=={{header|Frink}}==
=={{header|Frink}}==
Frink parses a large number of date/time formats, has robust date/time math, and automatically converts between timezones. By default, output times are in the user's defined timezone.
Frink parses a large number of date/time formats, has robust date/time math,
and automatically converts between timezones.
By default, output times are in the user's defined timezone.
<lang frink>
<lang frink>
### MMM dd yyyy h:mma ###
### MMM dd yyyy h:mma ###
Line 595: Line 601:
</lang>
</lang>


{{out}}
Output is:

<pre>
<pre>
AD 2009-03-08 AM 08:30:00.000 (Sun) Eastern Daylight Time
AD 2009-03-08 AM 08:30:00.000 (Sun) Eastern Daylight Time
Line 675: Line 680:
println (dt.plusHours(12).withZone(DateTimeZone.UTC))</lang>
println (dt.plusHours(12).withZone(DateTimeZone.UTC))</lang>


{{out}}
Output:
<pre>2009-03-07T18:30:00.000-06:00
<pre>2009-03-07T18:30:00.000-06:00
2009-03-08T07:30:00.000-05:00
2009-03-08T07:30:00.000-05:00
Line 779: Line 784:
[http://www.cs.arizona.edu/icon/library/src/procs/datetime.icn datetime provides SecToDateLine, and DateLineToSec] these convert between Icon's &dateline format and seconds from a configurable base date (which defaults to the normal 1970 epoch).
[http://www.cs.arizona.edu/icon/library/src/procs/datetime.icn datetime provides SecToDateLine, and DateLineToSec] these convert between Icon's &dateline format and seconds from a configurable base date (which defaults to the normal 1970 epoch).


{{out}}
Output:<pre>input = March 7 2009 7:30pm EST
<pre>input = March 7 2009 7:30pm EST
+12 hours = Sunday, March 8, 2009 7:30 am EST
+12 hours = Sunday, March 8, 2009 7:30 am EST
= Sunday, March 8, 2009 12:30 pm UTC
= Sunday, March 8, 2009 12:30 pm UTC
Line 801: Line 807:


}</lang>
}</lang>
{{out}}
Output:
<pre>March 8 2009 8:30AM EDT</pre>
<pre>March 8 2009 8:30AM EDT</pre>
or using <tt>System.out.println(date);</tt> as the last line:
or using <tt>System.out.println(date);</tt> as the last line:
Line 811: Line 817:
Input: March 7 2009 7:30pm EST
Input: March 7 2009 7:30pm EST


The input string is ambiguous since EST might represent any one of 3 different world time zones. Will assume US Eastern Standard Time of UTC -5 hours.
The input string is ambiguous since EST might represent
any one of 3 different world time zones.
Will assume US Eastern Standard Time of UTC -5 hours.


Javascript date objects are always in the local time zone.
Javascript date objects are always in the local time zone. If a date and time is provided in a different time zone, it must be dealt with manually as the date object's time zone offset is read only. Consequently, there may be issues if daylight saving is observed in one location but not the other.
If a date and time is provided in a different time zone, it must be dealt with manually as the date object's time zone offset is read only.
Consequently, there may be issues if daylight saving is observed in one location but not the other.


While ECMA-262 Ed 5 specifies a <code>Date.parse</code> method, it is not widely supported (2011) and parsing of strings other than the format specified are implementation dependent. Since the test string doesn't conform to the standard, it must be manually parsed.
While ECMA-262 Ed 5 specifies a <code>Date.parse</code> method, it is not widely supported (2011) and parsing of strings other than the format specified are implementation dependent. Since the test string doesn't conform to the standard, it must be manually parsed.
Line 897: Line 907:
print( os.date( "%c", os.time{ year=year, month=month, day=day, hour=hour, min=min, sec=0 } + 12 * 3600 ) )
print( os.date( "%c", os.time{ year=year, month=month, day=day, hour=hour, min=min, sec=0 } + 12 * 3600 ) )
</lang>
</lang>
{{out}}
Output:
<pre>Sun Mar 8 07:30:00 2009</pre>
<pre>Sun Mar 8 07:30:00 2009</pre>


Line 938: Line 948:
return
return
</lang>
</lang>
{{out}}
'''Output:'''
<pre>
<pre>
March 7 2009 7:30pm EST
March 7 2009 7:30pm EST
Line 951: Line 961:
ts.tmHour += 12
ts.tmHour += 12
echo ts.mktime</lang>
echo ts.mktime</lang>
{{out}}
Output:
<pre>Sun Mar 8 07:30:00 2009</pre>
<pre>Sun Mar 8 07:30:00 2009</pre>


Line 1,305: Line 1,315:
*/
*/
</lang>
</lang>
{{out}}
;Output
<pre>
<pre>
Manipulate the date string "March 7 2009 7:30pm EST" and present results in ISO 8601 timestamp format:
Manipulate the date string "March 7 2009 7:30pm EST" and present results in ISO 8601 timestamp format:
Line 1,345: Line 1,355:
If we're given an ambiguous timezone like 'EST' for input, we can handle this by changing it to the unambiguous Olson timezone id. This ensures daylight savings is correctly handled (which is especially tricky here, since March 7/8 is the DST rollover, and times jump ahead skipping an hour)
If we're given an ambiguous timezone like 'EST' for input, we can handle this by changing it to the unambiguous Olson timezone id. This ensures daylight savings is correctly handled (which is especially tricky here, since March 7/8 is the DST rollover, and times jump ahead skipping an hour)


{{out}}
Output:
<pre>March 8 2009 6:30AM MDT</pre>
<pre>March 8 2009 6:30AM MDT</pre>

=={{header|Perl 6}}==
=={{header|Perl 6}}==
Perl 6 comes with a build-in DateTime type to support most aspects of standard civic time calculation that are not dependent on cultural idiosyncracies. Unfortunately,
Perl 6 comes with a build-in DateTime type
to support most aspects of standard civic time calculation
Perl 6 does not yet have a date parsing module (mostly due to a reticence to inflict Western cultural imperialism on other cultures...or maybe just due to laziness), but that just gives us another opportunity to demonstrate the built-in grammar support.
that are not dependent on cultural idiosyncracies. <br>
Unfortunately, Perl 6 does not yet have a date parsing module
(mostly due to a reticence to inflict Western cultural imperialism on other cultures...
or maybe just due to laziness), but that just gives us another opportunity to demonstrate the built-in grammar support.


<lang perl6>my @month = <January February March April May June July August September October November December>;
<lang perl6>my @month = <January February March April May June July August September October November December>;
Line 1,455: Line 1,470:
write-host $date.AddHours(12)
write-host $date.AddHours(12)
write-host [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($date.AddHours(12),"Vladivostok Standard Time")</lang>
write-host [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($date.AddHours(12),"Vladivostok Standard Time")</lang>
Output (depends on user regional settings):
{{out}} (depends on user regional settings):
<pre>domingo, 08 de marzo de 2009 1:30:00
<pre>domingo, 08 de marzo de 2009 1:30:00
domingo, 08 de marzo de 2009 13:30:00
domingo, 08 de marzo de 2009 13:30:00
Line 1,504: Line 1,519:
"~a ~d ~b ~Y ~H:~M")
"~a ~d ~b ~Y ~H:~M")
</lang>
</lang>
{{out}}
Output:
<lang racket>
<lang racket>
"Sun 08 Mar 2009 07:30"
"Sun 08 Mar 2009 07:30"
Line 1,538: Line 1,553:
</lang>
</lang>


{{out}}
Output:

<pre>8-Mar-2009/7:30-5:00</pre>
<pre>8-Mar-2009/7:30-5:00</pre>


Line 1,557: Line 1,571:
say aDate ' + 12 hours ───► ' ndate ntime tz /*display new timestamp.*/
say aDate ' + 12 hours ───► ' ndate ntime tz /*display new timestamp.*/
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</lang>
{{out}}
'''output'''
<pre style="overflow:scroll">
<pre style="overflow:scroll">
March 7 2009 7:30pm EST + 12 hours ───► 8 Mar 2009 7:30am EST
March 7 2009 7:30pm EST + 12 hours ───► 8 Mar 2009 7:30am EST
Line 1,585: Line 1,599:
puts remote.rfc2822
puts remote.rfc2822
puts remote.zone</lang>
puts remote.zone</lang>
{{out}}
outputs
<pre>Sat, 07 Mar 2009 19:30:00 -0500
<pre>Sat, 07 Mar 2009 19:30:00 -0500
EST
EST
Line 1,636: Line 1,650:
}</lang>
}</lang>


{{out}}
Output:
<pre>March 8 2009 8:30AM EDT
<pre>March 8 2009 8:30AM EDT
March 8 2009 12:30PM GMT</pre>
March 8 2009 12:30PM GMT</pre>
Line 1,694: Line 1,708:
end func;</lang>
end func;</lang>


{{out}}
Output:
<pre>
<pre>
Given: 2009-03-07 19:30:00 UTC-5
Given: 2009-03-07 19:30:00 UTC-5
Line 1,793: Line 1,807:
(dt dateTime) asUTC displayNl.</lang>
(dt dateTime) asUTC displayNl.</lang>


Output example (note that EST should be EDT):
{{out}} (note that EST should be EDT):

<pre>March 8 2009 8:30AM EST
<pre>March 8 2009 8:30AM EST
2009-03-08T13:30:00+00:00</pre>
2009-03-08T13:30:00+00:00</pre>
Line 1,814: Line 1,827:
TZ=Asia/Shanghai date -d @$epoch</lang>
TZ=Asia/Shanghai date -d @$epoch</lang>


{{out}}
output:
<pre>Sun Mar 8 08:30:00 EDT 2009
<pre>Sun Mar 8 08:30:00 EDT 2009
Sun Mar 8 20:30:00 CST 2009</pre>
Sun Mar 8 20:30:00 CST 2009</pre>