Date manipulation: Difference between revisions
→{{header|langur}}
Langurmonkey (talk | contribs) |
|||
(21 intermediate revisions by 7 users not shown) | |||
Line 1,040:
----
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun fix-time-string (calendar-string)
"If CALENDAR-STRING has no space between time and am/a.m./pm/p.m., add a space.
Return string with space between time and am/a.m./pm/p.m."
(replace-regexp-in-string "\\([[:digit:]]\\)\\([ap]\\)" "\\1 \\2" calendar-string))
(defun is-pm (calendar-string)
"Test if CALENDAR-STRING has PM/pm/P.M/p.m in it."
(string-match-p "[Pp][.]?[Mm]" calendar-string))
(defun is-hour-1-to-11 (a-calendar-list)
"Test if hour in A-CALENDAR-LIST is between 1 and 11."
(let ((hour-value))
(setq hour-value (nth 2 a-calendar-list))
(and (>= hour-value 1) (<= hour-value 11))))
(defun adjust-if-pm (time-as-string)
"If TIME-AS-STRING includes pm/PM, and hour is 1 to 11, add 12 hours.
Return CALENDAR-LIST modified if date is pm and hour is 1-11; otherwise
return CALENDAR-LIST of original TIME-AS-STRING."
(let ((calendar-list))
(setq calendar-list (parse-time-string time-as-string))
(if (and (is-pm time-as-string) (is-hour-1-to-11 calendar-list))
(decoded-time-add calendar-list (make-decoded-time :hour 12))
calendar-list)))
(defun add-hours (calendar-list number-of-hours)
"Add NUMBER-OF-HOURS to CALENDAR-LIST."
(decoded-time-add calendar-list (make-decoded-time :hour number-of-hours)))
(defun calc-future-time (string-calendar-date number-of-hours-in-future)
"Calculate future time by adding NUMBER-OF-HOURS-IN-FUTURE to STRING-CALENDAR-DATE ."
(let ((fixed-calendar-string)
(24-hour-calendar-list)
(calendar-list-future-time)
(coded-future-time))
(setq fixed-calendar-string (fix-time-string string-calendar-date))
(setq 24-hour-calendar-list (adjust-if-pm fixed-calendar-string))
(setq calendar-list-future-time (add-hours 24-hour-calendar-list number-of-hours-in-future))
(setq coded-future-time (encode-time calendar-list-future-time))
(format-time-string "%B %e %Y %R %p %Z" coded-future-time)))
</syntaxhighlight>
{{out}}
(calc-future-time "March 7 2009 7:30pm EST" 12)
<pre>
"March 8 2009 08:30 AM EDT"
</pre>
And a second solution, which modifies the ELisp timestamp to calculate 12 hours in the future
<syntaxhighlight lang="lisp">
(defun fix-time-string (calendar-string)
"If CALENDAR-STRING has no space between time and a.m./p.m., add a space."
(replace-regexp-in-string "\\([[:digit:]]\\)\\([ap]\\)" "\\1 \\2" calendar-string))
(defun is-pm (calendar-string)
"Test if CALENDAR-STRING has PM/pm in it."
(string-match-p "[Pp][Mm]" time-as-string))
(defun is-hour-1-to-11 (a-calendar-list)
"Test if hour in A-CALENDAR-LIST is between 1 and 11."
(let ((hour-value))
(setq hour-value (nth 2 a-calendar-list))
(and (>= hour-value 1) (<= hour-value 11))))
(defun adjust-if-pm (time-as-string)
"If TIME-AS-STRING includes pm/PM, and hour is 1 to 11, add 12 hours.
Return time as integer of seconds past the epoch."
(let ((calendar-list)
(temp-time-stamp)
(time-stamp-as-integer))
(setq calendar-list (parse-time-string time-as-string))
(setq temp-time-stamp (encode-time calendar-list))
(setq time-stamp-as-integer (time-convert temp-time-stamp 'integer))
(if (and (is-pm time-as-string) (is-hour-1-to-11 calendar-list))
(+ time-stamp-as-integer (* 12 60 60)) ; return time + 12 hours, so that hour is 13-23
time-stamp-as-integer))) ; return time unchanged, leaving hour 0-12
(defun add-seconds (start-time-stamp-integer number-of-seconds)
"Add NUMBER-OF-HOURS to START-TIME-STAMP-INTEGER."
(+ start-time-stamp-integer number-of-seconds))
(defun calc-future-time (string-calendar-date number-of-seconds-in-future)
"Calculate future time by adding NUMBER-OF-SECONDS-IN-FUTURE to STRING-CALENDAR-DATE ."
(let ((fixed-calendar-string)
(time-stamp-as-integer)
(coded-current-time)
(future-time-as-integer))
(setq fixed-calendar-string (fix-time-string string-calendar-date))
(setq time-stamp-as-integer (adjust-if-pm fixed-calendar-string))
(setq future-time-as-integer (add-seconds time-stamp-as-integer number-of-seconds-in-future))
(format-time-string "%B %e %Y %R %p %Z" future-time-as-integer)))
</syntaxhighlight>
{{out}}
(calc-future-time "March 7 2009 7:30pm EST" (* 12 60 60))
<pre>
"March 8 2009 08:30 AM EDT"
</pre>
=={{header|Erlang}}==
Line 1,307 ⟶ 1,415:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?timeZone=America%2FNew_York&script=examples/Date_manipulation}}
'''Solution'''
'''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.
[[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,693 ⟶ 1,902:
=={{header|langur}}==
<syntaxhighlight lang="langur">
val input = "March 7 2009 7:30pm -05:00"
val iformat = "January 2 2006 3:04pm -07:00"
val oformat = "January 2 2006 3:04pm MST"
val d1 = datetime(input, iformat)
val d2 = d1 + dr/T12h/
val d3 = datetime(d2, "US/Arizona")
val d4 = datetime(d2, zls)
val d5 = datetime(d2, "Z")
val d6 = datetime(d2, "+02:30")
val d7 = datetime(d2, "EST")
writeln "input string: ", input
writeln "input format string: ", iformat
writeln "output format string: ", oformat
writeln()
writeln
writeln
writeln
writeln
writeln
writeln
writeln
</syntaxhighlight>
{{out}}
Line 1,872 ⟶ 2,080:
<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,563 ⟶ 2,813:
=={{header|Raku}}==
(formerly Perl 6)
Raku comes with a built-in DateTime type to support most aspects of standard civic time calculation
that are not dependent on cultural
<syntaxhighlight lang="raku" line>my @month = <January February March April May June July August September October November December>;
my %month = flat (@month Z=>
grammar US-DateTime {
Line 2,620 ⟶ 2,869:
$dt = $dt.later(hours => 12);
say "12 hours later,
say "12 hours later,
{{out}}
<pre>12 hours later,
12 hours later,
=={{header|REBOL}}==
Line 2,721 ⟶ 2,970:
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,153 ⟶ 3,445:
<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="
var fmt = "mmmm| |d| |yyyy| |H|:|MM|am| |zz|"
|