2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,7 +1,9 @@
{{omit from|ML/I}}
{{omit from|PARI/GP|No real capacity for string manipulation}}
;Task:
Given the date string "March 7 2009 7:30pm EST", <br>
output the time 12 hours later in any human-readable format.
As extra credit, display the resulting time in a time zone different from your own.
<br><br>

View file

@ -0,0 +1,121 @@
identification division.
program-id. date-manipulation.
environment division.
configuration section.
repository.
function all intrinsic.
data division.
working-storage section.
01 given-date.
05 filler value z"March 7 2009 7:30pm EST".
01 date-spec.
05 filler value z"%B %d %Y %I:%M%p %Z".
01 time-struct.
05 tm-sec usage binary-long.
05 tm-min usage binary-long.
05 tm-hour usage binary-long.
05 tm-mday usage binary-long.
05 tm-mon usage binary-long.
05 tm-year usage binary-long.
05 tm-wday usage binary-long.
05 tm-yday usage binary-long.
05 tm-isdst usage binary-long.
05 tm-gmtoff usage binary-c-long.
05 tm-zone usage pointer.
01 scan-index usage pointer.
01 time-t usage binary-c-long.
01 time-tm usage pointer.
01 reform-buffer pic x(64).
01 reform-length usage binary-long.
01 current-locale usage pointer.
01 iso-spec constant as "YYYY-MM-DDThh:mm:ss+hh:mm".
01 iso-date constant as "2009-03-07T19:30:00-05:00".
01 date-integer pic 9(9).
01 time-integer pic 9(9).
procedure division.
call "strptime" using
by reference given-date
by reference date-spec
by reference time-struct
returning scan-index
on exception
display "error calling strptime" upon syserr
end-call
display "Given: " given-date
if scan-index not equal null then
*> add 12 hours, and reform as local
call "mktime" using time-struct returning time-t
add 43200 to time-t
perform form-datetime
*> reformat as Pacific time
set environment "TZ" to "PST8PDT"
call "tzset" returning omitted
perform form-datetime
*> reformat as Greenwich mean
set environment "TZ" to "GMT"
call "tzset" returning omitted
perform form-datetime
*> reformat for Tokyo time, as seen in Hong Kong
set environment "TZ" to "Japan"
call "tzset" returning omitted
call "setlocale" using by value 6 by content z"en_HK.utf8"
returning current-locale
on exception
display "error with setlocale" upon syserr
end-call
move z"%c" to date-spec
perform form-datetime
else
display "date parse error" upon syserr
end-if
*> A more standard COBOL approach, based on ISO8601
display "Given: " iso-date
move integer-of-formatted-date(iso-spec, iso-date)
to date-integer
move seconds-from-formatted-time(iso-spec, iso-date)
to time-integer
add 43200 to time-integer
if time-integer greater than 86400 then
subtract 86400 from time-integer
add 1 to date-integer
end-if
display " " substitute(formatted-datetime(iso-spec
date-integer, time-integer, -300), "T", "/")
goback.
form-datetime.
call "localtime" using time-t returning time-tm
call "strftime" using
by reference reform-buffer
by value length(reform-buffer)
by reference date-spec
by value time-tm
returning reform-length
on exception
display "error calling strftime" upon syserr
end-call
if reform-length > 0 and <= length(reform-buffer) then
display " " reform-buffer(1 : reform-length)
else
display "date format error" upon syserr
end-if
.
end program date-manipulation.

View file

