The date format varies between countries; the arrangement of date, month and year can be categorized into these three:
- Year-month-date
- Month-date-year
- Date-month-year
In Gregorian calendar it's year of 2024 now (at this point I publish this problem); we often shorten the notation of year by using only bottom two digits: e.g. 24.
Month and date are usually represented as one or two digits.
Today you are having a triple desicion-problem who do something with date format.
Input
One of:
- A string of six digits
- An nonnegative integer up to 999999
- Three nonnegative integers up to 99
- Six nonnegative integers up to 9
- Any other reasonable representation that can be converted to a string of six digits or vise versa in consistent way
Here is a Python snippet of generator to generate every input as string:
f"{d:06}" for d in range(999999+1)
Output
A tuple of three boolean values whether it can be interpreted as YYMMDD, MMDDYY, and DDMMYY date format. As long as it is consistent, you can have flexible output. Examples but not limited to:
- a set of data
- an integer with possibly three bits
- 0-3 characters of string consists of y, m and/or d to indicate it is truthy for YYMMDD, MMDDYY, and DDMMYY respectively
- different exception classes
Specification
TLDR: Gregorian Calendar. YY is bottom two digits of the year. Handle leap year as usual. If YYMMDD is 000229 it is optional whether its format is valid or not.
A digit is a character one of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
The YY format is any combination of two digits.
The MM format is one of 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, or 12.
The DD format varies depending on the YY and MM formats:
- if MM is one of 04, 06, 09, or 11, then the format is one of 01, 02, 03, ..., 09, 10, 11, 12, ..., 19, 20, 21, ..., 29, or 30.
- if MM is one of 01, 03, 05, 07, 08, 10, or 12 then the format is one of 01, 02, ..., 29, 30 or 31.
- if MM is 02 and YY is NOT a multiple of 4, then the format is one of 01, ..., 27, or 28.
- if MM is 02 and YY is 00 then the format is one of what rule 3 says or optionally a string of 29.
- if MM is 02 and YY is NOT 00 and is a multiple of 4, then the format is what rule 3 says or 29.
The YYMMDD format is a concatenation of the YY, MM and DD formats.
The MMDDYY format is a concatenation of the MM, DD and YY formats.
The DDMMYY format is a concatenation of the DD, MM and YY formats.
Test cases
In the following table, maybe
means it is implementation-defined to output whether the value is truthy or falsey.
Input YYMMDD MMDDYY DDMMYY
000000 false false false
000001 false false false
000099 false false false
000100 false false false
000101 true false false
000102 true false false
000131 true false false
000132 false false false
000199 false false false
000200 false false false
000201 true false false
000228 true false false
000229 maybe false false
000230 false false false
000300 false false false
000301 true false false
000331 true false false
000332 false false false
001130 true false false
001231 true false false
001232 false false false
010000 false false false
010001 false false false
010099 false false false
010100 false true true
010101 true true true
010102 true true true
010131 true true true
010132 false true true
010199 false true true
010200 false true true
010201 true true true
010228 true true true
010229 false true true
010300 false true true
010301 true true true
010331 true true true
010332 false true true
010430 true true true
010431 false true true
010531 true true true
010532 false true true
011231 true true true
011232 false true true
011299 false true true
011300 false true false
011399 false true false
011400 false true false
013199 false true false
013200 false false false
014567 false false false
019999 false false false
020000 false false false
020001 false false false
020099 false false false
020100 false true true
020101 true true true
020102 true true true
020128 true true true
020131 true true true
020132 false true true
020199 false true true
020200 false true true
020201 true true true
121212 true true true
121213 true true true
121230 true true true
121231 true true true
121232 false true true
121299 false true true
121300 false true false
121301 false true false
121399 false true false
121400 false true false
123100 false true false
123199 false true false
123200 false false false
129999 false false false
130000 false false false
130099 false false false
130100 false false true
990101 true false false
990202 true false false
991230 true false false
991231 true false false
991232 false false false
991233 false false false
999999 false false false
Every case truthy for all of YYMMDD, MMDDYY, and DDMMYY
Too long; pasted on GitHub Gists.
Every maybe ones
Maybe for YYMMDD: 000229
Maybe for MMDDYY: 022900
Maybe for DDMMYY: 290200
That's all; just three.
Test case generator
inputrange = list(range(200, 231)) + list(range(0, 1000000, 2763))
yymmdds, mmddyys, ddmmyys = [], [], []
maybey, maybem, maybed = [], [], []
for y in range(99+1):
for m in range(1,12+1):
if m == 2:
dlast = 28 + (y%4==0)
else:
dlast = 31 - (m in [4,6,9,11])
for d in range(1,dlast+1):
yy, mm, dd = f"{y:02} {m:02} {d:02}".split(" ")
ys, ms, ds=yy+mm+dd, mm+dd+yy, dd+mm+yy
if (y,m,d) == (0,2,29):
[maybel.append(s)for (maybel,s)in [[maybey,ys],[maybem,ms],[maybed,ds]]]
continue
yymmdds.append(ys)
mmddyys.append(ms)
ddmmyys.append(ds)
for x in [f"{x:06}" for x in inputrange]:
y, m, d = ["maybe" if x in maybel else x in l for (maybel,l) in ((maybey,yymmdds),(maybem,mmddyys),(maybed,ddmmyys))]
print(x, y, m, d)
Rules
- code-golf
- decision-problem
- See also https://codegolf.meta.stackexchange.com/questions/tagged/defaults
See also
- List of date formats by country - Wikipedia
Challenges involving possibly ambiguous date format:
- What date is that again?
- Day of the week of an ambiguous date
- Resolving the Date Format Dispute
- Has my milk expired?