This file is indexed.

/usr/share/perl5/Regexp/Common/time.pm is in libregexp-common-time-perl 0.05-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
=head1 NAME

Regexp::Common::time - Date and time regexps.

=head1 VERSION

This is version 0.05 of Regexp::Common::time, Nov 20, 2013.

=cut

use strict;
package Regexp::Common::time;
$Regexp::Common::time::VERSION = '0.05';
use Regexp::Common qw(pattern);
use POSIX;

sub _croak { require Carp; goto &Carp::croak}

my $can_locale;
my $can_posix;
BEGIN
{
    eval
    {
        $can_posix = 0;
        require POSIX;
        $can_posix = 1;
    };
    eval
    {
        $can_locale = 0;
        require I18N::Langinfo;
        I18N::Langinfo->import(qw(langinfo));
        $can_locale = 1;
    };
}

# Master list of patterns
our %master
    = (
       c2   => q/\d{2}/,                                      # Century, 2 digits
       yr2  => q/\d{2}/,                                      # Year, 2 digits
       yr4  => q/\d{4}/,                                      # Year, 4 digits
       yr24 => q/(?:\d{2}(?:\d{2})?)/,                        # Year, 2 or 4 digits
       mo2  => q/(?:(?=[01])(?:0[1-9]|1[012]))/,              # Month, 2 digits
       mo12 => q/(?:0[1-9]|1[012]|(?<!\d)[1-9])/,             # Month, 1 or 2 digits
       mo_2 => q/(?:(?=[ 1])(?: [1-9]|1[012]))/,              # Month, 2 places, leading space
       dy2  => q/(?:(?=[0123])(?:0[1-9]|[12]\d|3[01]))/,      # Day, 2 digits
       dy12 => q/(?:0[1-9]|[12]\d|3[01]|(?<!\d)[1-9])/,       # Day, 1 or 2 digits
       dy_2 => q/(?:(?=[ 123])(?: [1-9]|[12]\d|3[01]))/,      # Day, 2 places, leading space
       doy3 => q/(?:(?=[0-3])(?:00[1-9]|0[1-9]\d|[12]\d\d|3(?:[0-5]\d|6[0-6])))/, # Day of year, 3 digits
       hr2  => q/(?:(?=[012])(?:[01]\d|2[0123]))/,            # Hour, 00-23, 2 digits
       hr12 => q/(?:(?=\d)(?:[01]\d|2[0123]|(?<!\d)\d))/,     # Hour, 0-23, 1 or 2 digits
       hr_2 => q/(?:(?=[ 12])(?:[ 1]\d|2[0123]))/,            # Hour,  0-23, 2 places, ld sp
       hx2  => q/(?:(?=[01])(?:0[1-9]|1[012]))/,              # Hour, 01-12, 2 digits
       hx12 => q/(?:(?=\d)(?:0[1-9]|1[012]|(?<!\d)[1-9]))/,   # Hour, 1-12, 1 or 2 digits
       hx_2 => q/(?:(?=[ 1])(?: [1-9]|1[012]))/,              # Hour,  1-12, 2 places, ld sp
       mi2  => q/(?:[0-5]\d)/,                                # Minute, 2 digits
       mi12 => q/(?:[0-5]\d|(?<!\d)\d)/,                      # Minute, 1 or 2 digits
       mi_2 => q/(?:[ 1-5]\d)/,                               # Minute, 2 places, leading sp
       sc2  => q/(?:(?=[0-6])(?:[0-5]\d|6[01]))/,             # Second, 2 digits, 00-61
       sc12 => q/(?:(?=[0-6])(?:[0-5]\d|6[01]|(?<!\d)\d))/,   # Second, 1 or 2 digits, 0-61
       sc_2 => q/(?:(?=[ 1-6])(?:[ 1-5]\d|6[01]))/,           # Second, 2 places,  0-61, ld sp
       wn2  => q/(?:(?=[0-5])(?:0[1-9]|[1-4]\d|5[0-3]))/,     # Week number, 2 digits, 01-53
       wnx2 => q/(?:(?=[0-5])(?:[0-4]\d|5[0-3]))/,            # Week number, 2 digits, 00-53
       wd1  => q/[0-6]/,                                      # Weekday number, 1 digit, 0-6
       wdx1 => q/[1-7]/,                                      # Weekday number, 1 digit, 1-7
       msec => q/\d{3}/,                                      # millisecond
       usec => q/\d{6}/,                                      # microsecond
       ampm => q/(?:(?=[AaPp])(?:[ap](?:m|\.m\.)?|[AP](?:M|\.M\.)?))/,    # am/pm indicator
       th   => q/(?:(?=[SNRTsnrt])(?:st|ST|nd|ND|rd|RD|th|TH))/,   # ordinal suffix
       tz   => q/(?:[-+](?:[01]\d|2[0-4])(?::?[0-5]\d)?|Z|GMT|UTC?|[ECMP][DS]T)/,    # Time zone
       ema  => _get_abbr_month_pattern(1),                    # English month abbreviation

       # The following are locale-specific, and will be populated later
       mname => q/TBD/,     # Full month name
       mabbr => q/TBD/,     # Month abbreviation
       dname => q/TBD/,     # Full weekday name
       dabbr => q/TBD/,     # Weekday abbreviation
       axpx  => q/TBD/,     # locale-specific AM/PM indicator
      );

my $npd  = q/(?<!\d)/;    # "No preceeding digit"
my $nfd  = q/(?!\d)/;     # "No following digit"
my $sdig = $npd . q/[1-9]/ . $nfd;     # One single digit (used for months and days)

sub _nospace
{
    my $s = shift;
    $s =~ s/([\x20\x09])/sprintf '\\x%02x', ord $1/eg;
    return $s;
}

my $anymon;   # general-purpose month capture.  Set in _setup_locale().

my $d = qq/$sdig|$master{dy2}/;
my $dcap  = qq/(?k:$d)/;

# Separator pattern: allows for certain punctuation, or none, plus optional space.
my $dsep = _nospace q{[-/. ]};

# "Middle" day.  Must be surrounded by matching separators
my $dmiddle  = _nospace qq{(?=(?>/$master{dy12}/|-$master{dy12}-| $master{dy12},? |\\.$master{dy12}\\.|(?!$dsep)$master{dy12}(?!$dsep)))$dsep?(?k:$master{dy12}),?$dsep?};
my $d2middle = _nospace qq{(?=(?>/$master{dy2}/|-$master{dy2}-| $master{dy2},? |\\.$master{dy2}\\.|(?!$dsep)$master{dy2}(?!$dsep)))$dsep?(?k:$master{dy2}),?$dsep?};

