ISO 8601日付の検証で純粋な正規表現を破る


12

ではRXによって検証ISO 8601、挑戦はするだけで、標準的な正規表現を使用していた標準の日付形式検証値を(前者はRXのための共通の仕事で、後者は異例でした)。勝った答えは778バイトを使用しました。この課題は、特別な日付関数またはクラスを使用せずに、選択した任意の言語を使用してそれを克服することです。

チャレンジ

最短のコードを見つける

  1. Proleptic Gregorianカレンダーのすべての可能な日付を検証します(これは1582年に最初に採用される前のすべての日付にも適用されます)。
  2. 無効な日付と一致しない
  3. 事前に定義された関数、メソッド、クラス、モジュールなどを使用して日付(および時刻)を処理しません。つまり、文字列および数値演算に依存します。

出力

出力は真実または偽です。日付を出力または変換する必要はありません。

入力

入力は、3つの拡張ISO 8601日付形式のいずれかの単一文字列です。

最初の2つは±YYYY-MM-DD(年、月、日)と±YYYY-DDD(年、日)です。両方ともうるう日に特別なケーシングが必要です。これらは、これらの拡張RXによって個別に単純に照合されます。

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

3番目の入力形式は±YYYY-wWW-D(年、週、日)です。複雑なうるう週パターンのため、複雑なものです。

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

条件

うるう年予期的グレゴリオ暦のカレンダーでは、含まれているうるう日 …-02-29、したがって、それは長い366日で、それ故に…-366存在します。これは、(おそらく負の)序数が4で割り切れる年に発生しますが、400で割り切れる場合を除き、100で割り切れません。 このカレンダーにはゼロ年が存在し、うるう年です。

長い年のISO週のカレンダーでは、第53週が含ま…-W53-…1は「という用語ができ、飛躍の週を」。これは、1月1日が木曜日であるすべての年に発生し、さらに水曜日であるすべてのうるう年に発生します。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 

最後の1つはオプションで有効です。つまり、入力文字列の先頭と末尾のスペースは削除されます。

無効な形式

-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/... (私はそれを投稿してはならない場合、私に教えて)
ダニエル・D

プログラマーがYEC(Young-Earth Creationist)である場合はどうなりますか?
リーキー修道女

-0000-08-10exaclty独断何ですか?年を負の0として許可しないのですか?
edc65

@ edc65はい、+0000-08-100000-08-10代わりに使用する必要があります。注、この課題の正規表現のバリエーションで受け入れられた答えは、この特定のテストケースを失敗したことを、けれども、それは本当に失敗した状態(すなわちAはありませんすべきではなく、必須)。
クリソフ

@KennyLauプログラマーは間違っています。
アークトゥルス

回答:


2

JavaScript(ES6)、236

負の0年(-0000)を許可する236バイト。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バイトのチェックを追加することなく、JavaScriptでの数値は、その13注加算-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>

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.