root/tags/lgt2311/contributions/iso8601.lgt

Revision 2077, 29.3 KB (checked in by pmoura, 4 years ago)

Reformatted header.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/******************************************************************************
2
3    Library: ISO8601.PL
4    Copyright (c) 2004-2005 Daniel L. Dudley
5
6    Purpose: ISO 8601 (and European civil calendar) compliant library of date
7             and time (clock) related predicates. That is, an ISO 8601 handler.
8
9    Author:  Daniel L. Dudley
10    Created: 2004-02-18
11
12******************************************************************************/
13
14
15:- object(iso8601).
16
17    :- info([
18        version is 1.0,
19        author is 'Daniel L. Dudley',
20        date is 2005/04/11,
21        comment is 'ISO 8601 (and European civil calendar) compliant library of date predicates.',
22        remarks is [
23            'Scope:' - 'This object currently provides a powerful, versatile and efficient set of date-handling predicates, which--thanks to Logtalk--may be used as is on a wide range of Prolog compilers. Besides taking time to familiarize oneself with each predicate, the user should take note of the following information.',
24            'Validation of dates: ' - 'Date parts are not validated--that is the caller''s responsibility! However, not being quite heartless yet, we do provide a predicate for this purpose.',
25            'Date arithmetic:' - 'Many of the examples illustrate a simplified method of doing date arithmetic. Note, however, that we do not generally recommend this practice--it is all too easy to make mistakes. The safest way of finding the day difference between two dates is to first convert the dates to their Julian day numbers and then subtract one from the other. Similarly, the safe way to add or subtract a day offset to a particular date is to first convert the date to its Julian day number, add or subtract the day offset, and then convert the result to its corresponding date.',
26            'BC years' - 'ISO 8601 specifies that the Gregorian calendar be used, yet requires that years prior to 1 AD be handled arithmetically, i.e., the year we know as 1 BC is year 0, 2 BC is year -1, 3 BC is year -2 and so on. We do not follow ISO 8601 with regard to the handling of BC years. Our date predicates will accept and interpret an input year 0 as 1 BC; however, a negative year, Year, should always be interpreted as abs(Year) =:= Year BC. We believe that the average person will find our handling of BC years more user-friendly than the ISO 8601 one, but we encourage feedback from users with a view to a possible change in future versions.',
27            'Week numbers:' - 'It is possible for a day (date) to have a week number that belongs to another year. Up to three of the first days of a calendar year may belong to the last week (number) of the prior calendar year, and up to three days of the last days of a calendar year may belong to the first week (number) of the next calendar year. It for this reason that the Week parameter in date/6-7 is a compound term, namely week(WeekNo,ActualYear).',
28            'Computation of Gregorian Easter Sunday:' - 'The algorithm is based upon the "Gaussian rule". Proleptic use is limited to years > 1582 AD, that is, after the introduction of the Gregorian calendar.',
29            'Some Christian feast day offsets from Easter Sunday:' - 'Carnival Monday: -48 days, Mardi Gras (Shrove Tuesday): -47 days, Ash Wednesday: -46 days, Palm Sunday: -7 days, Easter Friday: -2 days, Easter Saturday: -1 day, Easter Monday: +1 day, Ascension of Christ: +39 days, Whitsunday: +49 days, Whitmonday: +50 days, Feast of Corpus Christi: +60 days.'
30            ]]).
31
32    % CORE PREDICATES:
33
34    :- public(date/4).
35    :- mode(date(?integer, ?integer, ?integer, ?integer), zero_or_one).
36    :- info(date/4, [
37        comment is 'Get the system date and/or its Julian Day # or convert a Julian Day # to/from given date parts.',
38        arguments is [
39            'JD' - 'Julian day serial number',
40            'Year' - '0 or negative if converted BC year, positive otherwise',
41            'Month' - 'Normally an integer between 1 and 12 inclusive',
42            'Day' - 'Normally an integer between 1 and 31 inclusive depending upon month'],
43        examples is [
44            'Current date (i.e., today):' - date(JD,Y,M,D) - {JD = 2453471, Y = 2005, M = 4, D = 10},
45            'Convert a date to its Julian day number:' - date(JD,2000,2,29) - {JD = 2451604},
46            'Convert a Julian day number to its date:' - date(2451604,Yr,Mth,Day) - {Yr = 2000, Mth = 2, Day = 29},
47            'What is the date of day # 60 in year 2000?' - date(J,2000,1,60) - {J = 2451604},
48            'What is the Julian of the 1st day prior to 2000-1-1?' - date(J,2000,1,0) - {J = 2451544},
49            'What is the Julian of the 60th day prior to 2000-1-1?' - date(J,2000,1,-59) - {J = 2451485},
50            'Illegal date is auto-adjusted (see also next query)' - date(JD,1900,2,29) - {JD = 2415080},
51            'This is the correct date!' - date(2415080,Y,M,D) - {Y = 1900, M = 3, D = 1}]]).
52
53    :- public(date/5).
54    :- mode(date(?integer, ?integer, ?integer, ?integer, ?integer), zero_or_one).
55    :- info(date/5, [
56        comment is 'Ditto date/4 + get/check its day-of-week #.',
57        arguments is [
58            'JD' - 'Julian day serial number',
59            'Year' - '0 or negative if converted BC year, positive otherwise',
60            'Month' - 'Normally an integer between 1 and 12 inclusive',
61            'Day' - 'Normally an integer between 1 and 31 inclusive depending upon month',
62            'DoW' - 'Day of week, where Monday=1, Tuesday=2, ..., Sunday=7'],
63        examples is [
64            'Get the Julian and the day-of-week # of a date:' - date(JD,2000,2,29,DoW) - {JD = 2451604, DoW = 2},
65            'Check the validity of a given date (day-of-week is 2, not 4):' - date(JD,2002,3,5,4) - {no},
66            'Get the Julian day of a given date if it is a Sunday:' - date(JD,2004,2,29,7) - {JD = 2453065},
67            'Get the date and day-of-week # of a Julian:' - date(2451545,Y,M,D,DoW) - {Y = 2000, M = 1, D = 1, DoW = 6}]]).
68
69    :- public(date/6).
70    :- mode(date(?integer, ?integer, ?integer, ?integer, ?integer, ?compound), zero_or_one).
71    :- info(date/6, [
72        comment is 'Ditto date/5 + get/check its week #.',
73        arguments is [
74            'JD' - 'Julian day serial number',
75            'Year' - '0 or negative if converted BC year, positive otherwise',
76            'Month' - 'Normally an integer between 1 and 12 inclusive',
77            'Day' - 'Normally an integer between 1 and 31 inclusive depending upon month',
78            'DoW' - 'Day of week, where Monday=1, Tuesday=2, ..., Sunday=7',
79            'Week' - 'Compound term, week(WeekNo,ActualYear), of a day'],
80        examples is [
81            'Get the day-of-week and week number of a date:' - date(_,2000,1,1,DoW,Wk) - {DoW = 6, Wk = week(52,1999)},
82            'Get the week number and year of this week:' - date(_,_,_,_,_,Wk) - {Wk = week(7, 2004)},
83            'Get the Julian number and the week of a date if it is a Sunday:' - date(JD,2004,2,29,7,Wk) - {JD = 2453065, Wk = week(9,2004)},
84            'Get the day-of-week and week of a Julian day number:' - date(2453066,_,_,_,DoW,Wk) - {DoW = 1, Wk = week(10,2004)},
85            'Check that given date data matches:' - date(_,2004,3,1,1,week(10,2004)) - {yes},
86            'What is the date of a day of week (default is 1) in given week # and year?' - date(_,Y,M,D,DoW,week(26,2004)) - {Y = 2004, M = 6, D = 21, DoW = 1},
87            'Ditto for Sunday:' - date(_,Y,M,D,7,week(1,2005)) - {Y = 2005, M = 1, D = 9},
88            'Ditto for Tuesday in following week:' - date(_,Y,M,D,9,week(1,2005)) - {Y = 2005, M = 1, D = 11},
89            'Ditto for Thursday in the prior week:' - date(_,Y,M,D,4,week(0,2005)) - {Y = 2004, M = 12, D = 30},
90            'Ditto for Tuesday two weeks prior:' - date(_,Y,M,D,2,week(-1,2005)) - {Y = 2004, M = 12, D = 21},
91            'Ditto for Saturday:' - date(_,Y,M,D,6,week(53,2004)) - {Y = 2005, M = 1, D = 1},
92            'Ditto for Monday (note automatic compensation of nonexistent week number):' - date(_,Y,M,D,1,week(60,2004)) - {Y = 2005, M = 2, D = 14}]]).
93
94    :- public(date/7).
95    :- mode(date(?integer, ?integer, ?integer, ?integer, ?integer, ?compound, ?integer), zero_or_one).
96    :- info(date/7, [
97        comment is 'Ditto date/6 + get/check its day-of-year #.',
98        arguments is [
99            'JD' - 'Julian day serial number',
100            'Year' - '0 or negative if converted BC year, positive otherwise',
101            'Month' - 'Normally an integer between 1 and 12 inclusive',
102            'Day' - 'Normally an integer between 1 and 31 inclusive depending upon month',
103            'DoW' - 'Day of week, where Monday=1, Tuesday=2, ..., Sunday=7',
104            'Week' - 'Compound term, week(WeekNo,ActualYear), of a day',
105            'DoY' - 'Day of year (NB! calendar year, not week # year)'],
106        examples is [
107            'Get the date and day-of-year of a Julian number:' - date(2451649,Year,Month,Day,_,_,DoY) - {Year = 2000, Month = 4, Day = 14, DoY = 105},
108            'Get the Julian number, week number and day-of-year of a date, confirming that it is a Sunday:' - date(JD,2004,2,29,7,Wk,DoY) - {JD = 2453065, Wk = week(9,2004), DoY = 60},
109            'Confirm that a date is, in fact, a specific day-of-year:' - date(_,2004,3,1,_,_,61) - {yes},
110            'Get the Julian number, week day and day-of-year of a date:' - date(JD,2004,10,18,DoW,_,DoY) - {JD = 2453297, DoW = 1, DoY = 292},
111            'Get today''s day-of-year:' - date(_,_,_,_,_,_,DoY) - {DoY = 54},
112            'Get all missing date data (excl. Julian number) for the 60th calendar day of 2004:' - date(_,2004,Month,Day,DoW,Week,60) - {Month = 2, Day = 29, DoW = 7, Week = week(9,2004)},
113            'Match given date data and, if true, return the missing data (excl. Julian number):' - date(_,2004,3,Day,DoW,Week,61) - {Day = 1, DoW = 1, Week = week(10,2004)},
114            'Ditto (the 61st day-of-year cannot be both day 1 and 2 of the month):' - date(_,2004,Month,2,DoW,Week,61) - {no}]]).
115
116    :- public(date_string/3).
117    :- mode(date_string(+atom, +integer, ?atom), zero_or_one).
118    :- mode(date_string(+atom, ?list, ?atom), zero_or_one).
119    :- info(date_string/3, [
120        comment is 'Conversion between an ISO 8601 compliant date string and its components (truncated and expanded date representations are currently unsupported). Note that date components are not validated; that is the caller''s responsibility!',
121        arguments is [
122            'Format' - 'ISO 8601 format',
123            'Components' - 'When bound and String is free, either a Julian number or a [Year,Month,Day] term; it binds to the system day/date if free When free and String is bound, it binds to an integer list representing the numeric elements of String',
124            'String' - 'ISO 8601 formatted string correspondent to Components'],
125        examples is [
126            'Date, complete, basic (section 5.2.1.1):' - date_string('YYYYMMDD',[2004,2,29],Str) - {Str = '20040229'},
127            'Date, complete, basic (section 5.2.1.1):' - date_string('YYYYMMDD',Day,'20040229') - {Day = [2004,2,29]},
128            'Date, complete, extended (section 5.2.1.1):' - date_string('YYYY-MM-DD',[2003,12,16],Str) - {Str = '2003-12-16'},
129            'Date, complete, extended (section 5.2.1.1):' - date_string('YYYY-MM-DD',Day,'2003-12-16') - {Day = [2003,12,16]},
130            'Date, complete, extended (section 5.2.1.1):' - date_string('YYYY-MM-DD',_,Str) - {Str = '2004-02-17'},
131            'Date, complete, extended (section 5.2.1.1):' - date_string('YYYY-MM-DD',Day,'2004-02-17') - {Day = [2004,2,17]},
132            'Date, reduced, month (section 5.2.1.2 a):' - date_string('YYYY-MM',[2004,9,18],Str) - {Str = '2004-09'},
133            'Date, reduced, month (section 5.2.1.2 a):' - date_string('YYYY-MM',Day,'2004-09') - {Day = [2004,9]},
134            'Date, reduced, year (section 5.2.1.2 b):' - date_string('YYYY',[1900,7,24],Str) - {Str = '1900'},
135            'Date, reduced, year (section 5.2.1.2 b):' - date_string('YYYY',Day,'1900') - {Day = [1900]},
136            'Date, reduced, century (section 5.2.1.2 c):' - date_string('YY',2456557,Str) - {Str = '20'},
137            'Date, reduced, century (section 5.2.1.2 c):' - date_string('YY',Day,'20') - {Day = [20]},
138            'Date, ordinal, complete (section 5.2.2.1):' - date_string('YYYYDDD',[2005,3,25],Str) - {Str = '2005084'},
139            'Date, ordinal, complete (section 5.2.2.1):' - date_string('YYYYDDD',Day,'2005084') - {Day = [2005,84]},
140            'Date, ordinal, extended (section 5.2.2.1):' - date_string('YYYY-DDD',[1854,12,4],Str) - {Str = '1854-338'},
141            'Date, ordinal, extended (section 5.2.2.1):' - date_string('YYYY-DDD',Day,'1854-338') - {Day = [1854,338]},
142            'Week, complete, basic (section 5.2.3.1):' - date_string('YYYYWwwD',[2000,1,2],Str) - {Str = '1999W527'},
143            'Week, complete, basic (section 5.2.3.1):' - date_string('YYYYWwwD',Day,'1999W527') - {Day = [1999,52,7]},
144            'Week, complete, extended (section 5.2.3.1):' - date_string('YYYY-Www-D',[2003,12,29],Str) - {Str = '2004-W01-1'},
145            'Week, complete, extended (section 5.2.3.1):' - date_string('YYYY-Www-D',Day,'2004-W01-1') - {Day = [2004,1,1]},
146            'Week, complete, extended (section 5.2.3.1):' - date_string('YYYY-Www-D',2453167,Str) - {Str = '2004-W24-4'},
147            'Week, complete, extended (section 5.2.3.1):' - date_string('YYYY-Www-D',Day,'2004-W24-4') - {Day = [2004,24,4]},
148            'Week, reduced, basic (section 5.2.3.2):' - date_string('YYYYWww',[2004,2,29],Str) - {Str = '2004W09'},
149            'Week, reduced, basic (section 5.2.3.2):' - date_string('YYYYWww',Day,'2004W09') - {Day = [2004,9]},
150            'Week, reduced, extended (section 5.2.3.2):' - date_string('YYYY-Www',[2004,2,29],Str) - {Str = '2004-W09'},
151            'Week, reduced, extended (section 5.2.3.2):' - date_string('YYYY-Www',Day,'2004-W09') - {Day = [2004,9]}]]).
152
153    % MISCELLANEOUS PREDICATES (GOODIES):
154
155    :- public(valid_date/3).
156    :- mode(valid_date(+integer, +integer, +integer), zero_or_one).
157    :- info(valid_date/3, [
158        comment is 'Validate a given date in the Gregorian calendar.',
159        argnames is ['Year', 'Month', 'Day'],
160        examples is [
161            'Yes, the recent millenium was a leap year:' - valid_date(2000,2,29) - {yes},
162            '2004 was also a leap year:' - valid_date(2004,2,29) - {yes},
163            'Only 30 days in April:' - valid_date(2004,4,31) - {no},
164            '1 BC was a leap year:' - valid_date(-1,2,29) - {yes}
165      ]]).
166
167    :- public(leap_year/1).
168    :- mode(leap_year(?integer), zero_or_one).
169    :- info(leap_year/1, [
170        comment is 'Succeed if given year is a leap year in the Gregorian calendar.',
171        arguments is [
172            'Year' - 'The Gregorian calendar year to investigate. If free, it binds to the system year'],
173        examples is [
174            'No, the prior centenary was not a leap year:' - leap_year(1900) - {no},
175            'The recent millenium:' - leap_year(2000) - {yes},
176            'This year:' - leap_year(Year) - {Year = 2004},
177            'This year (equivalent to prior query):' - leap_year(_) - {yes},
178            'Next centennial:' - leap_year(2100) - {no},
179            'Year 0, equivalent to 1 BC:' - leap_year(0) - {yes},
180            '1 BC' - leap_year(-1) - {yes},
181            '4 BC' - leap_year(-4) - {no},
182            '5 BC' - leap_year(-5) - {yes}
183        ]]).
184
185    :- public(calendar_month/3).
186    :- mode(calendar_month(?integer, ?integer, -compound), zero_or_one).
187    :- info(calendar_month/3, [
188        comment is 'Compute a calendar month.',
189        arguments is [
190            'Year' - 'The calendar year',
191            'Month' - 'The calendar month',
192            'Calendar' - 'A compound term, m/3, composed of three main arguments specifying year, month, and a list of week and week day numbers (calendar body).'],
193        examples is [
194            'Compute the calendar of March, 2005:' - calendar_month(2005, 3, Calendar) - {Calendar = m(2005, 3,[w( 9, [ 0123456]),w(10, [ 789, 10, 11, 12, 13]),w(11, [14, 15, 16, 17, 18, 19, 20]),w(12, [21, 22, 23, 24, 25, 26, 27]),w(13, [28, 29, 30, 3100, 0]),w( 0, [ 0000000])])}]]).
195
196    :- public(easter_day/3).
197    :- mode(easter_day(?integer, -integer, -integer), zero_or_one).
198    :- info(easter_day/3, [
199        comment is 'Compute a Gregorian Easter Sunday.',
200        arguments is [
201            'Year' - 'Integer specifying the year to be investigated',
202            'Month' - 'Month in which Easter Sunday falls for given year',
203            'Day'- 'Day of month in which Easter Sunday falls for given year'],
204        examples is [
205            'Compute Easter Sunday for a particular year:' - easter_day(2006,Month,Day) - {Month=4, Day=16},
206            'Compute Easter Sunday for the current year:' - easter_day(Y,M,D) - {Y = 2005, M = 3, D = 27}
207            ]]).
208
209
210    /************************
211     ISO 8601 DATE PREDICATES
212     ************************/
213
214    %==============================================================================
215    % date(?JD, ?Year, ?Month, ?Day)
216
217    date(JD,Year,Month,Day) :-
218        (  var(JD), var(Year),  var(Month),  var(Day)
219            -> % GET THE SYSTEM DATE AND ITS JULIAN DAY SERIAL NUMBER:
220            {'$lgt_current_date'(Year, Month, Day)}
221            ;  true
222        ),
223        (  var(JD), nonvar(Year),  nonvar(Month),  nonvar(Day)
224            -> % CORRIGATE BC/AD CALENDAR YEARS TO FIT 0-BASED ALGORITHM:
225            (Year < 0 -> Year1 is Year + 1 ; Year1 = Year),
226            % CONVERT DATE PARTS TO JULIAN DAY SERIAL NUMBER:
227            A is (14 - Month) // 12,
228            Y is Year1 + 4800 - A,
229            M is Month + (12 * A) - 3,
230            D is Day + ((153 * M + 2) // 5) + (365 * Y) + (Y // 4),
231            JD is D - (Y // 100) + (Y // 400) - 32045
232        ;  nonvar(JD),
233            % CONVERT JULIAN DAY SERIAL NUMBER TO DATE PARTS:
234            A is JD + 32045,
235            B is (4 * (A + 36524)) // 146097 - 1,
236            C is A - ((146097 * B) // 4),
237            D is ((4 * (C + 365)) // 1461) - 1,
238            E is C - ((1461 * D) // 4),
239            M is ((5 * (E - 1)) + 2) // 153,
240            Day is E - (((153 * M) + 2) // 5),
241            Month is M + (3 - (12 * (M // 10))),
242            Year1 is ((100 * B) + D - 4800 + (M // 10)),
243            % CORRIGATE 0-BASED ALGORITHM RESULT TO BC/AD CALENDAR YEARS:
244            (Year1 < 1 -> Year is Year1 - 1 ;  Year = Year1)
245        ).
246
247
248    %==============================================================================
249    % date(?JD, ?Year, ?Month, ?Day, ?DoW)
250
251    date(JD,Year,Month,Day,DoW) :-
252        date(JD,Year,Month,Day),
253        DoW is (JD mod 7) + 1.
254
255
256    %==============================================================================
257    % date(?JD, ?Year, ?Month, ?Day, ?DoW, ?Week)
258
259    date(JD,Year,Month,Day,DoW,week(Wk,Yr)) :-
260        (  var(JD), var(Year), var(Month), var(Day), nonvar(Wk), nonvar(Yr)
261            -> (var(DoW) -> DoW = 1 ; true),
262            date(JD1,Yr,1,1,DoW1),
263            (DoW1 > 4 -> Offset = 0 ; Offset = 1),
264            JD is JD1 + ((Wk - Offset) * 7) + DoW - DoW1,
265            date(JD,Year,Month,Day)
266        ;  date(JD,Year,Month,Day,DoW),
267            D4 is (((JD + 31741 - (JD mod 7)) mod 146097) mod 36524) mod 1461,
268            L is D4 // 1460,
269            Wk is (((D4 - L) mod 365) + L) // 7 + 1,
270            % CORRIGATE YEAR AS NECESSARY:
271            (  Month =:= 1, (Day =:= 1 ; Day =:= 2 ; Day =:= 3), Wk > 1
272                -> Yr is Year - 1
273            ;  (  Month =:= 12, (Day =:= 29 ; Day =:= 30 ; Day =:= 31), Wk =:= 1
274                    -> Yr is Year + 1
275                ;  Yr = Year
276                )
277            )
278        ).
279
280
281    %==============================================================================
282    % date(?JD, ?Year, ?Month, ?Day, ?DoW, ?Week, ?DoY)
283
284    date(JD,Year,Month,Day,DoW,Week,DoY) :-
285        (   var(JD), nonvar(Year), (var(Month) ; var(Day)), nonvar(DoY)
286            -> date(JD1,Year,1,0),
287            JD is JD1 + DoY,
288            date(JD,Year,Month,Day,DoW,Week)
289        ;   date(JD,Year,Month,Day,DoW,Week),
290            date(JD1,Year,1,0),
291            DoY is JD - JD1
292        ).
293
294
295    %==============================================================================
296    % date_string(+Format, ?Day, ?String)
297
298    date_string('YYYYMMDD',Day,String) :-   % DATE
299        ( nonvar(String)
300          -> atom_chars(String,[Y0,Y1,Y2,Y3,M0,M1,D0,D1]),
301              number_chars(Y,[Y0,Y1,Y2,Y3]),
302              number_chars(M,[M0,M1]),
303              number_chars(D,[D0,D1]),
304              Day = [Y,M,D]
305          ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,M,D) ; true),
306              (Day = [Y,M,D] -> nonvar(Y), nonvar(M), nonvar(D) ; date(Day,Y,M,D)),
307              prepend_zeros(2,D,D1),
308              prepend_zeros(2,M,M1),
309              number_codes(Y,Y1),
310              list_of_lists_to_atom([Y1,M1,D1],String)
311        ).
312    date_string('YYYY-MM-DD',Day,String) :-  % DATE
313        ( nonvar(String)
314          -> atom_chars(String,[Y0,Y1,Y2,Y3,_,M0,M1,_,D0,D1]),
315              number_chars(Y,[Y0,Y1,Y2,Y3]),
316              number_chars(M,[M0,M1]),
317              number_chars(D,[D0,D1]),
318              Day = [Y,M,D]
319          ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,M,D) ; true),
320              (Day = [Y,M,D] -> nonvar(Y), nonvar(M), nonvar(D) ; date(Day,Y,M,D)),
321              prepend_zeros(2,D,D1),
322              prepend_zeros(2,M,M1),
323              number_codes(Y,Y1),
324              list_of_lists_to_atom([Y1,[45],M1,[45],D1],String)
325        ).
326    date_string('YYYY-MM',Day,String) :-      % YEAR & MONTH
327        ( nonvar(String)
328          -> atom_chars(String,[Y0,Y1,Y2,Y3,_,M0,M1]),
329              number_chars(Year,[Y0,Y1,Y2,Y3]),
330              number_chars(Month,[M0,M1]),
331              Day = [Year,Month]
332          ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,M,_) ; true),
333              (Day = [Y,M,_] -> nonvar(Y), nonvar(M) ; date(Day,Y,M,_)),
334              prepend_zeros(2,M,M1),
335              number_codes(Y,Y1),
336              list_of_lists_to_atom([Y1,[45],M1],String)
337        ).
338    date_string('YYYY',Day,String) :-         % YEAR
339        ( nonvar(String)
340          -> atom_chars(String,[Y0,Y1,Y2,Y3]),
341              number_chars(Year,[Y0,Y1,Y2,Y3]),
342              Day = [Year]
343        ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,_,_) ; true),
344            (Day = [Y|_] -> nonvar(Y) ; date(Day,Y,_,_)),
345            number_codes(Y,Codes),
346            atom_codes(String,Codes)
347        ).
348    date_string('YY',Day,String) :-         % CENTURY
349        ( nonvar(String)
350          -> atom_chars(String,[C0,C1]),
351              number_chars(Century,[C0,C1]),
352              Day = [Century]
353        ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,_,_) ; true),
354            (Day = [Y|_] -> nonvar(Y) ; date(Day,Y,_,_)),
355            Y1 is Y // 100,
356            number_codes(Y1,Codes),
357            atom_codes(String,Codes)
358        ).
359    date_string('YYYYDDD',Day,String) :-      % YEAR & DAY-OF-YEAR
360        ( nonvar(String)
361          -> atom_chars(String,[Y0,Y1,Y2,Y3,D0,D1,D2]),
362              number_chars(Year,[Y0,Y1,Y2,Y3]),
363              number_chars(DoY,[D0,D1,D2]),
364              Day = [Year,DoY]
365        ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,M,D) ; true),
366            (Day = [Y,M,D] -> nonvar(Y), nonvar(M), nonvar(D) ; JD = Day),
367            date(JD,Y,M,D,_,_,DoY),
368            prepend_zeros(3,DoY,DoY1),
369            number_codes(Y,Y1),
370            list_of_lists_to_atom([Y1,DoY1],String)
371        ).
372    date_string('YYYY-DDD',Day,String) :-   % YEAR & DAY-OF-YEAR
373        ( nonvar(String)
374          -> atom_chars(String,[Y0,Y1,Y2,Y3,_,D0,D1,D2]),
375              number_chars(Year,[Y0,Y1,Y2,Y3]),
376              number_chars(DoY,[D0,D1,D2]),
377              Day = [Year,DoY]
378        ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,M,D) ; true),
379            (Day = [Y,M,D] -> nonvar(Y), nonvar(M), nonvar(D) ; JD = Day),
380            date(JD,Y,M,D,_,_,DoY),
381            prepend_zeros(3,DoY,DoY1),
382            number_codes(Y,Y1),
383            list_of_lists_to_atom([Y1,[45],DoY1],String)
384        ).
385    date_string('YYYYWwwD',Day,String) :-   % YEAR, WEEK & DAY-OF-WEEK
386        ( nonvar(String)
387          -> atom_chars(String,[Y0,Y1,Y2,Y3,_,W0,W1,DoW0]),
388              number_chars(Year,[Y0,Y1,Y2,Y3]),
389              number_chars(Week,[W0,W1]),
390              number_chars(DoW,[DoW0]),
391              Day = [Year,Week,DoW]
392          ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,M,D) ; true),
393              (Day = [Y,M,D] -> nonvar(Y), nonvar(M), nonvar(D) ; JD = Day),
394              date(JD,Y,M,D,DoW,week(Wk,Yr)),
395              number_codes(Yr,Y1),
396              prepend_zeros(2,Wk,Wk1),
397              number_codes(DoW,DoW1),
398              List = [Y1,[87],Wk1,DoW1],
399              list_of_lists_to_atom(List,String)
400        ).
401    date_string('YYYY-Www-D',Day,String) :-  % YEAR, WEEK & DAY-OF-WEEK
402        ( nonvar(String)
403          -> atom_chars(String,[Y0,Y1,Y2,Y3,_,_,W0,W1,_,DoW0]),
404              number_chars(Year,[Y0,Y1,Y2,Y3]),
405              number_chars(Week,[W0,W1]),
406              number_chars(DoW,[DoW0]),
407              Day = [Year,Week,DoW]
408          ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,M,D) ; true),
409              (Day = [Y,M,D] -> nonvar(Y), nonvar(M), nonvar(D) ; JD = Day),
410              date(JD,Y,M,D,DoW,week(Wk,Yr)),
411              number_codes(Yr,Y1),
412              prepend_zeros(2,Wk,Wk1),
413              number_codes(DoW,DoW1),
414              List = [Y1,[45,87],Wk1,[45],DoW1],
415              list_of_lists_to_atom(List,String)
416        ).
417    date_string('YYYYWww',Day,String) :-      % YEAR & WEEK
418        ( nonvar(String)
419          -> atom_chars(String,[Y0,Y1,Y2,Y3,_,W0,W1]),
420              number_chars(Year,[Y0,Y1,Y2,Y3]),
421              number_chars(Week,[W0,W1]),
422              Day = [Year,Week]
423          ;  ((var(Day) ; Day = [Y|_], var(Y)) -> date(_,Y,M,D) ; true),
424              (Day = [Y,M,D] -> nonvar(Y), nonvar(M), nonvar(D) ; JD = Day),
425              date(JD,Y,M,D,_,week(Wk,Yr)),
426              number_codes(Yr,Y1),
427              prepend_zeros(2,Wk,Wk1),
428              List = [Y1,[87],Wk1],
429              list_of_lists_to_atom(List,String)
430        ).
431    date_string('YYYY-Www',Day,String) :-   % YEAR & WEEK
432        ( nonvar(String)
433          -> atom_chars(String,[Y0,Y1,Y2,Y3,_,_,W0,W1]),
434              number_chars(Year,[Y0,Y1,Y2,Y3]),
435              number_chars(Week,[W0,W1]),
436              Day = [Yea