Date manipulation: Difference between revisions

(15 intermediate revisions by 7 users not shown)
Line 554:
}else if( Occurs(",", cTime) ){ // horas>24 , minutos [0->59]
long min=0;
String cmin;
Stack{
addhour = Str2int( Get_token(cTime,1) );
min = Time2sec( Multi_copy( cminNULL, "00:", Get_token(cTime,2),NULL) );
}Stack_off;
Free secure cmin;
if( addhour<0 ) {
addhour = addhour*60*60 - min;
Line 1,309 ⟶ 1,307:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?timeZone=America%2FNew_York&script=examples/Date_manipulation}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. 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 storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''Creating the initial time.''' Fōrmulæ can create a time in any time zone, however, it always displays them in the current timezone. In order to cause no confusion, in this example the current time zone has been set as America/New_York, which is EST.
In '''[https://formulae.org/?timeZone=America%2FNew_York&example=Date_manipulation this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Date manipulation 01.png]]
 
[[File:Fōrmulæ - Date manipulation 02.png]]
 
'''Adding 12 hours.''' It is achieved adding to the time expression the desired number of milliseconds.
 
[[File:Fōrmulæ - Date manipulation 03.png]]
 
[[File:Fōrmulæ - Date manipulation 04.png]]
 
This is not a bug, the daylight saving time (in the America/New_York time zone), for 2009 started March 8 at 2:00 hrs, at which time clock is adjusted 1 hour later. It occurred between the 12 hour addition of our example. See [https://www.timeanddate.com/time/change/usa/new-york?year=2009 this page].
 
Note the ☀ symbol, indicating that the time is in daylight saving time (at the current time zone).
 
'''Showing results for other time zones.''' As it was said before, a time is always shown in current time zone, but a time can be formatted to different time zones.
 
If no time zone is specified, current time zone is used:
 
[[File:Fōrmulæ - Date manipulation 05.png]]
 
[[File:Fōrmulæ - Date manipulation 06.png]]
 
[[File:Fōrmulæ - Date manipulation 07.png]]
 
[[File:Fōrmulæ - Date manipulation 08.png]]
 
Let us use a different time zone:
 
[[File:Fōrmulæ - Date manipulation 09.png]]
 
[[File:Fōrmulæ - Date manipulation 10.png]]
 
[[File:Fōrmulæ - Date manipulation 11.png]]
 
[[File:Fōrmulæ - Date manipulation 12.png]]
 
Beside the time zone, a different locale can also be specified, in order to format the result in such that locale:
 
[[File:Fōrmulæ - Date manipulation 13.png]]
 
[[File:Fōrmulæ - Date manipulation 14.png]]
 
The components of a time expression can be obtained individually, even for a specific time zone:
 
[[File:Fōrmulæ - Date manipulation 15.png]]
 
[[File:Fōrmulæ - Date manipulation 16.png]]
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn DoIt
CFStringRef monthString, zoneString, ampmString
long month, day, year, hour, minute
CFStringRef dateString = @"March 7 2009 7:30pm EST"
DateFormatterRef df = fn DateFormatterInit
DateFormatterSetDateStyle( df, NSDateFormatterMediumStyle )
DateFormatterSetTimeStyle( df, NSDateFormatterMediumStyle )
DateFormatterSetDateFormat( df, @"MMMM d YYYY h:ma" )
CFArrayRef months = fn DateFormatterMonthSymbols( df )
CFCharacterSetRef spaceSet = fn CharacterSetWhitespaceSet
ScannerRef scanner = fn ScannerWithString( dateString )
ScannerSetCharactersToBeSkipped( scanner, fn CharacterSetWithCharactersInString(@": ") )
fn ScannerScanUpToCharactersFromSet( scanner, spaceSet, @monthString )
fn ScannerScanInteger( scanner, @day )
fn ScannerScanInteger( scanner, @year )
fn ScannerScanInteger( scanner, @hour )
fn ScannerScanInteger( scanner, @minute )
fn ScannerScanUpToCharactersFromSet( scanner, spaceSet, @ampmString )
fn ScannerScanUpToCharactersFromSet( scanner, spaceSet, @zoneString )
month = fn ArrayIndexOfObject( months, monthString ) + 1
if ( fn StringIsEqual( ampmString, @"pm" ) ) then hour += 12
DateComponentsRef comps = fn DateComponentsInit
DateComponentsSetMonth( comps, month )
DateComponentsSetDay( comps, day )
DateComponentsSetYear( comps, year )
DateComponentsSetHour( comps, hour + 12 )
DateComponentsSetMinute( comps, minute )
CFDateRef dt = fn CalendarDateFromComponents( fn CalendarCurrent, comps )
CFStringRef string = fn DateFormatterStringFromDate( df, dt )
string = fn StringByAppendingFormat( string, @" %@", zoneString )
print string
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
March 8 2009 7:30am EST
</pre>
 
=={{header|Go}}==
Line 1,697 ⟶ 1,796:
Langur currently uses the Go time package. Testing with Go 1.14.1 on Linux, the time package doesn't seem to parse "EST" correctly, and it seems to fail silently. Given these conditions, I use "-05:00" instead of "EST" in the input string.
 
{{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 .formatoformat = "January 2 2006 3:04pm MST" # output format
 
val .d1 = toDateTimedatetime .input, .iformat
val .d2 = .d1 + dtdr/PT12H/
val .d3 = toDateTimedatetime .d2, "US/Arizona"
val .d4 = toDateTimedatetime .d2, ZLSzls
val .d5 = toDateTimedatetime .d2, "Z"
val .d6 = toDateTimedatetime .d2, "+02:30"
val .d7 = toDateTimedatetime .d2, "EST"
 
writeln "input string: ", .input
writeln "input format string: ", .iformat
writeln "output format string: ", .formatoformat
writeln()
 
writeln $"original: \.d1; (\.d1:dt.format oformat;)"
writeln $"+12 hours: \.d2; (\.d2:dt.format oformat;)"
writeln $"in Arizona: \.d3; (\.d3:dt.format oformat;)"
writeln $"in local time zone: \.d4; (\.d4:dt.format oformat;)"
writeln $"in UTC: \.d5; (\.d5:dt.format oformat;)"
writeln $"+02:30 time zone: \.d6; (\.d6:dt.format oformat;)"
writeln $"in EST: \.d7; (\.d7:dt.format oformat;)"</syntaxhighlight>
 
{{out}}
Line 1,874 ⟶ 1,972:
<syntaxhighlight lang="mathematica">dstr = "March 7 2009 7:30pm EST";
DateString[DatePlus[dstr, {12, "Hour"}], {"DayName", " ", "MonthName", " ", "Day", " ", "Year", " ", "Hour24", ":", "Minute", "AMPM"}]</syntaxhighlight>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">import "dateTime"
import "stringUtil"
 
months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
]
 
date = "March 7 2009 7:30pm EST"
print "Original date/time : " + date
 
// change the date to standard format
items = date.split
month = months.indexOf(items[0]) + 1
day = items[1]
year = items[2]
time = items[3]
hour = time.split(":")[0].val
minute = time.split(":")[1][0:2]
pm = time.endsWith("pm")
if pm then hour = hour + 12
time = hour + ":" + minute
zone = items[4]
date = year + "-" + month + "-" + day + " " + time
 
// add 12 hours and display in original format
dval = dateTime.val(date) + 12*60*60
dfmt = "MMMM d yyyy h:mmtt"
date2 = dateTime.str(dval, dfmt) + " " + zone
print "12 hours later : " + date2
 
// change from EST to MST (2 hours earlier)
date3 = dateTime.str(dval - 2*60*60, dfmt) + " MST"
print "Adjusted to MST : " + date3</syntaxhighlight>
 
{{out}}
<pre>Original date/time : March 7 2009 7:30pm EST
12 hours later : March 8 2009 7:30am EST
Adjusted to MST : March 8 2009 5:30am MST
</pre>
 
=={{header|mIRC Scripting Language}}==
Line 2,565 ⟶ 2,705:
=={{header|Raku}}==
(formerly Perl 6)
 
Raku comes with a build-in DateTime type
Raku comes with a built-in DateTime type to support most aspects of standard civic time calculation
that are not dependent on cultural idiosyncraciesidiosyncrasies. <br>
 
Unfortunately, Raku does not yet have a date parsing module
(Unfortunately, Raku 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.
or maybe just due to laziness), but that just gives us another opportunity to demonstrate the built-in grammar support.
 
<syntaxhighlight lang="raku" line>my @month = <January February March April May June July August September October November December>;
my %month = flat (@month Z=> ^1..12), (@month».substr(0,3) Z=> ^1..12), 'Sept' => 89;
 
grammar US-DateTime {
Line 2,622 ⟶ 2,761:
$dt = $dt.later(hours => 12);
 
say "12 hours later, GMTUTC: $dt";
say "12 hours later, PSTPDT: $dt.in-timezone(-87 * 3600)";</syntaxhighlight>
{{out}}
<pre>12 hours later, GMTUTC: 2009-0203-08T12:30:00Z
12 hours later, PSTPDT: 2009-0203-08T0408T05:30:00-080008:00</pre>
 
=={{header|REBOL}}==
Line 2,723 ⟶ 2,862:
Original - March 7 2009 7:30pm EST
Manipulated - 08/03/2009 7:30am
</pre>
 
=={{header|RPL}}==
{{works with|HP|48}}
RPL time format does not handle time zones.
≪ "@" + → in
≪ { }
1 in SIZE '''FOR''' j
in j DUP SUB
'''CASE'''
"0123456789" OVER POS '''THEN''' STR→ '''END'''
DUP "a" ≥ OVER "z" ≤ AND '''THEN''' NUM 32 - CHR '''END'''
DUP "A" < OVER "Z" > OR '''THEN''' DROP ‘x’ '''END'''
'''END'''
'''IF''' OVER TYPE 5 ≠ '''THEN'''
'''IF''' DUP2 TYPE SWAP TYPE == '''THEN'''
'''IF''' DUP TYPE 2 ≠ '''THEN''' SWAP 10 * '''END''' +
'''ELSE'''
ROT ROT + SWAP
'''IF''' DUP ‘x’ SAME '''THEN''' DROP '''END'''
'''END END'''
'''NEXT'''
≫ ≫ '<span style="color:blue">ALPHANUM</span>' STO
≪ <span style="color:blue">ALPHANUM</span> → date
≪ { "JAN" "FEB" "MAR" "APR" "MAY" "JUN" "JUL" "AUG" "SEP" "OCT" "NOV" "DEC" }
date 1 GET 1 3 SUB POS 100 /
date 2 GET +
date 3 GET 1E6 / +
date 4 GET
date 5 GET 100 / +
'''IF''' date 6 GET "PM" == '''THEN''' 12 + '''END'''
≫ ≫ '<span style="color:blue">→DATIME</span>' STO
≪ <span style="color:blue">→DATIME</span> 1 +
'''IF''' DUP 24 > '''THEN''' 24 - SWAP 1 DATE+ SWAP '''END'''
TSTR
≫ '<span style="color:blue">TASK1</span>' STO
 
"March 7 2009 7:30pm EST" <span style="color:blue">TASK1</span>
{{out}}
<pre>
1: "SUN 08.03.09 07:30:00"
</pre>
 
Line 3,155 ⟶ 3,337:
<pre>Sun Mar 8 08:30:00 EDT 2009
Sun Mar 8 20:30:00 CST 2009</pre>
 
A version that works with the BSD/macOS version of date(1):
<syntaxhighlight lang="bash">epoch=$(( $(date -j -f '%B %d %Y %l:%M%p %Z' 'March 7 2009 7:30pm EST' +%s) + 43200 ))
date -r $epoch
TZ=Australia/Perth date -r $epoch</syntaxhighlight>
 
{{out}}
<pre>Sun Mar 8 08:30:55 EDT 2009
Sun Mar 8 21:30:55 AWDT 2009</pre>
 
=={{header|Wren}}==
{{libheader|Wren-date}}
<syntaxhighlight lang="ecmascriptwren">import "./date" for Date
 
var fmt = "mmmm| |d| |yyyy| |H|:|MM|am| |zz|"
885

edits