Date manipulation: Difference between revisions
Content added Content deleted
(Add Swift) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 12: | Line 12: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">V format_str = ‘%B %d %Y %I:%M%p’ |
||
print((time:strptime(‘March 7 2009 7:30pm’, format_str) |
print((time:strptime(‘March 7 2009 7:30pm’, format_str) |
||
+ TimeDelta(hours' 12)).strftime(format_str))</ |
+ TimeDelta(hours' 12)).strftime(format_str))</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 21: | Line 21: | ||
Only standard libraries are required. |
Only standard libraries are required. |
||
< |
<syntaxhighlight lang="ada">with Ada.Calendar; |
||
with Ada.Calendar.Formatting; |
with Ada.Calendar.Formatting; |
||
with Ada.Calendar.Time_Zones; |
with Ada.Calendar.Time_Zones; |
||
Line 117: | Line 117: | ||
Put_Line ("T1 => " & Img (T1, EST) & " = " & Img (T1, Lisbon)); |
Put_Line ("T1 => " & Img (T1, EST) & " = " & Img (T1, Lisbon)); |
||
Put_Line ("T2 => " & Img (T2, EST) & " = " & Img (T2, Lisbon)); |
Put_Line ("T2 => " & Img (T2, EST) & " = " & Img (T2, Lisbon)); |
||
end;</ |
end;</syntaxhighlight> |
||
Result: |
Result: |
||
Line 127: | Line 127: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
AppleScript has a built-in date class and can coerce a string to a date automatically. It also has reserved constants such as <code>hours</code> which are defined in the unit of seconds. There is no built-in support for time zones. |
AppleScript has a built-in date class and can coerce a string to a date automatically. It also has reserved constants such as <code>hours</code> which are defined in the unit of seconds. There is no built-in support for time zones. |
||
< |
<syntaxhighlight lang="applescript">set x to "March 7 2009 7:30pm EST" |
||
return (date x) + 12 * hours</ |
return (date x) + 12 * hours</syntaxhighlight> |
||
Result is: |
Result is: |
||
< |
<syntaxhighlight lang="applescript">date "Sunday, March 8, 2009 7:30:00 AM"</syntaxhighlight> |
||
The above is problematical in that: |
The above is problematical in that: |
||
Line 140: | Line 140: | ||
However, AppleScript can run shell scripts and, more recently, access some of the system's Objective-C API through its hybrid form AppleScriptObjectiveC. So as long as the date format's known, the task is doable: |
However, AppleScript can run shell scripts and, more recently, access some of the system's Objective-C API through its hybrid form AppleScriptObjectiveC. So as long as the date format's known, the task is doable: |
||
< |
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later |
||
use framework "Foundation" |
use framework "Foundation" |
||
Line 161: | Line 161: | ||
end dateManipulationTask |
end dateManipulationTask |
||
dateManipulationTask()</ |
dateManipulationTask()</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 168: | Line 168: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">; a tiny helper, so that we aren't too repetitive |
||
formatDate: function [dt][ |
formatDate: function [dt][ |
||
to :string .format: "MMMM d yyyy h:mmtt" dt |
to :string .format: "MMMM d yyyy h:mmtt" dt |
||
Line 180: | Line 180: | ||
print ["initial:" formatDate initial] |
print ["initial:" formatDate initial] |
||
print ["after 12 hours:" formatDate after.hours:12 initial]</ |
print ["after 12 hours:" formatDate after.hours:12 initial]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 188: | Line 188: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">DateString := "March 7 2009 7:30pm EST" |
||
; split the given string with RegExMatch |
; split the given string with RegExMatch |
||
Line 252: | Line 252: | ||
Result := "12" |
Result := "12" |
||
Return, Result |
Return, Result |
||
}</ |
}</syntaxhighlight> |
||
{{out|Message box shows}} |
{{out|Message box shows}} |
||
<pre>Given: March 7 2009 7:30pm EST |
<pre>Given: March 7 2009 7:30pm EST |
||
Line 261: | Line 261: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f DATE_MANIPULATION.AWK |
# syntax: GAWK -f DATE_MANIPULATION.AWK |
||
BEGIN { |
BEGIN { |
||
Line 280: | Line 280: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 288: | Line 288: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos"> |
||
@echo off |
@echo off |
||
Line 370: | Line 370: | ||
echo Manipulated - %monthname% %day% %year% %hour%:%minutes%%ampm% %timezone% |
echo Manipulated - %monthname% %day% %year% %hour%:%minutes%%ampm% %timezone% |
||
exit /b |
exit /b |
||
</syntaxhighlight> |
|||
</lang> |
|||
The code takes 3 inputs to demonstrate the ability to deal with leap years. |
The code takes 3 inputs to demonstrate the ability to deal with leap years. |
||
Line 392: | Line 392: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"DATELIB" |
||
date$ = "March 7 2009 7:30pm EST" |
date$ = "March 7 2009 7:30pm EST" |
||
Line 424: | Line 424: | ||
\ ":" + RIGHT$("0"+STR$(mins%), 2) + ampm$ + " " + zone$ |
\ ":" + RIGHT$("0"+STR$(mins%), 2) + ampm$ + " " + zone$ |
||
ENDPROC |
ENDPROC |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>March 8 2009 7:30am EST |
<pre>March 8 2009 7:30am EST |
||
Line 432: | Line 432: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{works with|POSIX}} |
{{works with|POSIX}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <time.h> |
#include <time.h> |
||
Line 450: | Line 450: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</syntaxhighlight> |
||
Note: <code>ctime</code> treats the date as local, so that it is like the timezone information were discarded (to see the passage to daylight saving time I must change the date into March 28... no matter the timezone specified) |
Note: <code>ctime</code> treats the date as local, so that it is like the timezone information were discarded (to see the passage to daylight saving time I must change the date into March 28... no matter the timezone specified) |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">class Program |
||
{ |
{ |
||
static void Main(string[] args) |
static void Main(string[] args) |
||
Line 468: | Line 468: | ||
Console.ReadLine(); |
Console.ReadLine(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 474: | Line 474: | ||
compiled with g++ -lboost_date_time |
compiled with g++ -lboost_date_time |
||
< |
<syntaxhighlight lang="cpp">#include <string> |
||
#include <iostream> |
#include <iostream> |
||
#include <boost/date_time/local_time/local_time.hpp> |
#include <boost/date_time/local_time/local_time.hpp> |
||
Line 541: | Line 541: | ||
return 0 ; |
return 0 ; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 551: | Line 551: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(import java.util.Date |
||
java.text.SimpleDateFormat) |
java.text.SimpleDateFormat) |
||
Line 561: | Line 561: | ||
long |
long |
||
(Date. ,) |
(Date. ,) |
||
(->> , (.format sdf ,)))))</ |
(->> , (.format sdf ,)))))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Line 568: | Line 568: | ||
Two parts to this example. Following the task spec using POSIX routines, and a more standardized COBOL form. COBOL 2014 uses ISO8601 date and time formats, and these formats may be more common in COBOL applications. |
Two parts to this example. Following the task spec using POSIX routines, and a more standardized COBOL form. COBOL 2014 uses ISO8601 date and time formats, and these formats may be more common in COBOL applications. |
||
< |
<syntaxhighlight lang="cobol"> identification division. |
||
program-id. date-manipulation. |
program-id. date-manipulation. |
||
Line 689: | Line 689: | ||
. |
. |
||
end program date-manipulation. |
end program date-manipulation. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 704: | Line 704: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
< |
<syntaxhighlight lang="ruby">time = Time.parse("March 7 2009 7:30pm EST", "%B %-d %Y %l:%M%p", Time::Location.load("EST")) |
||
time += 12.hours |
time += 12.hours |
||
puts time # 2009-03-08 07:30:00 -05:00 |
puts time # 2009-03-08 07:30:00 -05:00 |
||
puts time.in(Time::Location.load("Europe/Berlin")) # 2009-03-08 13:30:00 +01:00 |
puts time.in(Time::Location.load("Europe/Berlin")) # 2009-03-08 13:30:00 +01:00 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang="d"> |
|||
<lang d> |
|||
import std.stdio; |
import std.stdio; |
||
import std.format; |
import std.format; |
||
Line 744: | Line 744: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 752: | Line 752: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program DateManipulation; |
program DateManipulation; |
||
Line 834: | Line 834: | ||
Readln; |
Readln; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 840: | Line 840: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define my-date (string->date "March 7 2009 7:30 pm EST")) |
(define my-date (string->date "March 7 2009 7:30 pm EST")) |
||
→ Sun Mar 08 2009 01:30:00 GMT+0100 (CET) |
→ Sun Mar 08 2009 01:30:00 GMT+0100 (CET) |
||
Line 850: | Line 850: | ||
(date->string my-date) |
(date->string my-date) |
||
→ "8/3/2009 13:30:00" ;; human localized, Paris time. |
→ "8/3/2009 13:30:00" ;; human localized, Paris time. |
||
</syntaxhighlight> |
|||
</lang> |
|||
---- |
---- |
||
Line 856: | Line 856: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
It is human readable to me. |
It is human readable to me. |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( date_manipulation ). |
-module( date_manipulation ). |
||
Line 897: | Line 897: | ||
time_from_strings_hour( Hour, "am" ) -> erlang:list_to_integer( Hour ); |
time_from_strings_hour( Hour, "am" ) -> erlang:list_to_integer( Hour ); |
||
time_from_strings_hour( Hour, "pm" ) -> erlang:list_to_integer( Hour ) + 12. |
time_from_strings_hour( Hour, "pm" ) -> erlang:list_to_integer( Hour ) + 12. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 905: | Line 905: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
<syntaxhighlight lang="euphoria"> |
|||
<lang Euphoria> |
|||
--Date Manipulation task from Rosetta Code wiki |
--Date Manipulation task from Rosetta Code wiki |
||
--User:Lnettnay |
--User:Lnettnay |
||
Line 916: | Line 916: | ||
dt = add(dt, 12, HOURS) |
dt = add(dt, 12, HOURS) |
||
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")}) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 925: | Line 925: | ||
The .NET framework does not support parsing of time zone identifiers like "EST". We have to use time zone offsets like "-5". |
The .NET framework does not support parsing of time zone identifiers like "EST". We have to use time zone offsets like "-5". |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
let main() = |
let main() = |
||
Line 938: | Line 938: | ||
Console.WriteLine( "12 hours later in EST : {0}", date2_est ) |
Console.WriteLine( "12 hours later in EST : {0}", date2_est ) |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} (depends on locale settings): |
{{out}} (depends on locale settings): |
||
Line 947: | Line 947: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: calendar calendar.english calendar.format calendar.parser |
||
combinators io kernel math math.parser sequences splitting |
combinators io kernel math math.parser sequences splitting |
||
unicode ; |
unicode ; |
||
Line 968: | Line 968: | ||
"March 7 2009 7:30pm EST" parse-date dup 12 hours time+ |
"March 7 2009 7:30pm EST" parse-date dup 12 hours time+ |
||
[ timestamp>rfc822 print ] bi@</ |
[ timestamp>rfc822 print ] bi@</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 979: | Line 979: | ||
In the expression "d + 12hr", the "12hr" defines an instance of the Duration class, interpreting the duration in nanoseconds. |
In the expression "d + 12hr", the "12hr" defines an instance of the Duration class, interpreting the duration in nanoseconds. |
||
< |
<syntaxhighlight lang="fantom"> |
||
fansh> d := DateTime.fromLocale("March 7 2009 7:30pm EST", "MMMM D YYYY h:mmaa zzz") |
fansh> d := DateTime.fromLocale("March 7 2009 7:30pm EST", "MMMM D YYYY h:mmaa zzz") |
||
fansh> d |
fansh> d |
||
Line 987: | Line 987: | ||
fansh> (d+12hr).toTimeZone(TimeZone("London")) // the extra credit! |
fansh> (d+12hr).toTimeZone(TimeZone("London")) // the extra credit! |
||
2009-03-08T12:30:00Z London |
2009-03-08T12:30:00Z London |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
#include "vbcompat.bi" |
#include "vbcompat.bi" |
||
Line 1,075: | Line 1,075: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,088: | Line 1,088: | ||
and automatically converts between timezones. |
and automatically converts between timezones. |
||
By default, output times are in the user's defined timezone. |
By default, output times are in the user's defined timezone. |
||
< |
<syntaxhighlight lang="frink"> |
||
### MMM dd yyyy h:mma ### |
### MMM dd yyyy h:mma ### |
||
d = parseDate["March 7 2009 7:30pm EST"] |
d = parseDate["March 7 2009 7:30pm EST"] |
||
println[d + 12 hours -> Eastern] |
println[d + 12 hours -> Eastern] |
||
println[d + 12 hours -> Switzerland] // Extra credit |
println[d + 12 hours -> Switzerland] // Extra credit |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,102: | Line 1,102: | ||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
< |
<syntaxhighlight lang="funl">import time.{TimeZone, Date, SimpleDateFormat, Hour} |
||
pattern = SimpleDateFormat( 'MMMM d yyyy h:mma zzz' ) |
pattern = SimpleDateFormat( 'MMMM d yyyy h:mma zzz' ) |
||
Line 1,109: | Line 1,109: | ||
println( pattern.format(later) ) // Eastern Daylight Time |
println( pattern.format(later) ) // Eastern Daylight Time |
||
pattern.setTimeZone( TimeZone.getTimeZone('America/Los_Angeles') ) |
pattern.setTimeZone( TimeZone.getTimeZone('America/Los_Angeles') ) |
||
println( pattern.format(later) ) // U.S. Pacific Time</ |
println( pattern.format(later) ) // U.S. Pacific Time</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,127: | Line 1,127: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,157: | Line 1,157: | ||
fmt.Println("+12 hrs in Arizona:", t.In(atz)) |
fmt.Println("+12 hrs in Arizona:", t.In(atz)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,169: | Line 1,169: | ||
{{libheader|Joda Time|2.1}} |
{{libheader|Joda Time|2.1}} |
||
< |
<syntaxhighlight lang="groovy">import org.joda.time.* |
||
import java.text.* |
import java.text.* |
||
Line 1,180: | Line 1,180: | ||
println (dt) |
println (dt) |
||
println (dt.plusHours(12)) |
println (dt.plusHours(12)) |
||
println (dt.plusHours(12).withZone(DateTimeZone.UTC))</ |
println (dt.plusHours(12).withZone(DateTimeZone.UTC))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,189: | Line 1,189: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import qualified Data.Time.Clock.POSIX as P |
||
import qualified Data.Time.Format as F |
import qualified Data.Time.Format as F |
||
Line 1,202: | Line 1,202: | ||
"%B %e %Y %l:%M%P %Z" |
"%B %e %Y %l:%M%P %Z" |
||
"March 7 2009 7:30pm EST" |
"March 7 2009 7:30pm EST" |
||
t2 = P.posixSecondsToUTCTime $ 12 * 60 * 60 + P.utcTimeToPOSIXSeconds t1</ |
t2 = P.posixSecondsToUTCTime $ 12 * 60 * 60 + P.utcTimeToPOSIXSeconds t1</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>2009-03-08 12:30:00 UTC</pre> |
<pre>2009-03-08 12:30:00 UTC</pre> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest"> |
||
CHARACTER date="March 7 2009 7:30pm EST", am_pm, result*20 |
CHARACTER date="March 7 2009 7:30pm EST", am_pm, result*20 |
||
Line 1,218: | Line 1,218: | ||
! result = "Sun 2009-03-08 07:30" |
! result = "Sun 2009-03-08 07:30" |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
This uses the datetime procedures from the Icon Programming Library. Several supplemental procedures were needed to normalize the date format (as the one used in the task isn't fully compatible with the library), and to better handle time zones (as the library routines don't handle part hour time zones). |
This uses the datetime procedures from the Icon Programming Library. Several supplemental procedures were needed to normalize the date format (as the one used in the task isn't fully compatible with the library), and to better handle time zones (as the library routines don't handle part hour time zones). |
||
< |
<syntaxhighlight lang="icon">link datetime |
||
procedure main() |
procedure main() |
||
Line 1,288: | Line 1,288: | ||
} |
} |
||
return case x of { "list" : AZ ; "table" : TZ } |
return case x of { "list" : AZ ; "table" : TZ } |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 1,305: | Line 1,305: | ||
With that in mind: |
With that in mind: |
||
< |
<syntaxhighlight lang="j">require'dates' |
||
months=: <;._2 tolower 0 :0 |
months=: <;._2 tolower 0 :0 |
||
January |
January |
||
Line 1,331: | Line 1,331: | ||
getts=: getyear, getmonth, getday, gethour, getminsec |
getts=: getyear, getmonth, getday, gethour, getminsec |
||
timeadd=: 1&tsrep@+&tsrep |
timeadd=: 1&tsrep@+&tsrep |
||
deltaT=: (1 tsrep 0)&([ + -@#@[ {. ])</ |
deltaT=: (1 tsrep 0)&([ + -@#@[ {. ])</syntaxhighlight> |
||
This parser assumes that numeric date information appears to the left of time information, that month name is spelled out in full and that time zone may be ignored. (Alternate date representations are straightforward to implement but turn this into a somewhat open-ended problem). |
This parser assumes that numeric date information appears to the left of time information, that month name is spelled out in full and that time zone may be ignored. (Alternate date representations are straightforward to implement but turn this into a somewhat open-ended problem). |
||
Line 1,339: | Line 1,339: | ||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang="j"> (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST' |
||
2009 3 8 7 30 0 |
2009 3 8 7 30 0 |
||
timestamp (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST' |
timestamp (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST' |
||
08 Mar 2009 07:30:00 |
08 Mar 2009 07:30:00 |
||
isotimestamp (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST' |
isotimestamp (deltaT 12 0 0) timeadd getts 'March 7 2009 7:30pm EST' |
||
2009-03-08 07:30:00.000</ |
2009-03-08 07:30:00.000</syntaxhighlight> |
||
Note that the isotimestamp representation uses a space instead of a 'T' to separate date and time. |
Note that the isotimestamp representation uses a space instead of a 'T' to separate date and time. |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.time.*; |
||
import java.time.format.*; |
import java.time.format.*; |
||
Line 1,370: | Line 1,370: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,392: | Line 1,392: | ||
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. |
||
< |
<syntaxhighlight lang="javascript">function add12hours(dateString) { |
||
// Get the parts of the date string |
// Get the parts of the date string |
||
Line 1,432: | Line 1,432: | ||
'Input: ' + inputDateString + '\n' + |
'Input: ' + inputDateString + '\n' + |
||
'+12hrs in local time: ' + add12hours(inputDateString) |
'+12hrs in local time: ' + add12hours(inputDateString) |
||
);</ |
);</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|version with mktime}} |
{{works with|jq|version with mktime}} |
||
< |
<syntaxhighlight lang="jq">"March 7 2009 7:30pm EST" |
||
| strptime("%B %d %Y %I:%M%p %Z") |
| strptime("%B %d %Y %I:%M%p %Z") |
||
| .[3] += 12 |
| .[3] += 12 |
||
| mktime | strftime("%B %d %Y %I:%M%p %Z") </ |
| mktime | strftime("%B %d %Y %I:%M%p %Z") </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">"March 08 2009 07:30AM EST"</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
===without TimeZones library=== |
===without TimeZones library=== |
||
< |
<syntaxhighlight lang="julia">using Dates |
||
function main() |
function main() |
||
Line 1,458: | Line 1,458: | ||
main() |
main() |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre>March 08 2009 07:30</pre> |
<pre>March 08 2009 07:30</pre> |
||
===With TimeZones.jl=== |
===With TimeZones.jl=== |
||
< |
<syntaxhighlight lang="julia">using Dates, TimeZones |
||
function testdateparse() |
function testdateparse() |
||
Line 1,475: | Line 1,475: | ||
testdateparse() |
testdateparse() |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre>March 07 2009 07:30AM +05:00</pre> |
<pre>March 07 2009 07:30AM +05:00</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
import java.text.SimpleDateFormat |
import java.text.SimpleDateFormat |
||
Line 1,497: | Line 1,497: | ||
cal.timeZone = TimeZone.getTimeZone("MST") |
cal.timeZone = TimeZone.getTimeZone("MST") |
||
println(fmt.format(cal)) |
println(fmt.format(cal)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,509: | Line 1,509: | ||
{{works with|langur|0.10.1}} |
{{works with|langur|0.10.1}} |
||
< |
<syntaxhighlight lang="langur">val .input = "March 7 2009 7:30pm -05:00" |
||
val .iformat = "January 2 2006 3:04pm -07:00" # input format |
val .iformat = "January 2 2006 3:04pm -07:00" # input format |
||
val .format = "January 2 2006 3:04pm MST" # output format |
val .format = "January 2 2006 3:04pm MST" # output format |
||
Line 1,532: | Line 1,532: | ||
writeln $"in UTC: \.d5; (\.d5:dt.format;)" |
writeln $"in UTC: \.d5; (\.d5:dt.format;)" |
||
writeln $"+02:30 time zone: \.d6; (\.d6:dt.format;)" |
writeln $"+02:30 time zone: \.d6; (\.d6:dt.format;)" |
||
writeln $"in EST: \.d7; (\.d7:dt.format;)"</ |
writeln $"in EST: \.d7; (\.d7:dt.format;)"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,549: | Line 1,549: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">local(date) = date('March 7 2009 7:30PM EST',-format='MMMM d yyyy h:mma z') |
||
#date->add(-hour = 24) |
#date->add(-hour = 24) |
||
#date->timezone = 'GMT'</ |
#date->timezone = 'GMT'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,557: | Line 1,557: | ||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">---------------------------------------- |
||
-- Returns string representation of given date object in YYYY-MM-DD hh:mm:ii format |
-- Returns string representation of given date object in YYYY-MM-DD hh:mm:ii format |
||
-- @param {date} dateObj |
-- @param {date} dateObj |
||
Line 1,591: | Line 1,591: | ||
put "-" after char 4 of str |
put "-" after char 4 of str |
||
return str |
return str |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lingo">dateStr = "March 7 2009 7:30pm EST" |
||
-- parse string |
-- parse string |
||
Line 1,619: | Line 1,619: | ||
-- show as YYYY-MM-DD hh:mm:ii string |
-- show as YYYY-MM-DD hh:mm:ii string |
||
put dateToDateTimeString(newDateObj) |
put dateToDateTimeString(newDateObj) |
||
-- "2009-03-08 07:30:00"</ |
-- "2009-03-08 07:30:00"</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
The following solution is quite ugly, but unfortunately there is not anything like 'strptime'-function in Lua. |
The following solution is quite ugly, but unfortunately there is not anything like 'strptime'-function in Lua. |
||
< |
<syntaxhighlight lang="lua"> |
||
str = string.lower( "March 7 2009 7:30pm EST" ) |
str = string.lower( "March 7 2009 7:30pm EST" ) |
||
Line 1,650: | Line 1,650: | ||
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 ) ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Sun Mar 8 07:30:00 2009</pre> |
<pre>Sun Mar 8 07:30:00 2009</pre> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">twelve_hours := proc(str) |
||
local dt, zone; |
local dt, zone; |
||
local months := ["January","February","March","April","May","June","July","August","September","October","November","December"]; |
local months := ["January","February","March","April","May","June","July","August","September","October","November","December"]; |
||
Line 1,670: | Line 1,670: | ||
printf(TimeZone(dt)); |
printf(TimeZone(dt)); |
||
end proc; |
end proc; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out|Usage}} |
{{Out|Usage}} |
||
<lang>twelve_hours("March 7 2009 7:30pm EST"); |
<syntaxhighlight lang="text">twelve_hours("March 7 2009 7:30pm EST"); |
||
twelve_hours("March 2 2009 0:10am WET"); |
twelve_hours("March 2 2009 0:10am WET"); |
||
twelve_hours("March 2 2009 6:30am AST");</ |
twelve_hours("March 2 2009 6:30am AST");</syntaxhighlight> |
||
{{Out|Output}} |
{{Out|Output}} |
||
<pre> |
<pre> |
||
Line 1,683: | Line 1,683: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">dstr = "March 7 2009 7:30pm EST"; |
||
DateString[DatePlus[dstr, {12, "Hour"}], {"DayName", " ", "MonthName", " ", "Day", " ", "Year", " ", "Hour24", ":", "Minute", "AMPM"}]</ |
DateString[DatePlus[dstr, {12, "Hour"}], {"DayName", " ", "MonthName", " ", "Day", " ", "Year", " ", "Hour24", ":", "Minute", "AMPM"}]</syntaxhighlight> |
||
=={{header|mIRC Scripting Language}}== |
=={{header|mIRC Scripting Language}}== |
||
< |
<syntaxhighlight lang="mirc">echo -ag $asctime($calc($ctime(March 7 2009 7:30pm EST)+43200))</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols binary |
options replace format comments java crossref symbols binary |
||
Line 1,719: | Line 1,719: | ||
end |
end |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,732: | Line 1,732: | ||
We output the dates built after parsing in UTC. The module allows also to output in local time. |
We output the dates built after parsing in UTC. The module allows also to output in local time. |
||
< |
<syntaxhighlight lang="nim">import times |
||
const Date = "March 7 2009 7:30pm EST" |
const Date = "March 7 2009 7:30pm EST" |
||
Line 1,741: | Line 1,741: | ||
dt = dt + initDuration(hours = 12) |
dt = dt + initDuration(hours = 12) |
||
echo "Date 12 hours later is: ", dt.utc().format("MMMM d yyyy h:mmtt zzz")</ |
echo "Date 12 hours later is: ", dt.utc().format("MMMM d yyyy h:mmtt zzz")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,750: | Line 1,750: | ||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
===version 1=== |
===version 1=== |
||
<syntaxhighlight lang="oorexx"> |
|||
<lang ooRexx> |
|||
sampleDate = 'March 7 2009 7:30pm EST' |
sampleDate = 'March 7 2009 7:30pm EST' |
||
Line 1,877: | Line 1,877: | ||
*/ |
*/ |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
===version 2=== |
===version 2=== |
||
This example is written using the Open Object Rexx dialect to take advantage of the <code>DateTime</code> built–in class. |
This example is written using the Open Object Rexx dialect to take advantage of the <code>DateTime</code> built–in class. |
||
< |
<syntaxhighlight lang="rexx">/* Rexx */ |
||
sampleDate = 'March 7 2009 7:30pm EST' |
sampleDate = 'March 7 2009 7:30pm EST' |
||
Line 2,098: | Line 2,098: | ||
__ENDD__ |
__ENDD__ |
||
*/ |
*/ |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,125: | Line 2,125: | ||
We use Mountain Daylight Time for output. |
We use Mountain Daylight Time for output. |
||
< |
<syntaxhighlight lang="perl">use DateTime; |
||
use DateTime::Format::Strptime 'strptime'; |
use DateTime::Format::Strptime 'strptime'; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 2,135: | Line 2,135: | ||
->add(hours => 12) |
->add(hours => 12) |
||
->set_time_zone('America/Edmonton') |
->set_time_zone('America/Edmonton') |
||
->format_cldr('MMMM d yyyy h:mma zzz');</ |
->format_cldr('MMMM d yyyy h:mma zzz');</syntaxhighlight> |
||
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) |
||
Line 2,144: | Line 2,144: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
{{libheader|Phix/basics}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins<span style="color: #0000FF;">\<span style="color: #004080;">timedate<span style="color: #0000FF;">.<span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins<span style="color: #0000FF;">\<span style="color: #004080;">timedate<span style="color: #0000FF;">.<span style="color: #000000;">e</span> |
||
<span style="color: #7060A8;">set_timedate_formats<span style="color: #0000FF;">(<span style="color: #0000FF;">{<span style="color: #008000;">"Mmmm d yyyy h:mmpm tz"<span style="color: #0000FF;">}<span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">set_timedate_formats<span style="color: #0000FF;">(<span style="color: #0000FF;">{<span style="color: #008000;">"Mmmm d yyyy h:mmpm tz"<span style="color: #0000FF;">}<span style="color: #0000FF;">)</span> |
||
Line 2,155: | Line 2,155: | ||
<span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">adjust_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">,<span style="color: #7060A8;">timedelta<span style="color: #0000FF;">(<span style="color: #000000;">days<span style="color: #0000FF;">:=<span style="color: #000000;">31<span style="color: #0000FF;">*<span style="color: #000000;">4<span style="color: #0000FF;">)<span style="color: #0000FF;">)</span> |
<span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">adjust_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">,<span style="color: #7060A8;">timedelta<span style="color: #0000FF;">(<span style="color: #000000;">days<span style="color: #0000FF;">:=<span style="color: #000000;">31<span style="color: #0000FF;">*<span style="color: #000000;">4<span style="color: #0000FF;">)<span style="color: #0000FF;">)</span> |
||
<span style="color: #0000FF;">?<span style="color: #7060A8;">format_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">) |
<span style="color: #0000FF;">?<span style="color: #7060A8;">format_timedate<span style="color: #0000FF;">(<span style="color: #000000;">td<span style="color: #0000FF;">) |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,164: | Line 2,164: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
$time = new DateTime('March 7 2009 7:30pm EST'); |
$time = new DateTime('March 7 2009 7:30pm EST'); |
||
$time->modify('+12 hours'); |
$time->modify('+12 hours'); |
||
echo $time->format('c'); |
echo $time->format('c'); |
||
?></ |
?></syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de timePlus12 (Str) |
||
(use (@Mon @Day @Year @Time @Zone) |
(use (@Mon @Day @Year @Time @Zone) |
||
(and |
(and |
||
Line 2,189: | Line 2,189: | ||
(dec '@Time 86400) |
(dec '@Time 86400) |
||
(inc 'Date) ) |
(inc 'Date) ) |
||
(pack (dat$ Date "-") " " (tim$ @Time T) " " @Zone) ) ) ) )</ |
(pack (dat$ Date "-") " " (tim$ @Time T) " " @Zone) ) ) ) )</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">> (Calendar.dwim_time("March 7 2009 7:30pm EST")+Calendar.Hour()*12)->set_timezone("CET")->format_ext_time(); |
||
Result: "Saturday, 7 March 2009 12:30:00"</ |
Result: "Saturday, 7 March 2009 12:30:00"</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pl/i">/* The PL/I date functions handle dates and time in 49 */ |
||
/* different formats, but not that particular one. For any of the */ |
/* different formats, but not that particular one. For any of the */ |
||
/* standard formats, the following date manipulation will add */ |
/* standard formats, the following date manipulation will add */ |
||
Line 2,203: | Line 2,203: | ||
seconds = SECS(DATETIME()); |
seconds = SECS(DATETIME()); |
||
seconds = seconds + 12*60*60; |
seconds = seconds + 12*60*60; |
||
put list (SECSTODATE(seconds));</ |
put list (SECSTODATE(seconds));</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
The .NET framework does not support parsing of time zone identifiers like "EST". We have to use time zone offsets like "-5". |
The .NET framework does not support parsing of time zone identifiers like "EST". We have to use time zone offsets like "-5". |
||
< |
<syntaxhighlight lang="powershell">$date = [DateTime]::Parse("March 7 2009 7:30pm -5" ) |
||
write-host $date |
write-host $date |
||
write-host $date.AddHours(12) |
write-host $date.AddHours(12) |
||
write-host [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($date.AddHours(12),"Vladivostok Standard Time")</ |
write-host [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($date.AddHours(12),"Vladivostok Standard Time")</syntaxhighlight> |
||
{{out}} (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 |
||
Line 2,217: | Line 2,217: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang="purebasic"> |
|||
<lang PureBasic> |
|||
EnableExplicit |
EnableExplicit |
||
Line 2,316: | Line 2,316: | ||
CloseConsole() |
CloseConsole() |
||
EndIf |
EndIf |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,328: | Line 2,328: | ||
I don't do anything with timezone here, but it is possible. |
I don't do anything with timezone here, but it is possible. |
||
< |
<syntaxhighlight lang="python">import datetime |
||
def mt(): |
def mt(): |
||
Line 2,339: | Line 2,339: | ||
print datime2.strftime("%B %d %Y %I:%M%p %Z") + datime1[-3:] |
print datime2.strftime("%B %d %Y %I:%M%p %Z") + datime1[-3:] |
||
mt()</ |
mt()</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">time <- strptime("March 7 2009 7:30pm EST", "%B %d %Y %I:%M%p %Z") # "2009-03-07 19:30:00" |
||
isotime <- ISOdatetime(1900 + time$year, time$mon, time$mday, |
isotime <- ISOdatetime(1900 + time$year, time$mon, time$mday, |
||
time$hour, time$min, time$sec, "EST") # "2009-02-07 19:30:00 EST" |
time$hour, time$min, time$sec, "EST") # "2009-02-07 19:30:00 EST" |
||
twelvehourslater <- isotime + 12 * 60 * 60 # "2009-02-08 07:30:00 EST" |
twelvehourslater <- isotime + 12 * 60 * 60 # "2009-02-08 07:30:00 EST" |
||
timeincentraleurope <- format(isotime, tz="CET", usetz=TRUE) #"2009-02-08 01:30:00 CET"</ |
timeincentraleurope <- format(isotime, tz="CET", usetz=TRUE) #"2009-02-08 01:30:00 CET"</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
The solution below ignores the time zone. |
The solution below ignores the time zone. |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require srfi/19) |
(require srfi/19) |
||
Line 2,368: | Line 2,368: | ||
12hours)) |
12hours)) |
||
"~a ~d ~b ~Y ~H:~M") |
"~a ~d ~b ~Y ~H:~M") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="racket"> |
||
"Sun 08 Mar 2009 07:30" |
"Sun 08 Mar 2009 07:30" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,383: | Line 2,383: | ||
or maybe just due to laziness), but that just gives us another opportunity to demonstrate the built-in grammar support. |
or maybe just due to laziness), but that just gives us another opportunity to demonstrate the built-in grammar support. |
||
<lang |
<syntaxhighlight lang="raku" line>my @month = <January February March April May June July August September October November December>; |
||
my %month = flat (@month Z=> ^12), (@month».substr(0,3) Z=> ^12), 'Sept' => 8; |
my %month = flat (@month Z=> ^12), (@month».substr(0,3) Z=> ^12), 'Sept' => 8; |
||
Line 2,434: | Line 2,434: | ||
say "12 hours later, GMT: $dt"; |
say "12 hours later, GMT: $dt"; |
||
say "12 hours later, PST: $dt.in-timezone(-8 * 3600)";</ |
say "12 hours later, PST: $dt.in-timezone(-8 * 3600)";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>12 hours later, GMT: 2009-02-08T12:30:00Z |
<pre>12 hours later, GMT: 2009-02-08T12:30:00Z |
||
Line 2,440: | Line 2,440: | ||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">REBOL [ |
||
Title: "Date Manipulation" |
Title: "Date Manipulation" |
||
URL: http://rosettacode.org/wiki/Date_Manipulation |
URL: http://rosettacode.org/wiki/Date_Manipulation |
||
Line 2,464: | Line 2,464: | ||
print 12:00 + read-time "March 7 2009 7:30pm EST" |
print 12:00 + read-time "March 7 2009 7:30pm EST" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,470: | Line 2,470: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
<syntaxhighlight lang="red"> |
|||
<lang Red> |
|||
d: 07-Mar-2009/19:30 + 12:00 |
d: 07-Mar-2009/19:30 + 12:00 |
||
print d |
print d |
||
Line 2,476: | Line 2,476: | ||
d/timezone: 1 |
d/timezone: 1 |
||
print d |
print d |
||
8-Mar-2009/8:30:00+01:00</ |
8-Mar-2009/8:30:00+01:00</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This version only works with REXXes that support the '''date''' and '''time''' extended functions. |
This version only works with REXXes that support the '''date''' and '''time''' extended functions. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program adds 12 hours to a given date and time, displaying the before and after.*/ |
||
aDate = 'March 7 2009 7:30pm EST' /*the original or base date to be used.*/ |
aDate = 'March 7 2009 7:30pm EST' /*the original or base date to be used.*/ |
||
Line 2,495: | Line 2,495: | ||
say aDate ' + 12 hours ───► ' ndate ntime tz /*display the new timestamp to console.*/ |
say aDate ' + 12 hours ───► ' ndate ntime tz /*display the new timestamp to console.*/ |
||
/*stick a fork in it, we're all done. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 2,502: | Line 2,502: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Date manipulation |
# Project : Date manipulation |
||
Line 2,529: | Line 2,529: | ||
see "Original - " + dateorigin + nl |
see "Original - " + dateorigin + nl |
||
see "Manipulated - " + d + " " + t1 + ap + nl |
see "Manipulated - " + d + " " + t1 + ap + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,541: | Line 2,541: | ||
{{libheader|ActiveSupport}} |
{{libheader|ActiveSupport}} |
||
< |
<syntaxhighlight lang="ruby">require 'time' |
||
d = "March 7 2009 7:30pm EST" |
d = "March 7 2009 7:30pm EST" |
||
t = Time.parse(d) |
t = Time.parse(d) |
||
Line 2,558: | Line 2,558: | ||
# or, remote = new.in_time_zone('Beijing') |
# or, remote = new.in_time_zone('Beijing') |
||
puts remote.rfc2822 |
puts remote.rfc2822 |
||
puts remote.zone</ |
puts remote.zone</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Sat, 07 Mar 2009 19:30:00 -0500 |
<pre>Sat, 07 Mar 2009 19:30:00 -0500 |
||
Line 2,568: | Line 2,568: | ||
Using [[:Category:ActiveSupport|ActiveSupport]], we can add 12 hours with any of: |
Using [[:Category:ActiveSupport|ActiveSupport]], we can add 12 hours with any of: |
||
< |
<syntaxhighlight lang="ruby">new = t + 12.hours |
||
new = t.in(12.hours) |
new = t.in(12.hours) |
||
new = t.advance(:hours => 12)</ |
new = t.advance(:hours => 12)</syntaxhighlight> |
||
===DateTime class=== |
===DateTime class=== |
||
< |
<syntaxhighlight lang="ruby">require "date" |
||
puts d1 = DateTime.parse("March 7 2009 7:30pm EST") |
puts d1 = DateTime.parse("March 7 2009 7:30pm EST") |
||
# d1 + 1 would add a day, so add half a day: |
# d1 + 1 would add a day, so add half a day: |
||
puts d2 = d1 + 1/2r # 1/2r is a rational; 0.5 would also work |
puts d2 = d1 + 1/2r # 1/2r is a rational; 0.5 would also work |
||
puts d3 = d2.new_offset('+09:00')</ |
puts d3 = d2.new_offset('+09:00')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,587: | Line 2,587: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">theDate$ = "March 7 2009 7:30pm EST" |
||
monthName$ = "January February March April May June July August September October November December" |
monthName$ = "January February March April May June July August September October November December" |
||
Line 2,604: | Line 2,604: | ||
ap$ = "am" |
ap$ = "am" |
||
end if |
end if |
||
print date$(d);" ";t1$;ap$</ |
print date$(d);" ";t1$;ap$</syntaxhighlight><pre>03/08/2009 7:30am</pre> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
use chrono::prelude::*; |
use chrono::prelude::*; |
||
use chrono::Duration; |
use chrono::Duration; |
||
Line 2,632: | Line 2,632: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,640: | Line 2,640: | ||
</pre> |
</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">import java.text.SimpleDateFormat |
||
import java.util.{Calendar, Locale, TimeZone} |
import java.util.{Calendar, Locale, TimeZone} |
||
Line 2,656: | Line 2,656: | ||
println(df.format(c.getTime)) |
println(df.format(c.getTime)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,670: | Line 2,670: | ||
In the example below EST is replaced with UTC-05. |
In the example below EST is replaced with UTC-05. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "time.s7i"; |
include "time.s7i"; |
||
include "duration.s7i"; |
include "duration.s7i"; |
||
Line 2,715: | Line 2,715: | ||
aTime := toUTC(aTime); |
aTime := toUTC(aTime); |
||
writeln("In UTC: " <& aTime); |
writeln("In UTC: " <& aTime); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,725: | Line 2,725: | ||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
< |
<syntaxhighlight lang="sensetalk">set date to "March 7 2009 7:30pm EST" |
||
insert "[month name] [day] [year] [hour12]:[min][pm] [timeZoneID]" into the timeInputFormat |
insert "[month name] [day] [year] [hour12]:[min][pm] [timeZoneID]" into the timeInputFormat |
||
put date + 12 hours |
put date + 12 hours |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
< |
<syntaxhighlight lang="sensetalk">March 8 2009 7:30AM EST</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">var dt = frequire('DateTime::Format::Strptime') |
||
var input = 'March 7 2009 7:30pm EST' |
var input = 'March 7 2009 7:30pm EST' |
||
Line 2,743: | Line 2,743: | ||
.add(hours => 12) \ |
.add(hours => 12) \ |
||
.set_time_zone('America/Edmonton') \ |
.set_time_zone('America/Edmonton') \ |
||
.format_cldr('MMMM d yyyy h:mma zzz')</ |
.format_cldr('MMMM d yyyy h:mma zzz')</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>March 8 2009 6:30AM MDT</pre> |
<pre>March 8 2009 6:30AM MDT</pre> |
||
Line 2,756: | Line 2,756: | ||
The code also fails when adding a duration that "jumps" beyond two DST changes (e.g from EST to EDT and EST again); (it could be partially fixed by considering intervals instead of single date, and adding a fourth element to link to the "new" timezone abbreviation) |
The code also fails when adding a duration that "jumps" beyond two DST changes (e.g from EST to EDT and EST again); (it could be partially fixed by considering intervals instead of single date, and adding a fourth element to link to the "new" timezone abbreviation) |
||
< |
<syntaxhighlight lang="smalltalk">DateTime extend [ |
||
setYear: aNum [ year := aNum ] |
setYear: aNum [ year := aNum ] |
||
]. |
]. |
||
Line 2,805: | Line 2,805: | ||
dateTime: aDT [ dateAndTime := aDT ] |
dateTime: aDT [ dateAndTime := aDT ] |
||
].</ |
].</syntaxhighlight> |
||
Usage example (note: the code is rather rigid, so not all operations possible on DateTime are possible on DateTimeTZ). |
Usage example (note: the code is rather rigid, so not all operations possible on DateTime are possible on DateTimeTZ). |
||
< |
<syntaxhighlight lang="smalltalk">|s abbrDict dt| |
||
s := 'March 7 2009 7:30pm EST'. |
s := 'March 7 2009 7:30pm EST'. |
||
Line 2,837: | Line 2,837: | ||
}) displayNl. |
}) displayNl. |
||
(dt dateTime) asUTC displayNl.</ |
(dt dateTime) asUTC displayNl.</syntaxhighlight> |
||
{{out}} (note that EST should be EDT): |
{{out}} (note that EST should be EDT): |
||
Line 2,845: | Line 2,845: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
{{works with|Oracle}} |
{{works with|Oracle}} |
||
< |
<syntaxhighlight lang="sql"> |
||
-- March 7 2009 7:30pm EST |
-- March 7 2009 7:30pm EST |
||
Line 2,878: | Line 2,878: | ||
at time zone 'US/Arizona' plus_12_nodst |
at time zone 'US/Arizona' plus_12_nodst |
||
from dual; |
from dual; |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 2,898: | Line 2,898: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
<syntaxhighlight lang="standard ml"> |
|||
<lang Standard ML> |
|||
val smltime= fn input => (* parse given format *) |
val smltime= fn input => (* parse given format *) |
||
let |
let |
||
Line 2,920: | Line 2,920: | ||
(Date.fromTimeLocal o Time.fromReal)( ( date2real from) + hours * onehour ); |
(Date.fromTimeLocal o Time.fromReal)( ( date2real from) + hours * onehour ); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
output |
output |
||
<syntaxhighlight lang="standard ml"> |
|||
<lang Standard ML> |
|||
hoursFrom 12.0 ( smltime "March 7 2009 7:30pm EST" ) ; |
hoursFrom 12.0 ( smltime "March 7 2009 7:30pm EST" ) ; |
||
val it = Sun Mar 08 07:30:00 2009: Date.date |
val it = Sun Mar 08 07:30:00 2009: Date.date |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
let formatter = DateFormatter() |
let formatter = DateFormatter() |
||
Line 2,940: | Line 2,940: | ||
print(formatter.string(from: date)) |
print(formatter.string(from: date)) |
||
print(formatter.string(from: date + 60 * 60 * 12))</ |
print(formatter.string(from: date + 60 * 60 * 12))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,949: | Line 2,949: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.5}} |
{{works with|Tcl|8.5}} |
||
< |
<syntaxhighlight lang="tcl">set date "March 7 2009 7:30pm EST" |
||
set epoch [clock scan $date -format "%B %d %Y %I:%M%p %z"] |
set epoch [clock scan $date -format "%B %d %Y %I:%M%p %z"] |
||
set later [clock add $epoch 12 hours] |
set later [clock add $epoch 12 hours] |
||
puts [clock format $later] ;# Sun Mar 08 08:30:00 EDT 2009 |
puts [clock format $later] ;# Sun Mar 08 08:30:00 EDT 2009 |
||
puts [clock format $later -timezone :Asia/Shanghai] ;# Sun Mar 08 20:30:00 CST 2009</ |
puts [clock format $later -timezone :Asia/Shanghai] ;# Sun Mar 08 20:30:00 CST 2009</syntaxhighlight> |
||
Note the transition into daylight savings time in the interval (in the Eastern timezone). |
Note the transition into daylight savings time in the interval (in the Eastern timezone). |
||
Line 2,959: | Line 2,959: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
requires GNU date |
requires GNU date |
||
< |
<syntaxhighlight lang="bash">epoch=$(date -d 'March 7 2009 7:30pm EST +12 hours' +%s) |
||
date -d @$epoch |
date -d @$epoch |
||
TZ=Asia/Shanghai date -d @$epoch</ |
TZ=Asia/Shanghai date -d @$epoch</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,969: | Line 2,969: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-date}} |
{{libheader|Wren-date}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/date" for Date |
||
var fmt = "mmmm| |d| |yyyy| |H|:|MM|am| |zz|" |
var fmt = "mmmm| |d| |yyyy| |H|:|MM|am| |zz|" |
||
Line 2,979: | Line 2,979: | ||
// Adjust to MST say |
// Adjust to MST say |
||
d = d.adjustTime("MST") |
d = d.adjustTime("MST") |
||
System.print("Adjusted to MST : %(d)")</ |
System.print("Adjusted to MST : %(d)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,990: | Line 2,990: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
The iso8601 library offers additional Time/Date support but using the built in stuff: |
The iso8601 library offers additional Time/Date support but using the built in stuff: |
||
< |
<syntaxhighlight lang="zkl">var Date=Time.Date; |
||
fcn add12h(dt){ |
fcn add12h(dt){ |
||
re:=RegExp(0'|(\w+)\s+(\d+)\s+(\d+)\ +(.+)\s|); |
re:=RegExp(0'|(\w+)\s+(\d+)\s+(\d+)\ +(.+)\s|); |
||
Line 3,000: | Line 3,000: | ||
Y,M,D, h,m,s=Date.addHMS(dti,12); |
Y,M,D, h,m,s=Date.addHMS(dti,12); |
||
"%s %d %d %s".fmt(Date.monthNames[M],D,Y,Date.toAMPMString(h,m)); |
"%s %d %d %s".fmt(Date.monthNames[M],D,Y,Date.toAMPMString(h,m)); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">add12h("March 7 2009 7:30pm EST").println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>March 8 2009 07:30AM</pre> |
<pre>March 8 2009 07:30AM</pre> |