在验证ISO 8601日期时击败纯正则表达式


12

RX的ValiDate ISO 8601中,挑战在于仅使用标准正则表达式来验证标准日期格式(前者是RX的常见工作,后者是不寻常的)。获奖答案使用了778个字节。这个挑战是要使用您选择的任何语言来克服这种挑战,但要使用特殊的日期函数或类

挑战

查找最短的代码

  1. 验证Proleptic Gregorian日历中的每个可能日期(也适用于1582年首次采用之前的所有日期),
  2. 与任何无效的日期都不匹配,并且
  3. 不使用任何预定义的函数,方法,类,模块或类似方法来处理日期(和时间),即依赖于字符串和数字运算。

输出量

输出是真还是假。无需输出或转换日期。

输入值

输入是3种扩展的ISO 8601日期格式中的任何一种的单个字符串-无时间。

前两个是±YYYY-MM-DD(年,月,日)和±YYYY-DDD(年,日)。两者都需要special日的特殊保护套。这些扩展的RX天真地将它们分别匹配:

(?<year>[+-]?\d{4,})-(?<month>\d\d)-(?<day>\d\d)
(?<year>[+-]?\d{4,})-(?<doy>\d{3})

第三种输入格式是±YYYY-wWW-D(年,周,日)。由于leap周模式复杂,因此比较复杂。

(?<year>[+-]?\d{4,})-W(?<week>\d\d)-(?<dow>\d)

条件

一个闰年的Proleptic公历包含闰日 …-02-29,因此它是长366天,因此…-366存在。这种情况发生在序数可以被4整除但不能被100整除(除非也可以被400除)的任何 年份。此日历中存在年,并且是a年。

一个漫长的一年,在ISO周历包含了第53周…-W53-…,其中一个可以称之为一个“ 闰周 ”。在1月1日是星期四的所有年份中都会发生这种情况,在所有leap年都是星期三的所有leap年都发生这种情况。0001-01-01并且2001-01-01是星期一。事实证明,这种情况通常每5或6年出现一次,看似不规则。

一年至少有4位数字。不必支持位数超过10的年份,因为这已经足够接近宇宙的年龄(约140亿年)。前导加号是可选的,尽管实际标准建议多于4位数字的年份需要使用它。

不得接受不完整的部分或截短日期。-在所有情况下都必须使用连字符。(这些先决条件可以使导致+总是可选的。)

规则

这是代码高尔夫球。以字节为单位的最短代码获胜。较早的答案胜出。

测试用例

有效测试

2015-08-10
2015-10-08
12015-08-10
-2015-08-10
+2015-08-10
0015-08-10
1582-10-10
2015-02-28
2016-02-29
2000-02-29
0000-02-29
-2000-02-29
-2016-02-29
+2016-02-29
200000-02-29
-200000-02-29
+200000-02-29
2016-366
2000-366
0000-366
-2000-366
-2016-366
+2016-366
2015-081
2015-W33-1
2015-W53-7
+2015-W53-7
+2015-W33-1
-2015-W33-1
 2015-08-10 

最后一个可选地是有效的,即可以修剪输入字符串中的前导和尾随空格。

格式无效

-0000-08-10     # that's an arbitrary decision
15-08-10        # year is at least 4 digits long
2015-8-10       # month (and day) is exactly two digits long, i.e. leading zero is required
015-08-10       # year is at least 4 digits long
20150810        # though a valid ISO format, we require separators; could also be interpreted as a 8-digit year
2015 08 10      # separator must be hyphen-minus
2015.08.10      # separator must be hyphen-minus
2015–08–10      # separator must be hyphen-minus
2015-0810
201508-10       # could be October in the year 201508
2015 - 08 - 10  # no internal spaces allowed
2015-w33-1      # letter ‘W’ must be uppercase
2015W33-1       # it would be unambiguous to omit the separator in front of a letter, but not in the standard
2015W331        # though a valid ISO format we require separators
2015-W331
2015-W33        # a valid ISO date, but we require day-precision
2015W33         # though a valid ISO format we require separators and day-precision
2015-08         # a valid ISO format, but we require day-precision
201508          # a valid but ambiguous ISO format
2015            # a valid ISO format, but we require day-precision

无效的日期

2015-00-10  # month range is 1–12
2015-13-10  # month range is 1–12
2015-08-00  # day range is 1–28 through 31
2015-08-32  # max. day range is 1–31
2015-04-31  # day range for April is 1–30
2015-02-30  # day range for February is 1–28 or 29
2015-02-29  # day range for common February is 1–28
2100-02-29  # most century years are non-leap
-2100-02-29 # most century years are non-leap
2015-000    # day range is 1–365 or 366
2015-366    # day range is 1–365 in common years
2016-367    # day range is 1–366 in leap years
2100-366    # most century years are non-leap
-2100-366   # most century years are non-leap
2015-W00-1  # week range is 1–52 or 53
2015-W54-1  # week range is 1–53 in long years
2016-W53-1  # week range is 1–52 in short years
2015-W33-0  # day range is 1–7
2015-W33-8  # day range is 1–7

