Skip to content

Instantly share code, notes, and snippets.

@rns
Created October 7, 2014 10:20
Show Gist options
  • Save rns/dcbcfb9bfe2e4e0e9494 to your computer and use it in GitHub Desktop.
Save rns/dcbcfb9bfe2e4e0e9494 to your computer and use it in GitHub Desktop.
# 5/12
['date',['American month and day',['mm','5'],'/',['dd','12']]]
# 10/27
['date',['American month and day',['mm','10'],'/',['dd','27']]]
# 12/22/78
['date',['American month day and year',['mm','12'],'/',['dd','22'],'/',['y','78']]]
# 1/17/2006
['date',['American month day and year',['mm','1'],'/',['dd','17'],'/',['y','2006']]]
# 1/17/6
['date',['American month day and year',['mm','1'],'/',['dd','17'],'/',['y','6']]]
# 2008/6/30
['date',['Four digit year month and day with slashes',['YY','2008'],'/',['mm','6'],'/',['dd','30']]]
# 1978/12/22
['date',['Four digit year month and day with slashes',['YY','1978'],'/',['mm','12'],'/',['dd','22']]]
# 2008-6
['date',['Four digit year and month GNU YY',['YY','2008'],'-',['mm','6']]]
# 2008-06
['date',['Four digit year and month GNU YY',['YY','2008'],'-',['mm','06']]]
# 1978-12
['date',['Four digit year and month GNU YY',['YY','1978'],'-',['mm','12']]]
# 2008-6-30
['date',['Year month and day with dashes',['y','2008'],'-',['mm','6'],'-',['dd','30']]]
# 78-12-22
['date',['Year month and day with dashes',['y','78'],'-',['mm','12'],'-',['dd','22']]]
# 8-6-21
['date',['Year month and day with dashes',['y','8'],'-',['mm','6'],'-',['dd','21']]]
# 30-6-2008
['date',['Day month and four digit year with dots tabs or dashes',['dd','30'],'-',['mm','6'],'-',['YY','2008']]]
# 22.12 1978
['date',['Day month and four digit year with dots tabs or dashes',['dd','22'],'.',['mm','12'],' ',['YY','1978']]]
# 30.6.08
['date',['Day month and two digit year with dots or tabs',['dd','30'],'.',['mm','6'],'.',['yy','08']]]
# 22 12 78
['date',['Day month and two digit year with dots or tabs',['dd','22'],' ',['mm','12'],' ',['yy','78']]]
# 30-June 2008
['date',['Day textual month and year',['dd','30'],['separator_seq','-'],['m','June'],['separator_seq',' '],['y','2008']]]
# 22DEC78
['date',['Day textual month and year',['dd','22'],['m','DEC'],['y','78']]]
# 14 III 1879
['date',['Day textual month and year',['dd','14'],['separator_seq',' '],['m','III'],['separator_seq',' '],['y','1879']]]
# June 2008
['date',['Textual month and four digit year Day reset to 1',['m','June'],['separator_seq',' '],['YY','2008']]]
# DEC1978
['date',['Textual month and four digit year Day reset to 1',['m','DEC'],['YY','1978']]]
# March 1879
['date',['Textual month and four digit year Day reset to 1',['m','March'],['separator_seq',' '],['YY','1879']]]
# 2008 June
['date',['Four digit year and textual month Day reset to 1',['YY','2008'],['separator_seq',' '],['m','June']]]
# 1978-XII
['date',['Four digit year and textual month Day reset to 1',['YY','1978'],['separator_seq','-'],['m','XII']]]
# 1879.MArCH
['date',['Four digit year and textual month Day reset to 1',['YY','1879'],['separator_seq','.'],['m','MArCH']]]
# July 1st, 2008
['date',['Textual month day and year',['m','July'],['separator_seq',' '],['dd','1st'],['suffix_seq',', '],['y','2008']]]
# April 17, 1790
['date',['Textual month day and year',['m','April'],['separator_seq',' '],['dd','17'],['suffix_seq',', '],['y','1790']]]
# May.9,78
['date',['Textual month day and year',['m','May'],['separator_seq','.'],['dd','9'],['suffix_seq',','],['y','78']]]
# July 1st,
['date',['Textual month and day',['m','July'],['separator_seq',' '],['dd','1st'],['suffix_seq',',']]]
# Apr 17
['date',['Textual month and day',['m','Apr'],['separator_seq',' '],['dd','17']]]
# May.9
['date',['Textual month and day',['m','May'],['separator_seq','.'],['dd','9']]]
# 1 July
['date',['Day and textual month',['dd','1'],['separator_seq',' '],['m','July']]]
# 17 Apr
['date',['Day and textual month',['dd','17'],['separator_seq',' '],['m','Apr']]]
# 9.May
['date',['Day and textual month',['dd','9'],['separator_seq','.'],['m','May']]]
# May-09-78
['date',['Month abbreviation day and year',['M','May'],'-',['DD','09'],'-',['y','78']]]
# Apr-17-1790
['date',['Month abbreviation day and year',['M','Apr'],'-',['DD','17'],'-',['y','1790']]]
# 78-Dec-22
['date',['year month abbreviation and day',['y','78'],'-',['M','Dec'],'-',['DD','22']]]
# 1814-MAY-17
['date',['year month abbreviation and day',['y','1814'],'-',['M','MAY'],'-',['DD','17']]]
# 1978
['date',['Year and just the year',['YY','1978']]]
# 2008
['date',['Year and just the year',['YY','2008']]]
# March
['date',['Textual month and just the month',['m','March']]]
# jun
['date',['Textual month and just the month',['m','jun']]]
# DEC
['date',['Textual month and just the month',['m','DEC']]]
# 15810726
['date',['Eight digit year month and day',['YY','1581'],['MM','07'],['DD','26']]]
# 19780417
['date',['Eight digit year month and day',['YY','1978'],['MM','04'],['DD','17']]]
# 18140517
['date',['Eight digit year month and day',['YY','1814'],['MM','05'],['DD','17']]]
# 2008/06/30
['date',['Four digit year month and day with slashes',['YY','2008'],'/',['mm','06'],'/',['dd','30']]]
# 1978/12/22
['date',['Four digit year month and day with slashes',['YY','1978'],'/',['mm','12'],'/',['dd','22']]]
# 08-06-30
['date',['Year month and day with dashes',['y','08'],'-',['mm','06'],'-',['dd','30']]]
# 78-12-22
['date',['Year month and day with dashes',['y','78'],'-',['mm','12'],'-',['dd','22']]]
# -0002-07-26
['date',['Four digit year with optional sign month and day','-',['YY','0002'],'-',['MM','07'],'-',['DD','26']]]
# +1978-04-17
['date',['Four digit year with optional sign month and day','+',['YY','1978'],'-',['MM','04'],'-',['DD','17']]]
# 1814-05-17
['date',['Year month and day with dashes',['y','1814'],'-',['mm','05'],'-',['dd','17']]]
use 5.010;
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;
$Data::Dumper::Deepcopy = 1;
use Marpa::R2;
my $g = Marpa::R2::Scanless::G->new({ source => \(<<'END_OF_SOURCE')
:default ::= action => [ name, value]
lexeme default = action => [ name, value] latm => 1
# source: http://php.net/manual/en/datetime.formats.date.php
date ::= <American month and day>
date ::= <American month day and year>
date ::= <Four digit year month and day with slashes>
date ::= <Four digit year and month GNU YY>
date ::= <Year month and day with dashes>
date ::= <Day month and four digit year with dots tabs or dashes>
date ::= <Day month and two digit year with dots or tabs>
date ::= <Day textual month and year>
date ::= <Textual month and four digit year Day reset to 1>
date ::= <Four digit year and textual month Day reset to 1>
date ::= <Textual month day and year>
date ::= <Textual month and day>
date ::= <Day and textual month>
date ::= <Month abbreviation day and year>
date ::= <year month abbreviation and day>
date ::= <Year and just the year>
date ::= <Textual month and just the month>
date ::= <Eight digit year month and day>
date ::= <Four digit year with optional sign month and day>
#
# Localized Notations
#
# '5/12', '10/27'
<American month and day> ::= mm '/' dd
# '12/22/78', '1/17/2006', '1/17/6'
<American month day and year> ::= mm '/' dd '/' y
# '2008/6/30', '1978/12/22'
<Four digit year month and day with slashes> ::= YY '/' mm '/' dd
# '2008-6', '2008-06', '1978-12'
<Four digit year and month GNU YY> ::= YY '-' mm
# '2008-6-30', '78-12-22', '8-6-21'
<Year month and day with dashes> ::= y '-' mm '-' dd
# '30-6-2008', '22.12\t1978'
<Day month and four digit year with dots tabs or dashes> ::= dd [\.\t-] mm [\.\t-] YY
# '30.6.08', '22\t12\t78'
<Day month and two digit year with dots or tabs> ::= dd [\.\t] mm [\.\t] yy
# '30-June 2008', '22DEC78', '14 III 1879'
<Day textual month and year> ::= dd separator_seq m separator_seq y
<Day textual month and year> ::= dd separator_seq m y
<Day textual month and year> ::= dd m separator_seq y
<Day textual month and year> ::= dd m y
separator_seq ~ [ \t\.-]+
# 'June 2008', 'DEC1978', 'March 1879'
<Textual month and four digit year Day reset to 1> ::= m separator_seq YY
<Textual month and four digit year Day reset to 1> ::= m YY
# '2008 June', '1978-XII', '1879.MArCH'
<Four digit year and textual month Day reset to 1> ::= YY separator_seq m
# 'July 1st, 2008', 'April 17, 1790', 'May.9,78'
<Textual month day and year> ::= m separator_seq dd suffix_seq y
<Textual month day and year> ::= m dd suffix_seq y
suffix_seq ~ [,\.\t ]+
# 'July 1st,', 'Apr 17', 'May.9'
<Textual month and day> ::= m separator_seq dd suffix_seq
<Textual month and day> ::= m separator_seq dd
# '1 July', '17 Apr', '9.May'
<Day and textual month> ::= dd separator_seq m
# 'May-09-78', 'Apr-17-1790'
<Month abbreviation day and year> ::= M '-' DD '-' y
# '78-Dec-22', '1814-MAY-17'
<year month abbreviation and day> ::= y '-' M '-' DD
# '1978', '2008'
<Year and just the year> ::= YY
# 'March', 'jun', 'DEC'
<Textual month and just the month> ::= m
#
# ISO8601 Notations
#
# '15810726', '19780417', '18140517'
<Eight digit year month and day> ::= YY MM DD
# '-0002-07-26', '+1978-04-17', '1814-05-17'
<Four digit year with optional sign month and day> ::= [\+\-] YY '-' MM '-' DD
dd ~ [0-2] [0-9] daysuf # '7th', '22nd', '31'
dd ~ '3' [01] daysuf
dd ~ [0-2] [0-9]
dd ~ '3' [01]
dd ~ [0-9] daysuf
dd ~ [0-9]
DD ~ '0' [0-9] | [1-2][0-9] | '3' [01] # '07', '31'
m ~ 'january':i | 'february':i | 'march':i | 'april':i | 'may':i |
'june':i | 'july':i | 'august':i | 'september':i | 'october':i |
'november':i | 'december':i | 'jan':i | 'feb':i | 'mar':i | 'apr':i |
'jun':i | 'jul':i | 'aug':i | 'sep':i | 'sept':i | 'oct':i |
'nov':i | 'dec':i | 'I':i | 'II':i | 'III':i | 'IV':i | 'V':i | 'VI':i |
'VII':i | 'VIII':i | 'IX':i | 'X':i | 'XI':i | 'XII':i
M ~ 'jan':i | 'feb':i | 'mar':i | 'apr':i | 'may':i | 'jun':i |
'jul':i | 'aug':i | 'sep':i | 'sept':i | 'oct':i | 'nov':i | 'dec':i
mm ~ '0' [0-9] # '0', '04', '7', '12'
mm ~ [0-9]
mm ~ '1' [0-2]
MM ~ '0' [0-9] | '1' [0-2] # '00', '04', '07', '12'
y ~ [0-9] # '00', '78', '08', '8', '2008'
y ~ [0-9] [0-9]
y ~ [0-9] [0-9] [0-9]
y ~ [0-9] [0-9] [0-9] [0-9]
yy ~ [0-9] [0-9] # '00', '08', '78'
YY ~ [0-9] [0-9] [0-9] [0-9] # '2000', '2008', '1978'
daysuf ~ 'st' | 'nd' | 'rd' | 'th'
:discard ~ whitespace
whitespace ~ [\s]+
END_OF_SOURCE
} );
my @dates = (
'5/12', '10/27',
'12/22/78', '1/17/2006', '1/17/6',
'2008/6/30', '1978/12/22',
'2008-6', '2008-06', '1978-12',
'2008-6-30', '78-12-22', '8-6-21',
'30-6-2008', "22.12\t1978",
'30.6.08', "22\t12\t78",
'30-June 2008', '22DEC78', '14 III 1879',
'June 2008', 'DEC1978', 'March 1879',
'2008 June', '1978-XII', '1879.MArCH',
'July 1st, 2008', 'April 17, 1790', 'May.9,78',
'July 1st,', 'Apr 17', 'May.9',
'1 July', '17 Apr', '9.May',
'May-09-78', 'Apr-17-1790',
'78-Dec-22', '1814-MAY-17',
'1978', '2008',
'March', 'jun', 'DEC',
'15810726', '19780417', '18140517',
'2008/06/30', '1978/12/22',
'08-06-30', '78-12-22',
'-0002-07-26', '+1978-04-17', '1814-05-17'
);
for my $date (@dates){
say "# $date";
say Dumper ${ $g->parse( \$date ) };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment