Date manipulation: Difference between revisions
Content added Content deleted
Line 382: | Line 382: | ||
END |
END |
||
</lang> |
</lang> |
||
=={{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). |
|||
<lang Icon>link datetime |
|||
procedure main() |
|||
write("input = ",s := "March 7 2009 7:30pm EST" ) |
|||
write("+12 hours = ",SecToTZDateLine(s := TZDateLineToSec(s) + 12*3600,"EST")) |
|||
write(" = ",SecToTZDateLine(s,"UTC")) |
|||
write(" = ",SecToTZDateLine(s,"NST")) |
|||
end |
|||
procedure SecToTZDateLine(s,tz) #: returns dateline + time zone given seconds |
|||
return NormalizedDate(SecToDateLine(s+\(_TZdata("table")[\tz|"UTC"]))||" "|| tz) |
|||
end |
|||
procedure TZDateLineToSec(s) #: returns seconds given dateline (and time zone) |
|||
return ( |
|||
NormalizedDate(s) ? ( |
|||
d := tab(find("am"|"pm")+2),tab(many('\t ,')), |
|||
tz := \_TZdata("table")[tab(0)] |
|||
), |
|||
DateLineToSec(d) - tz) |
|||
end |
|||
procedure NormalizedDate(s) #: returns a consistent dateline |
|||
static D,M |
|||
initial { |
|||
D := ["Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"] |
|||
M := ["January","February","March","April","May","June", |
|||
"July","August","September","October","November","December"] |
|||
} |
|||
map(s) ? { # parse and build consistent dateline |
|||
ds := 1(x := !D, =map(x)) | "" # Weekday |
|||
ds ||:= 1(", ", tab(many('\t ,')|&pos)) |
|||
ds ||:= 1(x := !M, =map(x)) | fail # Month |
|||
ds ||:= 1(" ", tab(many('\t ,')|&pos)) |
|||
ds ||:= tab(many(&digits)) | fail # day |
|||
ds ||:= 1(", ", tab(many('\t ,'))) | fail |
|||
ds ||:= tab(many(&digits)) | fail # year |
|||
ds ||:= 1(" ", tab(many('\t ,'))) | fail |
|||
ds ||:= tab(many(&digits))||(=":"||tab(many(&digits))|&null) | fail # time |
|||
ds ||:= 1(" ", tab(many('\t ,')|&pos)) |
|||
ds ||:= =("am"|"pm") | fail # halfday |
|||
ds ||:= 1(" ", tab(many('\t ,')|&pos)) |
|||
tz := map(=!_TZdata("list"),&lcase,&ucase) |
|||
} |
|||
if ds[1] == "," then |
|||
ds := SecToDateLine(DateLineToSec("Sunday"||ds)) # get IPL to fix weekday |
|||
return ds ||:= " " || \tz|"UTC" |
|||
end |
|||
procedure _TZdata(x) #: internal return TZ data (demo version incomplete) |
|||
static TZ,AZ |
|||
initial { |
|||
TZ := table() |
|||
AZ := [] |
|||
"UTC/0;ACDT/+10.5;CET/1;EST/-5;NPT/+5.75;NST/-3.5;PST/-8;" ? |
|||
while ( a := tab(find("/")), move(1), o := tab(find(";")), move(1) ) do { |
|||
TZ[map(a)] := TZ[a] := integer(3600*o) |
|||
put(AZ,a,map(a)) |
|||
} |
|||
every TZ[&null|""] := TZ["UTC"] |
|||
} |
|||
return case x of { "list" : AZ ; "table" : TZ } |
|||
end</lang> |
|||
{{libheader|Icon Programming Library}} |
|||
[http://www.cs.arizona.edu/icon/library/src/procs/datetime.icn datetime provides SecToDateLine, and DateLineToSec] |
|||
Output:<pre>input = March 7 2009 7:30pm EST |
|||
+12 hours = Sunday, March 8, 2009 7:30 am EST |
|||
= Sunday, March 8, 2009 12:30 pm UTC |
|||
= Sunday, March 8, 2009 9:00 am NST</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 404: | Line 482: | ||
or using <tt>System.out.println(date);</tt> as the last line: |
or using <tt>System.out.println(date);</tt> as the last line: |
||
<pre>Sun Mar 08 08:30:00 EDT 2009</pre> |
<pre>Sun Mar 08 08:30:00 EDT 2009</pre> |
||
=={{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. |