2
偏离主题,但可能有用-栈溢出:stackoverflow.com/questions/28020805/… (如果我不应该发布,请告诉我)
Daniele D

如果程序员是YEC(地球创世主义者)怎么办?
Leaky Nun

关于-0000-08-10什么是专断的决定?不允许年份为负0?
edc65 '16

@ edc65是,+0000-08-100000-08-10应改为使用。但是请注意,此挑战的正则表达式变体中可接受的答案在此特定测试用例中未通过,因此这并不是一个失败的条件(即,should,不是must)。
Crissov

@KennyLau然后,程序员是错误的
Arcturus

Answers:


2

JavaScript(ES6),236

236个字节,允许为负0年(-0000)。返回true或false

s=>!!([,y,w,d]=s.match(/^([+-]?\d{4,})(-W?\d\d)?(-\d{1,3})$/)||[],n=y%100==0&y%400!=0|y%4!=0,l=((l=y-1)+8-~(l/4)+~(l/100)-~(l/400))%7,l=l==5|l==4&!n,+d&&(-w?d>`0${2+n}0101001010`[~w]-32:w?(w=w.slice(2),w>0&w<(53+l)&d>-8):d[3]&&d>n-367))

将负数0的校验加2个字节,但加13。请注意,在javascript中,数字值-0存在,并且在特殊情况下等于0,但1/-0-Infinity。此版本返回0或1

s=>([,y,w,d]=s.match(/^([+-]?\d{4,})(-W?\d\d)?(-\d{1,3})$/)||[],n=y%100==0&y%400!=0|y%4!=0,l=((l=y-1)+8-~(l/4)+~(l/100)-~(l/400))%7,l=l==5|l==4&!n,+d&&(-w?d>`0${2+n}0101001010`[~w]-32:w?(w=w.slice(2),w>0&w<(53+l)&d>-8):d[3]&&d>n-367))&!(!+y&1/y<0)

测试

Check=
  s=>!! // to obtain a true/false 
  (
    // parse year in y, middle part in w, day in d
    // day will be negative with 1 or 3 numeric digits and could be 0
    // week will be '-W' + 2 digits
    // month will be negative with2 digits and could be 0
    // if the date is in format yyyy-ddd, then w is empty
    [,y,w,d] = s.match(/^([+-]?\d{4,})(-W?\d\d)?(-\d{1,3})$/) || [],
    n = y%100==0 & y%400!=0 | y%4!=0, // n: not leap year
    l = ((l=y-1) + 8 -~(l/4) +~(l/100) -~(l/400)) % 7, 
    l = l==5| l==4 & !n, // l: long year (see http://mathforum.org/library/drmath/view/55837.html)
    +d && ( // if d is not empty and not 0
     -w // if w is numeric and not 0, then it's the month (negative)
     ? d > `0${2+n}0101001010`[~w] - 32 // check month length (for leap year too)
      : w // if w is not empty, then it's the week ('-Wnn')
        ? ( w = w.slice(2), w > 0 & w < (53+l) & d >- 8) // check long year too
        : d[3] && d > n-367 // else d is the prog day, has to be 3 digits and < 367 o 366
    )
  )

console.log=x=>O.textContent += x +'\n'

OK=['1900-01-01','2015-08-10','2015-10-08','12015-08-10','-2015-08-10','+2015-08-10'
,'0015-08-10','1582-10-10','2015-02-28','2016-02-29','2000-02-29'
,'0000-02-29','-2000-02-29','-2016-02-29','+2016-02-29','200000-02-29'
,'-200000-02-29','+200000-02-29','2016-366','2000-366','0000-366'
,'-2000-366','-2016-366','+2016-366','2015-081','2015-W33-1'
,'2015-W53-7','+2015-W53-7','+2015-W33-1','-2015-W33-1','2015-08-10']

KO=['-0000-08-10','15-08-10','2015-8-10','015-08-10','20150810','2015 08 10'
,'2015.08.10','2015–08–10','2015-0810','201508-10','2015 - 08 - 10','2015-w33-1'
,'2015W33-1','2015W331','2015-W331','2015-W33','2015W33','2015-08','201508'
,'2015','2015-00-10','2015-13-10','2015-08-00','2015-08-32','2015-04-31'
,'2015-02-30','2015-02-29','2100-02-29','-2100-02-29','2015-000'
,'2015-366','2016-367','2100-366','-2100-366','2015-W00-1'
,'2015-W54-1','2016-W53-1','2015-W33-0','2015-W33-8']

console.log('Valid')
OK.forEach(x=>console.log(Check(x)+' '+x))
console.log('Not valid')
KO.forEach(x=>console.log(Check(x)+' '+x))
<pre id=O></pre>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.