Date manipulation: Difference between revisions

Content added Content deleted
(→‎{{header|AppleScript}}: Added ASObjC alternative.)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 824: Line 824:


----
----

=={{header|Erlang}}==
=={{header|Erlang}}==
It is human readable to me.
It is human readable to me.
Line 958: Line 959:
2009-03-08T12:30:00Z London
2009-03-08T12:30:00Z London
</lang>
</lang>

=={{header|Fōrmulæ}}==

In [https://wiki.formulae.org/Date_manipulation this] page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
Line 1,095: Line 1,088:
March 8 2009 5:30AM PDT
March 8 2009 5:30AM PDT
</pre>
</pre>

=={{header|Fōrmulæ}}==

In [https://wiki.formulae.org/Date_manipulation this] page you can see the solution of this task.

Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.

The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.


=={{header|Go}}==
=={{header|Go}}==
Line 2,048: Line 2,049:
{{out}}
{{out}}
<pre>March 8 2009 6:30AM MDT</pre>
<pre>March 8 2009 6:30AM MDT</pre>

=={{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. <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>;
my %month = flat (@month Z=> ^12), (@month».substr(0,3) Z=> ^12), 'Sept' => 8;

grammar US-DateTime {
rule TOP { <month> <day>','? <year>','? <time> <tz> }

token month {
(\w+)'.'? { make %month{$0} // die "Bad month name: $0" }
}

token day { (\d ** 1..2) { make +$0 } }

token year { (\d ** 1..4) { make +$0 } }

token time {
(\d ** 1..2) ':' (\d ** 2) \h* ( :i <[ap]> \.? m | '' )
{
my $h = $0 % 12;
my $m = $1;
$h += 12 if $2 and $2.substr(0,1).lc eq 'p';
make $h * 60 + $m;
}
}

token tz { # quick and dirty for this task
[
| EDT { make -4 }
| [ EST| CDT] { make -5 }
| [ CST| MDT] { make -6 }
| [ MST| PDT] { make -7 }
| [ PST|AKDT] { make -8 }
| [AKST|HADT] { make -9 }
| HAST
]
}
}

$/ = US-DateTime.parse('March 7 2009 7:30pm EST') or die "Can't parse date";

my $year = $<year>.ast;
my $month = $<month>.ast;
my $day = $<day>.ast;
my $hour = $<time>.ast div 60;
my $minute = $<time>.ast mod 60;
my $timezone = $<tz>.ast * 3600;

my $dt = DateTime.new(:$year, :$month, :$day, :$hour, :$minute, :$timezone).in-timezone(0);

$dt = $dt.later(hours => 12);

say "12 hours later, GMT: $dt";
say "12 hours later, PST: $dt.in-timezone(-8 * 3600)";</lang>
{{out}}
<pre>12 hours later, GMT: 2009-02-08T12:30:00Z
12 hours later, PST: 2009-02-08T04:30:00-0800</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,341: Line 2,278:
"Sun 08 Mar 2009 07:30"
"Sun 08 Mar 2009 07:30"
</lang>
</lang>

=={{header|Raku}}==
(formerly 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. <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>;
my %month = flat (@month Z=> ^12), (@month».substr(0,3) Z=> ^12), 'Sept' => 8;

grammar US-DateTime {
rule TOP { <month> <day>','? <year>','? <time> <tz> }

token month {
(\w+)'.'? { make %month{$0} // die "Bad month name: $0" }
}

token day { (\d ** 1..2) { make +$0 } }

token year { (\d ** 1..4) { make +$0 } }

token time {
(\d ** 1..2) ':' (\d ** 2) \h* ( :i <[ap]> \.? m | '' )
{
my $h = $0 % 12;
my $m = $1;
$h += 12 if $2 and $2.substr(0,1).lc eq 'p';
make $h * 60 + $m;
}
}

token tz { # quick and dirty for this task
[
| EDT { make -4 }
| [ EST| CDT] { make -5 }
| [ CST| MDT] { make -6 }
| [ MST| PDT] { make -7 }
| [ PST|AKDT] { make -8 }
| [AKST|HADT] { make -9 }
| HAST
]
}
}

$/ = US-DateTime.parse('March 7 2009 7:30pm EST') or die "Can't parse date";

my $year = $<year>.ast;
my $month = $<month>.ast;
my $day = $<day>.ast;
my $hour = $<time>.ast div 60;
my $minute = $<time>.ast mod 60;
my $timezone = $<tz>.ast * 3600;

my $dt = DateTime.new(:$year, :$month, :$day, :$hour, :$minute, :$timezone).in-timezone(0);

$dt = $dt.later(hours => 12);

say "12 hours later, GMT: $dt";
say "12 hours later, PST: $dt.in-timezone(-8 * 3600)";</lang>
{{out}}
<pre>12 hours later, GMT: 2009-02-08T12:30:00Z
12 hours later, PST: 2009-02-08T04:30:00-0800</pre>


=={{header|REBOL}}==
=={{header|REBOL}}==