36
7
In the 1990s, COBOL computer engineers worked out a way to extend six-digit date fields by converting them to YYYDDD
where YYY
is the year - 1900
and DDD
is the day of the year [001 to 366]
. This scheme could extend the maximum date to 2899-12-31
.
In the year 2898, the engineers started panicking because their 900 year old code bases were going to fail. Being from the year 2898, they just used their time machine to send a lone Codeinator to the year 1998 with this algorithm and the task of getting it implemented as widely as possible:
Use a scheme
PPQQRR
where if01 ≤ QQ ≤ 12
then it's a standardYYMMDD
date in the 1900s, but ifQQ > 12
then it represents the days after2000-01-01
in base 100 forPP
andRR
but base 87 forQQ - 13
.
This scheme extends far beyond year 2899 and is also backwards compatible with standard dates, so no modifications of existing archives are required.
Some examples:
PPQQRR YYYY-MM-DD
000101 1900-01-01 -- minimum conventional date suggested by J. Allen
010101 1901-01-01 -- edge case suggested by J. Allen
681231 1968-12-31 -- as above
991231 1999-12-31 -- maximum conventional date
001300 2000-01-01 -- zero days after 2000-01-01
008059 2018-07-04 -- current date
378118 2899-12-31 -- maximum date using YYYDDD scheme
999999 4381-12-23 -- maximum date using PPQQRR scheme
Your challenge is to write a program or function to accept input as PPQQRR
and output as an ISO date YYYY-MM-DD
. Input method can be parameter, console or command line, whatever is easiest.
For your amusement, here is a noncompeting solution in COBOL-85:
IDENTIFICATION DIVISION.
PROGRAM-ID. DATE-CONVERSION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 T PIC 9(8).
01 U PIC 9(8).
01 D VALUE '999999'.
05 P PIC 9(2).
05 Q PIC 9(2).
05 R PIC 9(2).
01 F.
05 Y PIC 9(4).
05 M PIC 9(2).
05 D PIC 9(2).
PROCEDURE DIVISION.
IF Q OF D > 12 THEN
MOVE FUNCTION INTEGER-OF-DATE(20000101) TO T
COMPUTE U = R OF D + 100 * ((Q OF D - 13) + 87 * P OF D) + T
MOVE FUNCTION DATE-OF-INTEGER(U) TO F
DISPLAY "Date: " Y OF F "-" M OF F "-" D OF F
ELSE
DISPLAY "Date: 19" P OF D "-" Q OF D "-" R OF D
END-IF.
STOP RUN.
May output be a
[Y,M,D]
numeric list? Does it make a difference if that is my language's normal date format? – Adám – 2018-07-05T05:33:57.0074"But do not program in COBOL if you can avoid it." -- The Tao of Programming – tsh – 2018-07-05T07:57:51.300
9
Related HNQ: How to attract people to work on very old and outdated technologies?
– pipe – 2018-07-05T09:14:41.573Why from year 2000 and not 2900? – user202729 – 2018-07-05T10:59:45.270
1@user202729 because
yymmdd
doesn't work for for years>=2000
, that's the entire point of the Y2K debacle. – JAD – 2018-07-05T11:15:38.0672@Adám -- In the spirit of COBOL which is very fussy w.r.t. I/O, I'll have to say it needs to be in ISO
yyyy-mm-dd
format. – None – 2018-07-05T11:51:59.213Can we take input as an integer rather than as a string? – Giuseppe – 2018-07-05T12:02:18.347
4@Giuseppe -- In the spirit of COBOL which doesn't really differentiate strings and numbers, yes! Provided you can input leading zeros, e.g.
001300
. – None – 2018-07-05T12:04:05.223Suggested test cases:
000101
and010101
. – Jonathan Allan – 2018-07-05T14:30:13.793Additional suggested test cases: 010367, 000000, -99101, RABBIT – Jay – 2018-07-05T15:25:07.483
@Jay -- CodeGolf usually assumes input is valid. BTW
000000
is often used as an empty date in COBOL (akin tonull
). You can crash many a system by forcing a date field to all zeroes in a record! – None – 2018-07-05T15:31:01.7101@JonathanAllan -- Thank you, have added them including
681231
. I was thinking the conventional dates were too trivial to bother with but your comments below have convinced me otherwise! – None – 2018-07-05T15:49:04.047Do we ignore leap years? – Alexander Revo – 2018-07-12T14:03:09.213
@AlexanderRevo - No, leap years are important. – None – 2018-07-12T15:30:09.567