ではRXによって検証ISO 8601、挑戦はするだけで、標準的な正規表現を使用していた標準の日付形式検証と値を(前者はRXのための共通の仕事で、後者は異例でした)。勝った答えは778バイトを使用しました。この課題は、特別な日付関数またはクラスを使用せずに、選択した任意の言語を使用してそれを克服することです。
チャレンジ
最短のコードを見つける
- Proleptic Gregorianカレンダーのすべての可能な日付を検証します(これは1582年に最初に採用される前のすべての日付にも適用されます)。
- 無効な日付と一致しない
- 事前に定義された関数、メソッド、クラス、モジュールなどを使用して日付(および時刻)を処理しません。つまり、文字列および数値演算に依存します。
出力
出力は真実または偽です。日付を出力または変換する必要はありません。
入力
入力は、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
-0000-08-10
exaclty独断何ですか?年を負の0として許可しないのですか?
+0000-08-10
と0000-08-10
代わりに使用する必要があります。注、この課題の正規表現のバリエーションで受け入れられた答えは、この特定のテストケースを失敗したことを、けれども、それは本当に失敗した状態(すなわちAはありませんすべきではなく、必須)。