# "Middle" month.  Must be surrounded by matching separators
my $mFULLmiddle;   # Full month pattern, in middle (ymd and dmy).  Set in _setup_locale().
my $m2middle = _nospace qq{(?=(?>/$master{mo2}/|-$master{mo2}-| $master{mo2} |\\.$master{mo2}\\.|$master{mo2}(?!$dsep)))$dsep?(?k:$master{mo2})$dsep?};

# "Middle" minute.  Must be surrounded by matching separators
my $tsep = _nospace q/[:. ]/;
my $min2middle = _nospace qq{(?=(?>:$master{mi2}:|\\.$master{mi2}\\.| $master{mi2} |$master{mi2}(?!$tsep)))$tsep?(?k:$master{mi2})$tsep?};


# YMD builder
sub ymd
{
    my ($self, $flags_hr, $keys_ar) = @_;
    my $pattern = $keys_ar->[1];
    _setup_locale();

    # The second separator character is REQUIRED to be the same as the
    # first for YMD patterns.  Otherwise, "2005/10/21" is ambiguous:
    # it matches "(20)(05)/(10)" and "(2005)/(10)/(21)".

    # 'ymd' is the most flexible: year: 2/4 digits; month 1/2 digits or name; day 1/2 digits.
    if ($pattern eq 'ymd')
    {
        return qq/(?k:$npd(?k:$master{yr24})$mFULLmiddle$dcap$nfd)/;
    }
    # 'y4md': 4-digit year; 1 or 2 digit month and day.  Or named month.
    elsif ($pattern eq 'y4md')
    {
        return qq/(?k:(?k:$master{yr4})$mFULLmiddle$dcap$nfd)/;
    }
    # 'y2md': 2-digit year; 1 or 2 digit month and day.
    elsif ($pattern eq 'y2md')
    {
        return qq/(?k:(?k:$master{yr2})$mFULLmiddle$dcap$nfd)/;
    }
    elsif ($pattern eq 'y4m2d2'  ||  $pattern eq 'YMD')
    {
        return qq/(?k:(?k:$master{yr4})$m2middle(?k:$master{dy2}))/;
    }
    elsif ($pattern eq 'y2m2d2')
    {
        return qq/(?k:(?k:$master{yr2})$m2middle(?k:$master{dy2}))/;
    }

    # Probably the only way to get here is if I goof up and specify this subroutine
    # for a YMD pattern that is not handled above.
    die "Programming error: Unknown y-m-d pattern '$pattern'. Contact Regexp::Common::time author.";
}

# MDY builder
sub mdy
{
    my ($self, $flags_hr, $keys_ar) = @_;
    my $pattern = $keys_ar->[1];
    _setup_locale();

    # The second separator character is REQUIRED to be the same as the
    # first for YMD patterns, for the STRICT versions of these patterns
    # (the ones containing "m2d2").

    # 'mdy' is the most flexible: year: 2/4 digits; month 1/2 digits or named; day 1/2 digits.
    if ($pattern eq 'mdy')
    {
        return qq/(?k:$npd(?k:$anymon)$dmiddle(?k:$master{yr24})$nfd)/;
    }
    # 'mdy4': 4-digit year; 1 or 2 digit month and day.
    elsif ($pattern eq 'mdy4')
    {
        return qq/(?k:$npd(?k:$anymon)$dmiddle(?k:$master{yr4}))/;
    }
    # 'mdy2': 2-digit year; 1 or 2 digit month and day.
    elsif ($pattern eq 'mdy2')
    {
        return qq/(?k:$npd(?k:$anymon)$dmiddle(?k:$master{yr2}))/;
    }
    elsif ($pattern eq 'm2d2y4'  ||  $pattern eq 'MDY')
    {
        return qq/(?k:(?k:$master{mo2})$d2middle(?k:$master{yr4}))/;
    }
    elsif ($pattern eq 'm2d2y2')
    {
        return qq/(?k:(?k:$master{mo2})$d2middle(?k:$master{yr2}))/;
    }

    # Probably the only way to get here is if I goof up and specify this subroutine
    # for a YMD pattern that is not handled above.
    die "Programming error: Unknown m-d-y pattern '$pattern'. Contact Regexp::Common::time author.";
}

# DMY builder
sub dmy
{
    my ($self, $flags_hr, $keys_ar) = @_;
    my $pattern = $keys_ar->[1];
    _setup_locale();

    # The second separator character is REQUIRED to be the same as the
    # first for YMD patterns, for the STRICT versions of these patterns
    # (the ones containing "d2m2").

    # 'dmy' is the most flexible: year: 2/4 digits; month 1/2 digits; day 1/2 digits.
    if ($pattern eq 'dmy')
    {
        return qq/(?k:$npd$dcap$mFULLmiddle(?k:$master{yr24})$nfd)/;
    }
    # 'mdy4': 4-digit year; 1 or 2 digit month and day.
    elsif ($pattern eq 'dmy4')
    {
        return qq/(?k:$npd$dcap$mFULLmiddle(?k:$master{yr4}))/;
    }
    # 'y2md': 2-digit year; 1 or 2 digit month and day.
    elsif ($pattern eq 'dmy2')
    {
        return qq/(?k:$npd$dcap$mFULLmiddle(?k:$master{yr2}))/;
    }
    elsif ($pattern eq 'd2m2y4'  ||  $pattern eq 'DMY')
    {
        return qq/(?k:(?k:$master{dy2})$m2middle(?k:$master{yr4}))/;
    }
    elsif ($pattern eq 'd2m2y2')
    {
        return qq/(?k:(?k:$master{dy2})$m2middle(?k:$master{yr2}))/;
    }

    # Probably the only way to get here is if I goof up and specify this subroutine
    # for a YMD pattern that is not handled above.
    die "Programming error: Unknown d-m-y pattern '$pattern'. Contact Regexp::Common::time author.";
}

# HMS builder
sub hms
{
    my $hr     = $npd . q/[01]\d|2[0-4]|\d/;
    my $sec    = q/\d\d/;      # Can't limit it to 00-59!  Because it's optional, and out-of-range = no match.

#   my ($self, $flags_hr, $keys_ar) = @_;
    return qq/(?k:$npd(?k:$master{hr12})$tsep/      # hour
        .  qq/(?k:$master{mi2})/                    # minute
        .  qq/(?:$tsep(?k:$sec))?/                          # second
        .  qq/(?:\\s?(?k:$master{ampm}))?)/;        # am/pm
}

# Time::Format-like builder

my %tf =
    (
     yyyy   => $master{yr4},
     yy     => $master{yr2},
    'm{on}' => $master{mo12},
    'mm{on}'=> $master{mo2},
    '?m{on}'=> $master{mo_2},
     d      => $master{dy12},
     dd     => $master{dy2},
    '?d'    => $master{dy_2},
     h      => $master{hr12},
     hh     => $master{hr2},
    '?h'    => $master{hr_2},
     H      => $master{hx12},
     HH     => $master{hx2},
    '?H'    => $master{hx_2},
    'm{in}' => $master{mi12},
    'mm{in}'=> $master{mi2},
    '?m{in}'=> $master{mi_2},
     s      => $master{sc12},
     ss     => $master{sc2},
    '?s'    => $master{sc_2},
     mmm    => $master{msec},
     uuuuuu => $master{usec},
     am     => $master{ampm},
     AM     => $master{ampm},
    'a.m.'  => $master{ampm},
    'A.M.'  => $master{ampm},
     pm     => $master{ampm},
     PM     => $master{ampm},
    'p.m.'  => $master{ampm},
    'P.M.'  => $master{ampm},
     th     => $master{th},
     TH     => $master{th},
     tz     => $master{tz},
    );

my %disam;    # Disambiguator for 'm' format.
$disam{$_} = "{on}" foreach qw/yy d dd ?d/;           # If year or day is nearby, it's 'month'
$disam{$_} = "{in}" foreach qw/h hh ?h H HH ?H s ss ?s/;   # If hour or second is nearby, it's 'minute'
my $disambiguate_pat_1 = qr/
              (?<!\\)          # Must not follow a backslash
              (?=[ydhH])       # Must start with one of these
              (                # $1 begins
                (              # $2 begins.  Capture:
                    yy         #     a year
                  | [dhH]      #     a day or hour
                )
              [^?m\\]*         # Followed by something that's not part of a month
              )
              (?![?m]?m\{[io]n\})   # make sure it's not already unambiguous
              (?!mon)          # don't confuse "mon" with "m" "on"
              ([?m]?m)         # $3 is a month code
             /x;

my $disambiguate_pat_2 = qr/
              (?<!\\)         # ignore things that begin with backslash
              ([?m]?m)        # $1 is a month code
              (               # $2 begins.
                 [^a-zA-Z]*   #     any number of non-alphas
                 (?<!\\)      #     no backslash
                 (?=[?dsy])   #     Next char must be one of these
                 (            #     $3 begins.  Capture:
                    \??[ds]   #         a day or a second
                  | yy        #         or a year
                 )
              )/x;

# The Big Date/Time Pattern
my $bigpat = qr/
              (?<!\\)                      # Don't expand something preceded by backslash
              (?=[dDy?hHsaApPMmWwutT])     # Jump to one of these characters
              (
                 [Dd]ay|DAY                # Weekday abbreviation
              |  yy(?:yy)?                 # Year
              |  [?m]?m\{[oi]n\}           # Unambiguous month-minute codes
              |  th | TH                   # day suffix
              |  [?d]?d                    # Day
              |  [?h]?h                    # Hour (24)
              |  [?H]?H                    # Hour (12)
              |  [?s]?s                    # Second
              |  [apAP]\.?[mM]\.?          # am and pm strings
              |  [Mm]on(?:th)?|MON(?:TH)?  # Month names and abbrev
              |  [Ww]eekday|WEEKDAY        # Weekday names
              |  mmm|uuuuuu                # millisecond and microsecond
              |  tz                        # time zone
               )/x;

sub tf_builder
{
    my ($self, $flags_hr, $keys_ar) = @_;

    # User must specify *something* as the pattern
    _croak q{Mandatory "-pat" flag missing in tf pattern}
        if !exists $flags_hr->{-pat};

    my $pattern  = $flags_hr->{-pat};

    # Localize
    _setup_locale();

    # Copying from Time::Format...
    # "Guess" how to interpret ambiguous 'm'
    $pattern =~ s/$disambiguate_pat_1/$1$3$disam{$2}/gx;
    $pattern =~ s/$disambiguate_pat_2/$1$disam{$3}$2/gx;

    # If the pattern contains any parentheses, then the caller is
    # responsible for doing all the captures.
    if ($pattern =~ /(?<!\\)(?:\\\\)*\(/)   # even number of backslashes
    {
        $pattern =~ s/$bigpat/$tf{$1}/gx;
    }
    else        # we'll handle the capturing
    {
        $pattern =~ s/$bigpat/(?k:$tf{$1})/gx;
        $pattern = "(?k:$pattern)";
    }

    return $pattern;
}

# strftime builder
my %strftime =
    (
     C => $master{c2},     # two-digit century
     D =>"$master{mo2}/$master{dy2}/$master{yr2}",
     d => $master{dy2},    # two-digit day
     e => $master{dy_2},   # 1 or 2-digit day, leading space
     H => $master{hr2},    # hour, 00-23
     I => $master{hx2},    # hour, 01-12
     j => $master{doy3},   # day-of-year, 001-366
     m => $master{mo2},    # month, 01-12
     M => $master{mi2},    # minute, 00-59
     n => "\n",
     R =>"$master{hr2}:$master{mi2}",
     S => $master{sc2},    # Second, 00-61
     T =>"$master{hr2}:$master{mi2}:$master{sc2}",
     t => "\t",
     u => $master{wdx1},   # Weekday number, 1-7
     U => $master{wnx2},   # Week number, 00-53
     V => $master{wn2},    # Week number, 01-53
     w => $master{wd1},    # Weekday number, 0-6
     W => $master{wnx2},   # Week number, 00-53
     y => $master{yr2},    # two-digit year
     Y => $master{yr4},    # four-digit year
     Z => $master{tz},     # time zone
    '%' => '%',

     # additional useful patterns not specified by strftime
     _d => $master{dy12},  # 1- or 2-digit day number
     _H => $master{hr12},  # 1- or 2-digit 24-hour hour
     _I => $master{hx12},  # 1- or 2-digit 12-hour hour
     _m => $master{mo12},  # 1- or 2-digit month number
     _M => $master{mi12},  # 1- or 2-digit minute
   );

sub strftime_builder
{
    my ($self, $flags_hr, $keys_ar) = @_;

    # User must specify *something* as the pattern
    _croak q{Mandatory "-pat" flag missing in strftime pattern}
        if !exists $flags_hr->{-pat};

    my $pattern  = $flags_hr->{-pat};

    # Localize
    _setup_locale();

    # If the pattern contains any parentheses, then the caller is
    # responsible for doing all the captures.
    if ($pattern =~ /(?<!\\)(?:\\\\)*\(/)   # even number of backslashes
    {
        $pattern =~ s/(?<!\\)   %(_?.)   /$strftime{$1}/gx;
    }
    else        # we'll handle the capturing
    {
        # If the pattern consists of a single pattern, then
        # the enclosing (?k:) is redundant and annoying.
        my $solo = $pattern =~ /\A                 # Start of user's pattern
                                (?:
                                    \\b            # a word break
                                  |
                                    \\A            # Start of string
                                  |
                                    \^             # Start of string
                                  |
                                    \(\?[^\)]*\)   # Some other zero-width assertion
                                )*                 # (any number of such assertions)
                                %_?.        # The meat of the user's actual pattern
                                (?:
                                    \\b            # word break
                                  |
                                    \\z            # REAL end of string
                                  |
                                    \\Z            # end of string
                                  |
                                    \$             # end of line or string
                                  |
                                    \(\?[^\)]*\)   # some other assertion
                                )*
                                \z    # Actual end of user's pattern
                                /x;
        $pattern =~ s/(?<!\\)   %(_?.)   /(?k:$strftime{$1})/gx;
        $pattern = "(?k:$pattern)"  unless $solo;
    }

    return $pattern;
}

sub american
{
    my ($self, $flags_hr, $keys_ar) = @_;
    _setup_locale();

    return join '',
           qq/(?k:\\b/,           # must start on word boundary
           qq/(?k:$master{mname}|$master{mabbr})/,   # Month name or abbr
           qq/ {1,2}/,            # one or two spaces
           qq/(?k:$master{dy12})/,      # one- or two-digit day
           qq/(?:,| |, )/,        # Comma or space or both
           qq/(?k:'$master{yr2}|$master{yr24})/,     # Year: 'yy or yyyy or yy
           qq/$nfd)/;             # No following digits
}



# Localization.
# Bug: This is bulky and inefficient, and sets up many patterns that may never be used.
# On the other hand, it's generally only ever called once.
my $latest_setup_locale;
sub _setup_locale
{
    # Do nothing if locale has not changed since we set it up
    my $current_locale;
    $current_locale = $can_posix?  POSIX::setlocale(POSIX::LC_TIME())  :  q{};
    $current_locale = q{} if  !defined $current_locale;

    # No changes needed
    return if defined $latest_setup_locale
           &&  $latest_setup_locale eq $current_locale;

    $latest_setup_locale = $current_locale;

    my $dt_fmt;      # locale-specific date/time format
    my $d_fmt;       # locale-specific date format
    my $t_fmt;       # locale-specific time format
    my $t_ap_fmt;    # locale-specific time with am/pm format
    my $am_str;      # locale-specific ante-meridian string
    my $pm_str;      # locale-specific post-meridian string

    if ($can_locale)
    {
        eval
        {
            ($dt_fmt, $d_fmt, $t_fmt, $t_ap_fmt) = map langinfo($_),
                (
                 I18N::Langinfo::D_T_FMT(),
                 I18N::Langinfo::D_FMT(),
                 I18N::Langinfo::T_FMT(),
                 I18N::Langinfo::T_FMT_AMPM(),
                );
            ($am_str, $pm_str) = map langinfo($_),
                (
                 I18N::Langinfo::AM_STR(),
                 I18N::Langinfo::PM_STR(),
                );

        };
    }
    if (!$can_locale  ||  $@)    # Internationalization didn't work for some reason
    {
        $dt_fmt   = q{%a %b %e %H:%M:%S %Y};
        $d_fmt    = q{%m/%d/%y};
        $t_fmt    = q{%H:%M:%S};
        $t_ap_fmt = q{%I:%M:%S %p};
        $am_str   = q{AM};
        $pm_str   = q{PM};
    }

    # Update master patterns
    $master{dname} = _get_full_weekday_pattern();
    $master{dabbr} = _get_abbr_weekday_pattern();
    $master{mname} = _get_full_month_pattern();
    $master{mabbr} = _get_abbr_month_pattern();
    $master{axpx}  = qq/(?:\Q$am_str\E|\Q$pm_str\E)/;

    # Pattern variables for dmy-mdy-ymd patterns
    $anymon = _nospace qq/(?>(?i)$master{mo2}|$sdig|$master{mname}|$master{mabbr})/;
    $mFULLmiddle = _nospace qq{(?=(?>/$anymon/|-$anymon-| $anymon |\\.$anymon\\.|(?!$dsep)$anymon(?!$dsep)))$dsep?(?k:$anymon)$dsep?};

    # Pattern variables for Time::Format
    $tf{Weekday} = $tf{WEEKDAY} = $tf{weekday} = $master{dname};
    $tf{Day}     = $tf{DAY}     = $tf{day}     = $master{dabbr};
    $tf{Month}   = $tf{MONTH}   = $tf{month}   = $master{mname};
    $tf{Mon}     = $tf{MON}     = $tf{mon}     = $master{mabbr};

    # Pattern variables for strftime
    $strftime{A} = $master{dname};
    $strftime{a} = $master{dabbr};
    $strftime{B} = $master{mname};
    $strftime{b} = $master{mabbr};
    $strftime{h} = $strftime{b};    # defined synonym
    $strftime{r} ="$master{hx2}:$master{mi2}:$master{sc2} (?:$am_str|$pm_str)",

    # Set up locale-dependent strftime patterns
    $strftime{p} = $master{axpx};
    foreach ($dt_fmt, $d_fmt, $t_fmt, $t_ap_fmt)
    {
        # the "|| q{}" below is to avoid "uninitialized" warnings.
        s/%(.)/$strftime{$1} || q{}/eg;
    }
    $strftime{c} = _nospace $dt_fmt;
    $strftime{r} = _nospace $t_ap_fmt;
    $strftime{x} = _nospace $d_fmt;
    $strftime{X} = _nospace $t_fmt;
}

sub _first_chars
{
    my %uniq = map {substr ($_,0,1) => 1} @_;
    return join q{}, map quotemeta, keys %uniq;
}

sub _get_full_month_pattern
{
    my @Mon_Name;
    if ($can_locale)
    {
        eval
        {
            @Mon_Name = map langinfo($_),
                (
                 I18N::Langinfo::MON_1(),
                 I18N::Langinfo::MON_2(),
                 I18N::Langinfo::MON_3(),
                 I18N::Langinfo::MON_4(),
                 I18N::Langinfo::MON_5(),
                 I18N::Langinfo::MON_6(),
                 I18N::Langinfo::MON_7(),
                 I18N::Langinfo::MON_8(),
                 I18N::Langinfo::MON_9(),
                 I18N::Langinfo::MON_10(),
                 I18N::Langinfo::MON_11(),
                 I18N::Langinfo::MON_12(),
                );
        };
    }
    if (!$can_locale  ||  $@)
    {
        @Mon_Name = qw(January February March April May June July August September October November December);
    }

    my $prematch = _first_chars(@Mon_Name);
    my $alternat = join '|', map quotemeta, @Mon_Name;
    return qq/(?=[$prematch])(?>$alternat)/;
}

sub _get_abbr_month_pattern
{
    my $english_only = shift;
    my @Mon_Abbr;
    if (!$english_only  &&  $can_locale)
    {
        eval
        {
            @Mon_Abbr = map langinfo($_),
                (
                 I18N::Langinfo::ABMON_1(),
                 I18N::Langinfo::ABMON_2(),
                 I18N::Langinfo::ABMON_3(),
                 I18N::Langinfo::ABMON_4(),
                 I18N::Langinfo::ABMON_5(),
                 I18N::Langinfo::ABMON_6(),
                 I18N::Langinfo::ABMON_7(),
                 I18N::Langinfo::ABMON_8(),
                 I18N::Langinfo::ABMON_9(),
                 I18N::Langinfo::ABMON_10(),
                 I18N::Langinfo::ABMON_11(),
                 I18N::Langinfo::ABMON_12(),
                );
        };
    }
    if ($english_only  ||  !$can_locale  ||  $@)
    {
        @Mon_Abbr = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
    }

    my $prematch = _first_chars(@Mon_Abbr);
    my $alternat = join '|', map quotemeta, @Mon_Abbr;
    return qq/(?=[$prematch])(?>$alternat)/;
}

sub _get_full_weekday_pattern
{
    my @Day_Name;
    if ($can_locale)
    {
        eval
        {
            @Day_Name = map langinfo($_),
                (
                 I18N::Langinfo::DAY_1(),
                 I18N::Langinfo::DAY_2(),
                 I18N::Langinfo::DAY_3(),
                 I18N::Langinfo::DAY_4(),
                 I18N::Langinfo::DAY_5(),
                 I18N::Langinfo::DAY_6(),
                 I18N::Langinfo::DAY_7(),
                );
        };
    }
    if (!$can_locale  ||  $@)
    {
        @Day_Name = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
    }

    my $prematch = _first_chars(@Day_Name);
    my $alternat = join '|', map quotemeta, @Day_Name;
    return qq/(?=[$prematch])(?>$alternat)/;
}

sub _get_abbr_weekday_pattern
{
    my $english_only = shift;
    my @Day_Abbr;
    if (!$english_only  &&  $can_locale)
    {
        eval
        {
            @Day_Abbr = map langinfo($_),
                (
                 I18N::Langinfo::ABDAY_1(),
                 I18N::Langinfo::ABDAY_2(),
                 I18N::Langinfo::ABDAY_3(),
                 I18N::Langinfo::ABDAY_4(),
                 I18N::Langinfo::ABDAY_5(),
                 I18N::Langinfo::ABDAY_6(),
                 I18N::Langinfo::ABDAY_7(),
                );
        };
    }
    if ($english_only  ||  !$can_locale  ||  $@)
    {
        @Day_Abbr = qw(Sun Mon Tue Wed Thu Fri Sat);
    }

    my $prematch = _first_chars(@Day_Abbr);
    my $alternat = join '|', map quotemeta, @Day_Abbr;
    return qq/(?=[$prematch])(?>$alternat)/;
}

# Set up all the patterns

for (qw(ymd y4md y2md y4m2d2 y2m2d2 YMD))
{
    pattern name   => ['time', $_],
            create => \&ymd,
}

for (qw(mdy mdy4 mdy2 m2d2y4 m2d2y2 MDY))
{
    pattern name   => ['time', $_],
            create => \&mdy,
}

for (qw(dmy dmy4 dmy2 d2m2y4 d2m2y2 DMY))
{
    pattern name   => ['time', $_],
            create => \&dmy,
}

for (qw(hms))
{
    pattern name   => ['time', $_],
            create => \&hms,
}

for (qw(strftime))
{
    pattern name   => ['time', $_],
            create => \&strftime_builder,
}

for (qw(tf))
{
    pattern name   => ['time', $_],
            create => \&tf_builder,
}
for (qw(american))
{
    pattern name   => ['time', $_],
            create => \&american,
}


my $dt_sep = q/(?:(?<=\\d)[T_ ](?=\\d))?/;
pattern name => ['time', 'iso'],
        create => join '',
    qq/(?k:/,
    qq/(?=\\d)/,     # Expect a digit
    qq/(?:/,         # Begin optional date portion
        qq/(?k:$master{yr4})/,   $m2middle,   qq/(?k:$master{dy2})/,
    qq/)?/,          # End optional date portion
    $dt_sep,
    qq/(?:/,         # Begin optional time portion
        qq/(?k:$master{hr2})/,  $min2middle,  qq/(?k:$master{sc2})/,
    qq/)?)/;         # End optional time portion

pattern name => ['time', 'mail'],
    create => join '',
                   qq/(?k:$npd/,    # No preceeding digit
                   qq/(?=\\d)/,     # Expect a digit
                   qq/(?k:$master{dy12})\\s*/,     # Day
                   qq/(?k:$master{ema})\\s*/,      # Month (english name abbreviation)
                   qq/(?k:$master{yr24})\\s+/,     # Year
                   qq/(?k:$master{hr2}):/,         # Hour
                   qq/(?k:$master{mi2}):/,         # Minute
                   qq/(?k:$master{sc2})\\s*/,      # Second
                   qq/(?k:$master{tz})/,           # Time zone
                   qq/$nfd)/;       # No trailing digit

pattern name => ['time', 'MAIL'],
    create => join '',
                   qq/(?k:$npd/,    # No preceeding digit
                   qq/(?=\\d)/,     # Expect a digit
                   qq/(?k:$master{dy12})\\s*/,     # Day
                   qq/(?k:$master{ema})\\s*/,      # Month (english name abbreviation)
                   qq/(?k:$master{yr4})\\s+/,      # Year
                   qq/(?k:$master{hr2}):/,         # Hour
                   qq/(?k:$master{mi2}):/,         # Minute
                   qq/(?k:$master{sc2})\\s*/,      # Second
                   qq/(?k:[-+]\\d{4})/,            # Time zone
                   qq/$nfd)/;       # No trailing digit


1;
__END__

=head1 SYNOPSIS

 use Regexp::Common qw(time);

 # Piecemeal, Time::Format-like patterns
 $RE{time}{tf}{-pat => 'pattern'}

 # Piecemeal, strftime-like patterns
 $RE{time}{strftime}{-pat => 'pattern'}

 # Match ISO8601-style date/time strings
 $RE{time}{iso}

 # Match RFC2822-style date/time strings
 $RE{time}{mail}
 $RE{time}{MAIL}    # more-strict matching

 # Match informal American date strings
 $RE{time}{american}

 # Fuzzy date patterns
 #               YEAR/MONTH/DAY
 $RE{time}{ymd}         # Most flexible
 $RE{time}{YMD}         # Strictest (equivalent to y4m2d2)
                 # Other available patterns: y2md, y4md, y2m2d2, y4m2d2

 #               MONTH/DAY/YEAR  (American style)
 $RE{time}{mdy}         # Most flexible
 $RE{time}{MDY}         # Strictest (equivalent to m2d2y4)
                 # Other available patterns: mdy2, mdy4, m2d2y2, m2d2y4

 #               DAY/MONTH/YEAR  (European style)
 $RE{time}{mdy}         # Most flexible
 $RE{time}{MDY}         # Strictest (equivalent to d2m2y4)
                 # Other available patterns: dmy2, dmy4, d2m2y2, d2m2y4

 # Fuzzy time pattern
 #               HOUR/MINUTE/SECOND
 $RE{time}{hms}    # H: matches 1 or 2 digits; 12 or 24 hours
                   # M: matches 2 digits.
                   # S: matches 2 digits; may be omitted
                   # May be followed by "a", "am", "p.m.", etc.


=head1 DESCRIPTION

This module creates regular expressions that can be used for parsing
dates and times.  See L<Regexp::Common> for a general description of
how to use this interface.

Parsing dates is a dirty business. Dates are generally specified in
one of three possible orders: year/month/day, month/day/year, or
day/month/year.  Years can be specified with four digits or with two
digits (with assumptions made about the century).  Months can be
specified as one digit, two digits, as a spelled-out name, or as a
three-letter abbreviation.  Day numbers can be one digit or two
digits, with limits depending on the month (and, in the case of
February, even the year).  Also, different people use different
punctuation for separating the various elements.

A human can easily recognize that "October 21, 2005" and "21.10.05"
refer to the same date, but it's tricky to get a program to come to
the same conclusion.  This module attempts to make it possible to do
so, with a minimum of difficulty.

=over 4

=item *

If you know the exact format of the data to be matched, use one of the
specific, piecemeal pattern builders: C<tf> or C<strftime>.

=item *

If you are parsing RFC-2822 mail headers, use the C<mail> pattern.

=item *

If you are parsing informal American dates, use the C<american> pattern.

=item *

If there is some variability in your input data, use one of the
fuzzy-matching patterns in the C<dmy>, C<mdy>, or C<ymd> families.

=item *

If the data are wildly variable, such as raw user input, you should
probably give up and use L<Date::Manip> or L<Date::Parse>.

=back

Time values are generally much simpler to parse than date values.
Only one fuzzy pattern is provided, and it should suffice for most
needs.

=head1 Time::Format PATTERNS

The L<Time::Format> module uses simple, intuitive strings for
specifying date and time formats.  You can use these patterns here as
well.  See L<Time::Format> for details about its format specifiers.

I<Example:>

    $str = 'Thu November 2, 2005';
    $str =~ $RE{time}{tf}{-pat => 'Day Month d, yyyy'};

The patterns can contain more complex regexp expressions as well:

    $str =~ $RE{time}{tf}{-pat => '(Weekday|Day) (Month|Mon) d, yyyy'};

Time zone matching (the C<tz> format code) attempts to adhere to RFC2822
and ISO8601 as much as possible.  The following time zones are matched:

    Z
    UT        UTC
    +hh:mm    -hh:mm
    +hhmm     -hhmm
    +hh       -hh
    GMT   EST EDT   CST CDT   MST MDT   PST PDT

=head1 strftime PATTERNS

The POSIX C<strftime> function is a long-recognized standard for
formatting dates and times.  This module supports most of C<stftime>'s
codes for matching; specifically, the C<aAbBcCDdeHIjmMnprRSTtuUVwWyxXYZ%>
codes.  The C<%Z> format matches time zones in the same manner as
described above under L</Time::Format PATTERNS>.

Also, this module provides the following nonstandard codes:

C<   %_d  -> 1- or 2-digit day number (1-31)

C<   %_H  -> 1- or 2-digit hour (0-23)

C<   %_I  -> 1- or 2-digit hour (1-12)

C<   %_m  -> 1- or 2-digit month number (1-12)

C<   %_M  -> 1- or 2-digit minute (0-59)

I<Example:>

    $str = 'Thu November 2, 2005';
    $str =~ $RE{time}{strftime}{-pat => '%a %B %_d, %Y'};

The patterns can contain more complex regexp expressions as well:

    $str =~ $RE{time}{strftime}{-pat => '(%A|%a)? (%B|%b) ?%_d, %Y'};

=head1 ISO-8601 DATE/TIME MATCHING

The C<$RE{time}{iso}> pattern will match most (all?) strings formatted
as recommended by ISO-8601.  The canonical ISO-8601 form is:

    YYYY-MM-DDTHH:MM:SS

(where "C<T>" is a literal T character).  The C<$RE{time}{iso}>
pattern will match this form, and some variants:

=over 4

=item *

The date separator character may be a hyphen, slash (C</>), period, or
empty string (omitted).  The two date separators must match.

=item *

The time separator character may be a colon, a period, a space, or
empty string (omitted).  The two time separators must match.

=item *

The date-time separator may be a C<T>, an underscore, a space, or
empty string (omitted).

=item *

Either the date or the time may be omitted.  But at least one must be
there.

=item *

If the date is not omitted, all three of its components must be present.

=item *

If the time is not omitted, all three of its components must be present.

=back

=head1 RFC 2822 MATCHING

RFC 2822 specifies the format of date/time values in e-mail message
headers.  In a nutshell, the format is:

    dd Mon yyyy hh:mm:ss +zzzz

where C<dd> is the day of the month; C<Mon> is the abbreviated month
name (apparently always in English); C<yyyy> is the year; C<hh:mm:ss>
is the time; and C<+zzzz> is the time zone, generally specified as an
offset from GMT.

RFC 2822 requires that the weekday also be specified, but this module
ignores the weekday, as it is redundant and only supplied for human
readability.

RFC 2822 requires that older, obsolete date forms be allowed as well;
for example, alphanumeric time zone codes (e.g. EDT).  This module's
C<mail> allows for these obsolete date forms.  If you want to match
only the proper date forms recommended by RFC 2822, you can use the
C<MAIL> pattern instead.

In either case, C<mail> or C<MAIL>, the pattern generated is very
flexible about whitespace.  The main differences are: with C<MAIL>,
two-digit years are not permitted, and the time zone must be four
digits preceded by a + or - sign.

=head1 INFORMAL AMERICAN MATCHING

People in North America, particularly in the United States, are fond
of specifying dates as "Month dd, yyyy", or sometimes with a two-digit
year and apostrophe: "Month dd, 'yy".  The C<american> pattern matches
this style of date.  It allows either a month name or abbreviation,
and is flexible with respect to commas and whitespace.

=head1 FUZZY PATTERN OVERVIEW

Fuzzy date patterns have the following properties in common:

=over

=item *

The pattern names consist of the letters C<y>, C<m>, and C<d>, each
optionally followed by a digit (C<2> for C<m> and C<d>; C<2>
or C<4> for C<y>).

=item *

If a C<y> is followed by a 2 or a 4, it must match that many digits.

=item *

If a C<y> has no trailing digit, it can match I<either> 2 or 4 digits,
trying 4 first.

=item *

If an C<m> is followed by a 2, then only two-digit matches for the
month are considered, and month names are not matched.

=item *

If an C<m> is not followed by a 2, then the month may be 1 or 2
digits, or a spelled-out name.

=item *

Just like for months, if a C<d> is followed by a 2, then only
two-digit matches for the day are considered.

=item *

Just like for months, if a C<d> has no trailing digit, then the day
may be 1 or 2 digits, and a 1-digit match may not have any adjacent
digits.

=item *

The uppercase C<DMY>, C<MDY>, and C<YMD> patterns are synonyms for the
strict C<d2m2y4>, C<m2d2y4>, and C<y4m2d2> patterns, respectively.

=item *

If a one-digit match is considered for the month, then no adjacent
digits are allowed.  (e.g.: "1/23/45" in M/D/Y format has a valid
one-digit month match, but "12345" does not.  Nor does "91/23/45").

=item *

If a pattern begins with an digitless C<d>, C<m>, or C<y>, then, in
the string to be matched, any leading digits will cause the pattern to
fail.  For example: C<"012/23/45"> will I<not> match C<$RE{time}{mdy}>.
However, it I<will> match C<$RE{time}{m2d2y2}>.  If you specify an
exact pattern by using C<m2> instead of C<m>, this module assumes you
know what you're doing.

=item *

Likewise, a pattern ending with a digitless C<d> or C<y> will not
match if there are trailing digits in the string.

=back

=head1 FUZZY PATTERN DETAILS

=head2 Year-Month-Day order

=over

=item $RE{time}{ymd}

 "05/4/2"      =~ $RE{time}{ymd};
 "2005-APR-02" =~ $RE{time}{ymd};

This is the most flexible of the numeric-only year/month/day formats.
It matches a date of the form "year/month/day", where the year may be
2 or 4 digits; the month may be 1 or 2 digits or a spelled-out name or
name abbreviation, and the day may be 1 or 2 digits.  The
year/month/day elements may be directly adjacent to each other, or may
be separated by a space, period, slash (C</>), or hyphen.

=item $RE{time}{y4md}

 "2005/4/2"    =~ $RE{time}{y4md};
 "2005 APR 02" =~ $RE{time}{y4md};

This works as L<$RE{time}{ymd}>, except that the year is restricted to
be exactly 4 digits.

=item $RE{time}{y4m2d2}

 "2005/04/02" =~ $RE{time}{y4m2d2};

This works as L<$RE{time}{ymd}>, except that the year is restricted to
be exactly 4 digits, and the month and day must be exactly 2 digits each.

=item $RE{time}{y2md}

 "05/4/2"    =~ $RE{time}{y2md};
 "05.APR.02" =~ $RE{time}{y2md};

This works as L<$RE{time}{ymd}>, except that the year is restricted to
be exactly 2 digits.

=item $RE{time}{y2m2d2}

 "05/04/02" =~ $RE{time}{y2m2d2};

This works as L<$RE{time}{ymd}>, except that the year is restricted to
be exactly 2 digits, and the month and day must be exactly 2 digits each.

=item $RE{time}{YMD}

 "2005/04/02" =~ $RE{time}{YMD};

This is a shorthand for the "canonical" year/month/day format, C<y4m2d2>.

=back

=head2 Month-Day-Year (American) order

=over

=item $RE{time}{mdy}

=item $RE{time}{mdy4}

=item $RE{time}{m2d2y4}

=item $RE{time}{mdy2}

=item $RE{time}{m2d2y2}

=item $RE{time}{MDY}

These patterns function as the equivalent year/month/day patterns,
above; the only difference is the order of the elements.  C<MDY> is a
synonym for C<m2d2y4>.

=back

=head2 Day-Month-Year (European) order

=over

=item $RE{time}{dmy}

=item $RE{time}{dmy4}

=item $RE{time}{d2m2y4}

=item $RE{time}{dmy2}

=item $RE{time}{d2m2y2}

=item $RE{time}{DMY}

These patterns function as the equivalent year/month/day patterns,
above; the only difference is the order of the elements.  C<DMY> is a
synonym for C<d2m2y4>.

=back

=head1 Time pattern (Hour-minute-second)

=over

=item $RE{time}{hms}

 "10:06:12a" =~ /$RE{time}{hms}/;
 "9:00 p.m." =~ /$RE{time}{hms}/;

Matches a time value in a string.

The hour must be in the range 0 to 24.  The minute and second values
must be in the range 0 to 59, and must be two digits (i.e., they must
have leading zeroes if less than 10).

The hour, minute, and second components may be separated by colons
(C<:>), periods, or spaces.

The "seconds" value may be omitted.

The time may be followed by an "am/pm" indicator; that is, one of the
following values:

  a   am   a.m.  p   pm   p.m.   A   AM   A.M.  P   PM   P.M.

There may be a space between the time and the am/pm indicator.

=back

=head1 CAPTURES (-keep)

Under C<-keep>, the C<tf> and C<strftime> patterns capture the entire
match as C<$1>, plus one capture variable for each format specifier.
However, if your pattern contains any parentheses, C<tf> and
C<strftime> will I<not> capture anything additional beyond what you
specify, C<-keep> or not.  In other words: if you use parentheses, you
are responsible for all capturing.

The C<iso> pattern captures:

C<  $1  -> the entire match

C<  $2  -> the year

C<  $3  -> the month

C<  $4  -> the day

C<  $5  -> the hour

C<  $6  -> the minute

C<  $7  -> the second

The year, month, and day (C<$2>, C<$3>, and C<$4>) will be C<undef> if
the matched string contains only a time value (e.g., "12:34:56").  The
hour, minute, and second (C<$5>, C<$6>, and C<$7>) will be C<undef> if
the matched string contains only a date value (e.g., "2005-01-23").


The C<mail> and C<MAIL> patterns capture:

C<  $1  -> the entire match

C<  $2  -> the day

C<  $3  -> the month

C<  $4  -> the year

C<  $5  -> the hour

C<  $6  -> the minute

C<  $7  -> the second

C<  $8  -> the time zone


The C<american> pattern captures:

C<  $1  -> the entire match

C<  $2  -> the month

C<  $3  -> the day

C<  $4  -> the year


The fuzzy y/m/d patterns capture

C<  $1  -> the entire match

C<  $2  -> the year

C<  $3  -> the month

C<  $4  -> the day


The fuzzy m/d/y patterns capture

C<  $1  -> the entire match

C<  $2  -> the month

C<  $3  -> the day

C<  $4  -> the year


The fuzzy d/m/y patterns capture

C<  $1  -> the entire match

C<  $2  -> the day

C<  $3  -> the month

C<  $4  -> the year


The fuzzy h/m/s pattern captures

C<  $1  -> the entire match

C<  $2  -> the hour

C<  $3  -> the minute

C<  $4  -> the second  (C<undef> if omitted)

C<  $5  -> the am/pm indicator (C<undef> if omitted)


=head1 EXAMPLES

 # Typical usage: parsing a data record.
 #
 $rec = "blah blah 2005/10/21 blah blarrrrrgh";
 @date = $rec =~ m{^blah blah $RE{time}{YMD}{-keep}};
 # or
 @date = $rec =~ m{^blah blah $RE{time}{tf}{-pat=>'yyyy/mm/dd'}{-keep}};
 # or
 @date = $rec =~ m{^blah blah $RE{time}{strftime}{-pat=>'%Y/%m/%d'}{-keep}};

 # Typical usage: parsing variable-format data.
 #
 use Time::Normalize;

 $record = "10-SEP-2005";

 # This block tries M-D-Y first, then D-M-Y, then Y-M-D
 my $matched;
 foreach my $pattern (qw(mdy dmy ymd))
 {
     @values = $record =~ /^$RE{time}{$pattern}{-keep}/
         or next;

     $matched = $pattern;
 }
 if ($matched)
 {
     eval{ ($year, $month, $day) = normalize_rct($matched, @values) };
     if ($@)
     {
         .... # handle erroneous data
     }
 }
 else
 {
     .... # no match
 }
 #
 # $day is now 10; $month is now 09; $year is now 2005.


 # Time examples

 $time = '9:10pm';

 @time_data = $time =~ /$RE{time}{hms}{-keep}/;
 # captures '9:10pm', '9', '10', undef, 'pm'

 @time_data = $time =~ /$RE{time}{tf}{-pat => '(h):(mm)(:ss)?(am)?'}{-keep}/;
 # captures '9', '10', undef, 'pm'

=head1 EXPORTS

This module exports no symbols to the caller's namespace.

=head1 SEE ALSO

It's not enough that the date regexps can match various formats.  You
then have to parse those matched data values and translate them into
useful values.  The L<Time::Normalize> module is highly recommended
for performing this repetitive, error-prone task.

=head1 REQUIREMENTS

Requires L<Regexp::Common>, of course.

If L<POSIX> and L<I18N::Langinfo> are available, this module will use
them; otherwise, it will use hardcoded English values for month and
weekday names.

L<Test::More> is required for the test suite.

=head1 AUTHOR / COPYRIGHT

Copyright (c) 2005-2008 by Eric J. Roode, ROODE I<-at-> cpan I<-dot-> org

All rights reserved.

To avoid my spam filter, please include "Perl", "module", or this
module's name in the message's subject line, and/or GPG-sign your
message.

This module is copyrighted only to ensure proper attribution of
authorship and to ensure that it remains available to all.  This
module is free, open-source software.  This module may be freely used
for any purpose, commercial, public, or private, provided that proper
credit is given, and that no more-restrictive license is applied to
derivative (not dependent) works.

Substantial efforts have been made to ensure that this software meets
high quality standards; however, no guarantee can be made that there
are no undiscovered bugs, and no warranty is made as to suitability to
any given use, including merchantability.  Should this module cause
your house to burn down, your dog to collapse, your heart-lung machine
to fail, your spouse to desert you, or George Bush to be re-elected, I
can offer only my sincere sympathy and apologies, and promise to
endeavor to improve the software.


=cut