@ -1,5 +1,5 @@
my @month = <January February March April May June July August September October November December>;
my %month = (@month Z=> ^12).flat, (@month».substr(0,3) Z=> ^12).flat, 'Sept' => 8;
my %month = flat (@month Z=> ^12), (@month».substr(0,3) Z=> ^12), 'Sept' => 8;
grammar US-DateTime {
rule TOP { <month> <day>','? <year>','? <time> <tz> }
@ -46,7 +46,7 @@ my $timezone = $<tz>.ast * 3600;
my $dt = DateTime.new(:$year, :$month, :$day, :$hour, :$minute, :$timezone).in-timezone(0);
$dt .= delta(12,hour);
$dt = $dt.later(hours => 12);
say "12 hours later, GMT: $dt";
say "12 hours later, PST: $dt.in-timezone(-8 * 3600)";

View file

@ -0,0 +1,98 @@
EnableExplicit
Procedure.i ToPBDate(Date$, *zone.String)
Protected year, month, day, hour, minute
Protected month$, temp$, time$, pm$, zone$
month$ = StringField(date$, 1, " ")
day = Val(StringField(date$, 2, " "))
year = Val(StringField(date$, 3, " "))
time$ = StringField(date$, 4, " ")
zone$ = StringField(date$, 5, " ")
Select month$
Case "January" : month = 1
Case "February" : month = 2
Case "March" : month = 3
Case "April" : month = 4
Case "May" : month = 5
Case "June" : month = 6
Case "July" : month = 7
Case "August" : month = 8
Case "September" : month = 9
Case "October" : month = 10
Case "November" : month = 11
Case "December" : month = 12
EndSelect
hour = Val(StringField(time$, 1, ":"))
temp$ = StringField(time$, 2, ":")
minute = Val(Left(temp$, 2))
pm$ = Right(temp$, 2)
If pm$ = "am"
If hour = 12 : hour = 0 : EndIf
Else
If hour <> 12 : hour + 12 : EndIf
EndIf
*zone\s = zone$
ProcedureReturn Date(year, month, day, hour, minute, 0)
EndProcedure
Procedure.s FromPBDate(Date, zone$)
Protected year$ = Str(Year(Date))
Protected month = Month(Date)
Protected day$ = Str(Day(Date))
Protected hour = Hour(Date)
Protected minute = Minute(Date)
Protected month$, time$, pm$, result$
Select month
Case 1 : month$ = "January"
Case 2 : month$ = "February"
Case 3 : month$ = "March"
Case 4 : month$ = "April"
Case 5 : month$ = "May"
Case 6 : month$ = "June"
Case 7 : month$ = "July"
Case 8 : month$ = "August"
Case 9 : month$ = "September"
Case 10 : month$ = "October"
Case 11 : month$ = "November"
Case 12 : month$ = "December"
EndSelect
If hour > 12
hour - 12
pm$ = "pm"
ElseIf hour = 12
pm$ = "pm"
Else
If hour = 0 : hour = 12 : EndIf
pm$ = "am"
EndIf
time$ = Str(hour) + ":" + RSet(Str(minute), 2, "0") + pm$
result$ = month$ + " " + day$ + " " + year$ + " " + time$ + " " + zone$
ProcedureReturn result$
EndProcedure
Define date
Define date1$, date2$
Define zone.String
If OpenConsole()
date1$ = "March 7 2009 7:30pm EST"
PrintN("Starting date/time : " + date1$)
date = ToPBDate(date1$, @zone)
date = AddDate(date, #PB_Date_Hour, 12); add 12 hours
date2$ = FromPBDate(date, zone\s)
PrintN("12 hours later : " + date2$)
date = AddDate(date, #PB_Date_Hour, 5); adjust to GMT
date2$ = FromPBDate(date, "GMT")
PrintN("Or in GMT timezone : " + date2$)
PrintN("")
PrintN("Press any key to close the console")
Repeat: Delay(10) : Until Inkey() <> ""
CloseConsole()
EndIf

View file

@ -1,13 +1,16 @@
/*REXX pgm to add 12 hours to a date & time, showing the before & after.*/
aDate = 'March 7 2009 7:30pm EST'
parse var aDate mon dd yyyy hhmm tz . /*obtain the various parts&pieces*/
mins = time('M',hhmm,'C') /*get the # minutes past midnight*/
mins = mins + (12*60) /*add twelve hours to timestamp. */
nMins = mins // 1440 /*compute #min into same/next day*/
days = mins % 1440 /*compute number of days "added".*/
aBdays = date('B',dd left(mon,3) yyyy) /*# of base days since REXX epoch*/
nBdays = aBdays + days /*now, add the # of days "added".*/
nDate = date(,nBdays,'B') /*calculate the new date (maybe).*/
nTime = time('C',nMins,'M') /* " " " time " */
say aDate ' + 12 hours ' ndate ntime tz /*display new timestamp.*/
/*stick a fork in it, we're done.*/
/*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.*/
parse var aDate mon dd yyyy hhmm tz . /*obtain the various parts and pieces. */
mins = time('M', hhmm, "C") /*get the number minutes past midnight.*/
mins = mins + (12*60) /*add twelve hours to the timestamp.*/
nMins = mins // 1440 /*compute number min into same/next day*/
days = mins % 1440 /*compute number of days added to dats.*/
aBdays = date('B', dd left(mon,3) yyyy) /*number of base days since REXX epoch.*/
nBdays = aBdays + days /*now, add the number of days added. */
nDate = date(,nBdays, 'B') /*calculate the new date (maybe). */
nTime = time('C', nMins, "M") /* " " " time " */
say aDate ' + 12 hours ' ndate ntime tz /*display the new timestamp to console.*/
/*stick a fork in it, we're all done. */

View file

@ -4,7 +4,7 @@ $ include "seed7_05.s7i";
const func time: parseDate (in string: dateStri) is func
result
var time: result is time.value;
var time: aTime is time.value;
local
const array string: monthNames is [] ("January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November", "December");
@ -13,23 +13,23 @@ const func time: parseDate (in string: dateStri) is func
var string: timeStri is "";
begin
dateParts := split(dateStri, ' ');
result.year := integer parse (dateParts[3]);
result.month := 1;
while monthNames[result.month] <> dateParts[1] do
incr(result.month);
aTime.year := integer parse (dateParts[3]);
aTime.month := 1;
while monthNames[aTime.month] <> dateParts[1] do
incr(aTime.month);
end while;
result.day := integer parse (dateParts[2]);
aTime.day := integer parse (dateParts[2]);
timeStri := dateParts[4];
if endsWith(timeStri, "am") then
result.hour := integer parse (timeStri[.. pred(pos(timeStri, ':'))]);
aTime.hour := integer parse (timeStri[.. pred(pos(timeStri, ':'))]);
elsif endsWith(timeStri, "pm") then
result.hour := integer parse (timeStri[.. pred(pos(timeStri, ':'))]) + 12;
aTime.hour := integer parse (timeStri[.. pred(pos(timeStri, ':'))]) + 12;
else
raise RANGE_ERROR;
end if;
result.minute := integer parse (timeStri[succ(pos(timeStri, ':')) .. length(timeStri) - 2]);
aTime.minute := integer parse (timeStri[succ(pos(timeStri, ':')) .. length(timeStri) - 2]);
if dateParts[5] <> "UTC" then
result.timeZone := 60 * integer parse (dateParts[5][4 ..]);
aTime.timeZone := 60 * integer parse (dateParts[5][4 ..]);
end if;
end func;