unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 861a70506acad209d5d5676100e34285b618e062 38580 bytes (raw)
name: parse-time-string/parse-time-string.c 	 # note: path name is non-authoritative(*)

   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
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
 
/*
 * parse time string - user friendly date and time parser
 * Copyright © 2012 Jani Nikula
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Jani Nikula <jani@nikula.org>
 */

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>

#include "parse-time-string.h"

/*
 * IMPLEMENTATION DETAILS
 *
 * At a high level, the parsing is done in two phases: 1) actual
 * parsing of the input string and storing the parsed data into
 * 'struct state', and 2) processing of the data in 'struct state'
 * according to current time (or provided reference time) and
 * rounding. This is evident in the main entry point function
 * parse_time_string().
 *
 * 1) The parsing phase - parse_input()
 *
 * Parsing is greedy and happens from left to right. The parsing is as
 * unambiguous as possible; only unambiguous date/time formats are
 * accepted. Redundant or contradictory absolute date/time in the
 * input (e.g. date specified multiple times/ways) is not
 * accepted. Relative date/time on the other hand just accumulates if
 * present multiple times (e.g. "5 days 5 days" just turns into 10
 * days).
 *
 * Parsing decisions are made on the input format, not value. For
 * example, "20/5/2005" fails because the recognized format here is
 * MM/D/YYYY, even though the values would suggest DD/M/YYYY.
 *
 * Parsing is mostly stateless in the sense that parsing decisions are
 * not made based on the values of previously parsed data, or whether
 * certain data is present in the first place. (There are a few
 * exceptions to the latter part, though, such as parsing of time zone
 * that would otherwise look like plain time.)
 *
 * When the parser encounters a number that is not greedily parsed as
 * part of a format, the interpretation is postponed until the next
 * token is parsed. The parser for the next token may consume the
 * previously postponed number. For example, when parsing "20 May" the
 * meaning of "20" is not known until "May" is parsed. If the parser
 * for the next token does not consume the postponed number, the
 * number is handled as a "lone" number before parser for the next
 * token finishes.
 *
 * 2) The processing phase - create_output()
 *
 * Once the parser in phase 1 has finished, 'struct state' contains
 * all the information from the input string, and it's no longer
 * needed. Since the parser does not even handle the concept of "now",
 * the processing initializes the fields referring to the current
 * date/time.
 *
 * If requested, the result is rounded towards past or future. The
 * idea behind rounding is to support parsing date/time ranges in an
 * obvious way. For example, for a range defined as two dates (without
 * time), one would typically want to have an inclusive range from the
 * beginning of start date to the end of the end date. The caller
 * would use rounding towards past in the start date, and towards
 * future in the end date.
 *
 * The absolute date and time is shifted by the relative date and
 * time, and time zone adjustments are made. Daylight saving time
 * (DST) is specifically *not* handled at all.
 *
 * Finally, the result is stored to time_t.
 */

#define unused(x) x __attribute__ ((unused))

/* XXX: Redefine these to add i18n support. The keyword table uses
 * N_() to mark strings to be translated; they are accessed
 * dynamically using _(). */
#define _(s) (s)	/* i18n: define as gettext (s) */
#define N_(s) (s)	/* i18n: define as gettext_noop (s) */

#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))

/*
 * Field indices in the tm and set arrays of struct state.
 *
 * NOTE: There's some code that depends on the ordering of this enum.
 */
enum field {
    /* Keep SEC...YEAR in this order. */
    TM_ABS_SEC,		/* seconds */
    TM_ABS_MIN,		/* minutes */
    TM_ABS_HOUR,	/* hours */
    TM_ABS_MDAY,	/* day of the month */
    TM_ABS_MON,		/* month */
    TM_ABS_YEAR,	/* year */

    TM_ABS_WDAY,	/* day of the week. special: may be relative */
    TM_ABS_ISDST,	/* daylight saving time */

    TM_AMPM,		/* am vs. pm */
    TM_TZ,		/* timezone in minutes */

    /* Keep SEC...YEAR in this order. */
    TM_REL_SEC,		/* seconds relative to absolute or reference time */
    TM_REL_MIN,		/* minutes ... */
    TM_REL_HOUR,	/* hours ... */
    TM_REL_DAY,		/* days ... */
    TM_REL_MON,		/* months ... */
    TM_REL_YEAR,	/* years ... */
    TM_REL_WEEK,	/* weeks ... */

    TM_NONE,		/* not a field */

    TM_SIZE = TM_NONE,
    TM_FIRST_ABS = TM_ABS_SEC,
    TM_FIRST_REL = TM_REL_SEC,
};

/* Values for the set array of struct state. */
enum field_set {
    FIELD_UNSET,	/* The field has not been touched by parser. */
    FIELD_SET,		/* The field has been set by parser. */
    FIELD_NOW,		/* The field will be set to reference time. */
};

static enum field
next_abs_field (enum field field)
{
    /* NOTE: Depends on the enum ordering. */
    return field < TM_ABS_YEAR ? field + 1 : TM_NONE;
}

static enum field
abs_to_rel_field (enum field field)
{
    assert (field <= TM_ABS_YEAR);

    /* NOTE: Depends on the enum ordering. */
    return field + (TM_FIRST_REL - TM_FIRST_ABS);
}

/* Get epoch value for field. */
static int
field_epoch (enum field field)
{
    if (field == TM_ABS_MDAY || field == TM_ABS_MON)
	return 1;
    else if (field == TM_ABS_YEAR)
	return 1970;
    else
	return 0;
}

/* The parsing state. */
struct state {
    int tm[TM_SIZE];			/* parsed date and time */
    enum field_set set[TM_SIZE];	/* set status of tm */

    enum field last_field;	/* Previously set field. */
    enum field next_field;	/* Next field for parse_postponed_number() */
    char delim;

    int postponed_length;	/* Number of digits in postponed value. */
    int postponed_value;
    char postponed_delim;	/* The delimiter preceding postponed number. */
};

/*
 * Helpers for postponed numbers.
 *
 * postponed_length is the number of digits in postponed value. 0
 * means there is no postponed number. -1 means there is a postponed
 * number, but it comes from a keyword, and it doesn't have digits.
 */
static int
get_postponed_length (struct state *state)
{
    return state->postponed_length;
}

/*
 * Consume a previously postponed number. Return true if a number was
 * in fact postponed, false otherwise. Store the postponed number's
 * value in *v, length in the input string in *n (or -1 if the number
 * was written out and parsed as a keyword), and the preceding
 * delimiter to *d.
 */
static bool
get_postponed_number (struct state *state, int *v, int *n, char *d)
{
    if (!state->postponed_length)
	return false;

    if (n)
	*n = state->postponed_length;

    if (v)
	*v = state->postponed_value;

    if (d)
	*d = state->postponed_delim;

    state->postponed_length = 0;
    state->postponed_value = 0;
    state->postponed_delim = 0;

    return true;
}

/* Parse a previously postponed number if one exists. */
static int parse_postponed_number (struct state *state, int v, int n, char d);
static int
handle_postponed_number (struct state *state, enum field next_field)
{
    int v = state->postponed_value;
    int n = state->postponed_length;
    char d = state->postponed_delim;
    int r;

    if (!n)
	return 0;

    state->postponed_value = 0;
    state->postponed_length = 0;
    state->postponed_delim = 0;

    state->next_field = next_field;
    r = parse_postponed_number (state, v, n, d);
    state->next_field = TM_NONE;

    return r;
}

/*
 * Postpone a number to be handled later. If one exists already,
 * handle it first. n may be -1 to indicate a keyword that has no
 * number length.
 */
static int
set_postponed_number (struct state *state, int v, int n)
{
    int r;
    char d = state->delim;

    /* Parse a previously postponed number, if any. */
    r = handle_postponed_number (state, TM_NONE);
    if (r)
	return r;

    state->postponed_length = n;
    state->postponed_value = v;
    state->postponed_delim = d;

    return 0;
}

static void
set_delim (struct state *state, char delim)
{
    state->delim = delim;
}

static void
unset_delim (struct state *state)
{
    state->delim = 0;
}

/*
 * Field set/get/mod helpers.
 */

/* Return true if field has been set. */
static bool
is_field_set (struct state *state, enum field field)
{
    assert (field < ARRAY_SIZE (state->tm));

    return field < ARRAY_SIZE (state->set) &&
	   state->set[field] != FIELD_UNSET;
}

static void
unset_field (struct state *state, enum field field)
{
    assert (field < ARRAY_SIZE (state->tm));

    state->set[field] = FIELD_UNSET;
    state->tm[field] = 0;
}

/*
 * Set field to value. A field can only be set once to ensure the
 * input does not contain redundant and potentially conflicting data.
 */
static int
set_field (struct state *state, enum field field, int value)
{
    int r;

    assert (field < ARRAY_SIZE (state->tm));

    /* Fields can only be set once. */
    if (field < ARRAY_SIZE (state->set) && state->set[field] != FIELD_UNSET)
	return -PARSE_TIME_ERR_ALREADYSET;

    state->set[field] = FIELD_SET;

    /* Parse postponed number, if any. */
    r = handle_postponed_number (state, field);
    if (r)
	return r;

    unset_delim (state);

    state->tm[field] = value;
    state->last_field = field;

    return 0;
}

/*
 * Mark n fields in fields to be set to the reference date/time in the
 * specified time zone, or local timezone if not specified. The fields
 * will be initialized after parsing is complete and timezone is
 * known.
 */
static int
set_fields_to_now (struct state *state, enum field *fields, size_t n)
{
    size_t i;
    int r;

    for (i = 0; i < n; i++) {
	r = set_field (state, fields[i], 0);
	if (r)
	    return r;
	state->set[fields[i]] = FIELD_NOW;
    }

    return 0;
}

/* Modify field by adding value to it. To be used on relative fields,
 * which can be modified multiple times (to accumulate). */
static int
mod_field (struct state *state, enum field field, int value)
{
    int r;

    assert (field < ARRAY_SIZE (state->tm));   /* assert relative??? */

    if (field < ARRAY_SIZE (state->set))
	state->set[field] = FIELD_SET;

    /* Parse postponed number, if any. */
    r = handle_postponed_number (state, field);
    if (r)
	return r;

    unset_delim (state);

    state->tm[field] += value;
    state->last_field = field;

    return 0;
}

/*
 * Get field value. Make sure the field is set before query. It's most
 * likely an error to call this while parsing (for example fields set
 * as FIELD_NOW will only be set to some value after parsing).
 */
static int
get_field (struct state *state, enum field field)
{
    assert (field < ARRAY_SIZE (state->tm));

    return state->tm[field];
}

/*
 * Validity checkers.
 */
static bool is_valid_12hour (int h)
{
    return h >= 0 && h <= 12;
}

static bool is_valid_time (int h, int m, int s)
{
    /* Allow 24:00:00 to denote end of day. */
    if (h == 24 && m == 0 && s == 0)
	return true;

    return h >= 0 && h <= 23 && m >= 0 && m <= 59 && s >= 0 && s <= 59;
}

static bool is_valid_mday (int mday)
{
    return mday >= 1 && mday <= 31;
}

static bool is_valid_mon (int mon)
{
    return mon >= 1 && mon <= 12;
}

static bool is_valid_year (int year)
{
    return year >= 1970;
}

static bool is_valid_date (int year, int mon, int mday)
{
    return is_valid_year (year) && is_valid_mon (mon) && is_valid_mday (mday);
}

/* Unset indicator for time and date set helpers. */
#define UNSET -1

/* Time set helper. No input checking. Use UNSET (-1) to leave unset. */
static int
set_abs_time (struct state *state, int hour, int min, int sec)
{
    int r;

    if (hour != UNSET) {
	if ((r = set_field (state, TM_ABS_HOUR, hour)))
	    return r;
    }

    if (min != UNSET) {
	if ((r = set_field (state, TM_ABS_MIN, min)))
	    return r;
    }

    if (sec != UNSET) {
	if ((r = set_field (state, TM_ABS_SEC, sec)))
	    return r;
    }

    return 0;
}

/* Date set helper. No input checking. Use UNSET (-1) to leave unset. */
static int
set_abs_date (struct state *state, int year, int mon, int mday)
{
    int r;

    if (year != UNSET) {
	if ((r = set_field (state, TM_ABS_YEAR, year)))
	    return r;
    }

    if (mon != UNSET) {
	if ((r = set_field (state, TM_ABS_MON, mon)))
	    return r;
    }

    if (mday != UNSET) {
	if ((r = set_field (state, TM_ABS_MDAY, mday)))
	    return r;
    }

    return 0;
}

/*
 * Keyword parsing and handling.
 */
struct keyword;
typedef int (*setter_t)(struct state *state, struct keyword *kw);

struct keyword {
    const char *name;	/* keyword */
    enum field field;	/* field to set, or FIELD_NONE if N/A */
    int value;		/* value to set, or 0 if N/A */
    setter_t set;	/* function to use for setting, if non-NULL */
};

/*
 * Setter callback functions for keywords.
 */
static int
kw_set_default (struct state *state, struct keyword *kw)
{
    return set_field (state, kw->field, kw->value);
}

static int
kw_set_rel (struct state *state, struct keyword *kw)
{
    int multiplier = 1;

    /* Get a previously set multiplier, if any. */
    get_postponed_number (state, &multiplier, NULL, NULL);

    /* Accumulate relative field values. */
    return mod_field (state, kw->field, multiplier * kw->value);
}

static int
kw_set_number (struct state *state, struct keyword *kw)
{
    /* -1 = no length, from keyword. */
    return set_postponed_number (state, kw->value, -1);
}

static int
kw_set_month (struct state *state, struct keyword *kw)
{
    int n = get_postponed_length (state);

    /* Consume postponed number if it could be mday. This handles "20
     * January". */
    if (n == 1 || n == 2) {
	int r, v;

	get_postponed_number (state, &v, NULL, NULL);

	if (!is_valid_mday (v))
	    return -PARSE_TIME_ERR_INVALIDDATE;

	r = set_field (state, TM_ABS_MDAY, v);
	if (r)
	    return r;
    }

    return set_field (state, kw->field, kw->value);
}

static int
kw_set_ampm (struct state *state, struct keyword *kw)
{
    int n = get_postponed_length (state);

    /* Consume postponed number if it could be hour. This handles
     * "5pm". */
    if (n == 1 || n == 2) {
	int r, v;

	get_postponed_number (state, &v, NULL, NULL);

	if (!is_valid_12hour (v))
	    return -PARSE_TIME_ERR_INVALIDTIME;

	r = set_abs_time (state, v, 0, 0);
	if (r)
	    return r;
    }

    return set_field (state, kw->field, kw->value);
}

static int
kw_set_timeofday (struct state *state, struct keyword *kw)
{
    return set_abs_time (state, kw->value, 0, 0);
}

static int
kw_set_today (struct state *state, unused (struct keyword *kw))
{
    enum field fields[] = { TM_ABS_YEAR, TM_ABS_MON, TM_ABS_MDAY };

    return set_fields_to_now (state, fields, ARRAY_SIZE (fields));
}

static int
kw_set_now (struct state *state, unused (struct keyword *kw))
{
    enum field fields[] = { TM_ABS_HOUR, TM_ABS_MIN, TM_ABS_SEC };

    return set_fields_to_now (state, fields, ARRAY_SIZE (fields));
}

static int
kw_set_ordinal (struct state *state, struct keyword *kw)
{
    int n, v;

    /* Require a postponed number. */
    if (!get_postponed_number (state, &v, &n, NULL))
	return -PARSE_TIME_ERR_DATEFORMAT;

    /* Ordinals are mday. */
    if (n != 1 && n != 2)
	return -PARSE_TIME_ERR_DATEFORMAT;

    /* Be strict about st, nd, rd, and lax about th. */
    if (strcasecmp (kw->name, "st") == 0 && v != 1 && v != 21 && v != 31)
	return -PARSE_TIME_ERR_INVALIDDATE;
    else if (strcasecmp (kw->name, "nd") == 0 && v != 2 && v != 22)
	return -PARSE_TIME_ERR_INVALIDDATE;
    else if (strcasecmp (kw->name, "rd") == 0 && v != 3 && v != 23)
	return -PARSE_TIME_ERR_INVALIDDATE;
    else if (strcasecmp (kw->name, "th") == 0 && !is_valid_mday (v))
	return -PARSE_TIME_ERR_INVALIDDATE;

    return set_field (state, TM_ABS_MDAY, v);
}

/*
 * Accepted keywords.
 *
 * A keyword may optionally contain a '|' to indicate the minimum
 * match length. Without one, full match is required. It's advisable
 * to keep the minimum match parts unique across all keywords.
 *
 * If keyword begins with upper case letter, then the matching will be
 * case sensitive. Otherwise the matching is case insensitive.
 *
 * If setter is NULL, set_default will be used.
 *
 * Note: Order matters. Matching is greedy, longest match is used, but
 * of equal length matches the first one is used, unless there's an
 * equal length case sensitive match which trumps case insensitive
 * matches.
 */
static struct keyword keywords[] = {
    /* Weekdays. */
    { N_("sun|day"),	TM_ABS_WDAY,	0,	NULL },
    { N_("mon|day"),	TM_ABS_WDAY,	1,	NULL },
    { N_("tue|sday"),	TM_ABS_WDAY,	2,	NULL },
    { N_("wed|nesday"),	TM_ABS_WDAY,	3,	NULL },
    { N_("thu|rsday"),	TM_ABS_WDAY,	4,	NULL },
    { N_("fri|day"),	TM_ABS_WDAY,	5,	NULL },
    { N_("sat|urday"),	TM_ABS_WDAY,	6,	NULL },

    /* Months. */
    { N_("jan|uary"),	TM_ABS_MON,	1,	kw_set_month },
    { N_("feb|ruary"),	TM_ABS_MON,	2,	kw_set_month },
    { N_("mar|ch"),	TM_ABS_MON,	3,	kw_set_month },
    { N_("apr|il"),	TM_ABS_MON,	4,	kw_set_month },
    { N_("may"),	TM_ABS_MON,	5,	kw_set_month },
    { N_("jun|e"),	TM_ABS_MON,	6,	kw_set_month },
    { N_("jul|y"),	TM_ABS_MON,	7,	kw_set_month },
    { N_("aug|ust"),	TM_ABS_MON,	8,	kw_set_month },
    { N_("sep|tember"),	TM_ABS_MON,	9,	kw_set_month },
    { N_("oct|ober"),	TM_ABS_MON,	10,	kw_set_month },
    { N_("nov|ember"),	TM_ABS_MON,	11,	kw_set_month },
    { N_("dec|ember"),	TM_ABS_MON,	12,	kw_set_month },

    /* Durations. */
    { N_("y|ears"),	TM_REL_YEAR,	1,	kw_set_rel },
    { N_("w|eeks"),	TM_REL_WEEK,	1,	kw_set_rel },
    { N_("d|ays"),	TM_REL_DAY,	1,	kw_set_rel },
    { N_("h|ours"),	TM_REL_HOUR,	1,	kw_set_rel },
    { N_("hr|s"),	TM_REL_HOUR,	1,	kw_set_rel },
    { N_("m|inutes"),	TM_REL_MIN,	1,	kw_set_rel },
    /* M=months, m=minutes */
    { N_("M"),		TM_REL_MON,	1,	kw_set_rel },
    { N_("mins"),	TM_REL_MIN,	1,	kw_set_rel },
    { N_("mo|nths"),	TM_REL_MON,	1,	kw_set_rel },
    { N_("s|econds"),	TM_REL_SEC,	1,	kw_set_rel },
    { N_("secs"),	TM_REL_SEC,	1,	kw_set_rel },

    /* Numbers. */
    { N_("one"),	TM_NONE,	1,	kw_set_number },
    { N_("two"),	TM_NONE,	2,	kw_set_number },
    { N_("three"),	TM_NONE,	3,	kw_set_number },
    { N_("four"),	TM_NONE,	4,	kw_set_number },
    { N_("five"),	TM_NONE,	5,	kw_set_number },
    { N_("six"),	TM_NONE,	6,	kw_set_number },
    { N_("seven"),	TM_NONE,	7,	kw_set_number },
    { N_("eight"),	TM_NONE,	8,	kw_set_number },
    { N_("nine"),	TM_NONE,	9,	kw_set_number },
    { N_("ten"),	TM_NONE,	10,	kw_set_number },
    { N_("dozen"),	TM_NONE,	12,	kw_set_number },
    { N_("hundred"),	TM_NONE,	100,	kw_set_number },

    /* Special number forms. */
    { N_("this"),	TM_NONE,	0,	kw_set_number },
    { N_("last"),	TM_NONE,	1,	kw_set_number },

    /* Other special keywords. */
    { N_("yesterday"),	TM_REL_DAY,	1,	kw_set_rel },
    { N_("today"),	TM_NONE,	0,	kw_set_today },
    { N_("now"),	TM_NONE,	0,	kw_set_now },
    { N_("noon"),	TM_NONE,	12,	kw_set_timeofday },
    { N_("midnight"),	TM_NONE,	0,	kw_set_timeofday },
    { N_("am"),		TM_AMPM,	0,	kw_set_ampm },
    { N_("a.m."),	TM_AMPM,	0,	kw_set_ampm },
    { N_("pm"),		TM_AMPM,	1,	kw_set_ampm },
    { N_("p.m."),	TM_AMPM,	1,	kw_set_ampm },
    { N_("st"),		TM_NONE,	0,	kw_set_ordinal },
    { N_("nd"),		TM_NONE,	0,	kw_set_ordinal },
    { N_("rd"),		TM_NONE,	0,	kw_set_ordinal },
    { N_("th"),		TM_NONE,	0,	kw_set_ordinal },

    /* Timezone codes: offset in minutes. XXX: Add more codes. */
    { N_("pst"),	TM_TZ,		-8*60,	NULL },
    { N_("mst"),	TM_TZ,		-7*60,	NULL },
    { N_("cst"),	TM_TZ,		-6*60,	NULL },
    { N_("est"),	TM_TZ,		-5*60,	NULL },
    { N_("ast"),	TM_TZ,		-4*60,	NULL },
    { N_("nst"),	TM_TZ,		-(3*60+30),	NULL },

    { N_("gmt"),	TM_TZ,		0,	NULL },
    { N_("utc"),	TM_TZ,		0,	NULL },

    { N_("wet"),	TM_TZ,		0,	NULL },
    { N_("cet"),	TM_TZ,		1*60,	NULL },
    { N_("eet"),	TM_TZ,		2*60,	NULL },
    { N_("fet"),	TM_TZ,		3*60,	NULL },

    { N_("wat"),	TM_TZ,		1*60,	NULL },
    { N_("cat"),	TM_TZ,		2*60,	NULL },
    { N_("eat"),	TM_TZ,		3*60,	NULL },
};

/*
 * Compare strings s and keyword. Return number of matching chars on
 * match, 0 for no match. Match must be at least n chars, or all of
 * keyword if n < 0, otherwise it's not a match. Use match_case for
 * case sensitive matching.
 */
static size_t
stringcmp (const char *s, const char *keyword, ssize_t n, bool match_case)
{
    ssize_t i;

    if (!n)
	return 0;

    for (i = 0; *s && *keyword; i++, s++, keyword++) {
	if (match_case) {
	    if (*s != *keyword)
		break;
	} else {
	    if (tolower ((unsigned char) *s) !=
		tolower ((unsigned char) *keyword))
		break;
	}
    }

    if (n > 0)
	return i < n ? 0 : i;
    else
	return *keyword ? 0 : i;
}

/*
 * Parse a keyword. Return < 0 on error, number of parsed chars on
 * success.
 */
static ssize_t
parse_keyword (struct state *state, const char *s)
{
    unsigned int i;
    size_t n, max_n = 0;
    struct keyword *kw = NULL;
    int r;

    /* Match longest keyword */
    for (i = 0; i < ARRAY_SIZE (keywords); i++) {
	/* Match case if keyword begins with upper case letter. */
	bool mcase = isupper ((unsigned char) keywords[i].name[0]);
	ssize_t minlen = -1;
	char keyword[128];
	char *p;

	strncpy (keyword, _(keywords[i].name), sizeof (keyword));

	/* Truncate too long keywords. XXX: Make this dynamic? */
	keyword[sizeof (keyword) - 1] = '\0';

	/* Minimum match length. */
	p = strchr (keyword, '|');
	if (p) {
	    minlen = p - keyword;

	    /* Remove the minimum match length separator. */
	    memmove (p, p + 1, strlen (p + 1) + 1);
	}

	n = stringcmp (s, keyword, minlen, mcase);
	if (n > max_n || (n == max_n && mcase)) {
	    max_n = n;
	    kw = &keywords[i];
	}
    }

    if (!kw)
	return -PARSE_TIME_ERR_KEYWORD;

    if (kw->set)
	r = kw->set (state, kw);
    else
	r = kw_set_default (state, kw);

    if (r < 0)
	return r;

    return max_n;
}

/*
 * Non-keyword parsers and their helpers.
 */

static int
set_user_tz (struct state *state, char sign, int hour, int min)
{
    int tz = hour * 60 + min;

    assert (sign == '+' || sign == '-');

    if (hour < 0 || hour > 14 || min < 0 || min > 59 || min % 15)
	return -PARSE_TIME_ERR_INVALIDTIME;

    if (sign == '-')
	tz = -tz;

    return set_field (state, TM_TZ, tz);
}

/*
 * Independent parsing of a postponed number when it wasn't consumed
 * during parsing of the following token.
 */
static int
parse_postponed_number (struct state *state, int v, int n, char d)
{
    if (n == 1 || n == 2) {
	/* Notable exception: Previous field affects parsing. This
	 * handles "January 20". */
	if (state->last_field == TM_ABS_MON) {
	    /* D[D] */
	    if (!is_valid_mday (v))
		return -PARSE_TIME_ERR_INVALIDDATE;

	    return set_field (state, TM_ABS_MDAY, v);
	} else if (n == 2) {
	    /* XXX: Only allow if last field is hour, min, or sec? */
	    if (d == '+' || d == '-') {
		/* +/-HH */
		return set_user_tz (state, d, v, 0);
	    }
	}
    } else if (n == 4) {
	/* Notable exception: Value affects parsing. Time zones are
	 * always at most 1400 and we don't understand years before
	 * 1970. */
	if (!is_valid_year (v)) {
	    if (d == '+' || d == '-') {
		/* +/-HHMM */
		return set_user_tz (state, d, v / 100, v % 100);
	    }
	} else {
	    /* YYYY */
	    return set_field (state, TM_ABS_YEAR, v);
	}
    } else if (n == 6) {
	/* HHMMSS */
	int hour = v / 10000;
	int min = (v / 100) % 100;
	int sec = v % 100;

	if (!is_valid_time (hour, min, sec))
	    return -PARSE_TIME_ERR_INVALIDTIME;

	return set_abs_time (state, hour, min, sec);
    } else if (n == 8) {
	/* YYYYMMDD */
	int year = v / 10000;
	int mon = (v / 100) % 100;
	int mday = v % 100;

	if (!is_valid_date (year, mon, mday))
	    return -PARSE_TIME_ERR_INVALIDDATE;

	return set_abs_date (state, year, mon, mday);
    } else {
	return -PARSE_TIME_ERR_FORMAT;
    }

    return -PARSE_TIME_ERR_FORMAT;
}

static int tm_get_field (const struct tm *tm, enum field field);

static int
set_timestamp (struct state *state, time_t t)
{
    struct tm tm;
    enum field f;
    int r;

    if (gmtime_r (&t, &tm) == NULL)
	return -PARSE_TIME_ERR_LIB;

    for (f = TM_ABS_SEC; f != TM_NONE; f = next_abs_field (f)) {
	r = set_field (state, f, tm_get_field (&tm, f));
	if (r)
	    return r;
    }

    r = set_field (state, TM_TZ, 0);
    if (r)
	return r;

    /* XXX: Prevent TM_AMPM with timestamp, e.g. "@123456 pm" */

    return 0;
}

/* Parse a single number. Typically postpone parsing until later. */
static int
parse_single_number (struct state *state, unsigned long v,
		     unsigned long n)
{
    assert (n);

    if (state->delim == '@')
	return set_timestamp (state, (time_t) v);

    if (v > INT_MAX)
	return -PARSE_TIME_ERR_FORMAT;

    return set_postponed_number (state, v, n);
}

static bool
is_time_sep (char c)
{
    return c == ':';
}

static bool
is_date_sep (char c)
{
    return c == '/' || c == '-' || c == '.';
}

static bool
is_sep (char c)
{
    return is_time_sep (c) || is_date_sep (c);
}

/* Two-digit year: 00...69 is 2000s, 70...99 1900s, if n == 0 keep
 * unset. */
static int
expand_year (unsigned long year, size_t n)
{
    if (n == 2) {
	return (year < 70 ? 2000 : 1900) + year;
    } else if (n == 4) {
	return year;
    } else {
	return UNSET;
    }
}

/* Parse a date number triplet. */
static int
parse_date (struct state *state, char sep,
	    unsigned long v1, unsigned long v2, unsigned long v3,
	    size_t n1, size_t n2, size_t n3)
{
    int year = UNSET, mon = UNSET, mday = UNSET;

    assert (is_date_sep (sep));

    switch (sep) {
    case '/': /* Date: M[M]/D[D][/YY[YY]] or M[M]/YYYY */
	if (n1 != 1 && n1 != 2)
	    return -PARSE_TIME_ERR_DATEFORMAT;

	if ((n2 == 1 || n2 == 2) && (n3 == 0 || n3 == 2 || n3 == 4)) {
	    /* M[M]/D[D][/YY[YY]] */
	    year = expand_year (v3, n3);
	    mon = v1;
	    mday = v2;
	} else if (n2 == 4 && n3 == 0) {
	    /* M[M]/YYYY */
	    year = v2;
	    mon = v1;
	} else {
	    return -PARSE_TIME_ERR_DATEFORMAT;
	}
	break;

    case '-': /* Date: YYYY-MM[-DD] or DD-MM[-YY[YY]] or MM-YYYY */
	if (n1 == 4 && n2 == 2 && (n3 == 0 || n3 == 2)) {
	    /* YYYY-MM[-DD] */
	    year = v1;
	    mon = v2;
	    if (n3)
		mday = v3;
	} else if (n1 == 2 && n2 == 2 && (n3 == 0 || n3 == 2 || n3 == 4)) {
	    /* DD-MM[-YY[YY]] */
	    year = expand_year (v3, n3);
	    mon = v2;
	    mday = v1;
	} else if (n1 == 2 && n2 == 4 && n3 == 0) {
	    /* MM-YYYY */
	    year = v2;
	    mon = v1;
	} else {
	    return -PARSE_TIME_ERR_DATEFORMAT;
	}
	break;

    case '.': /* Date: D[D].M[M][.[YY[YY]]] */
	if ((n1 != 1 && n1 != 2) || (n2 != 1 && n2 != 2) ||
	    (n3 != 0 && n3 != 2 && n3 != 4))
	    return -PARSE_TIME_ERR_DATEFORMAT;

	year = expand_year (v3, n3);
	mon = v2;
	mday = v1;
	break;
    }

    if (year != UNSET && !is_valid_year (year))
	return -PARSE_TIME_ERR_INVALIDDATE;

    if (mon != UNSET && !is_valid_mon (mon))
	return -PARSE_TIME_ERR_INVALIDDATE;

    if (mday != UNSET && !is_valid_mday (mday))
	return -PARSE_TIME_ERR_INVALIDDATE;

    return set_abs_date (state, year, mon, mday);
}

/* Parse a time number triplet. */
static int
parse_time (struct state *state, char sep,
	    unsigned long v1, unsigned long v2, unsigned long v3,
	    size_t n1, size_t n2, size_t n3)
{
    assert (is_time_sep (sep));

    if ((n1 != 1 && n1 != 2) || n2 != 2 || (n3 != 0 && n3 != 2))
	return -PARSE_TIME_ERR_TIMEFORMAT;

    /*
     * Notable exception: Previously set fields affect
     * parsing. Interpret (+|-)HH:MM as time zone only if hour and
     * minute have been set.
     *
     * XXX: This could be fixed by restricting the delimiters
     * preceding time. For '+' it would be justified, but for '-' it
     * might be inconvenient. However prefer to allow '-' as an
     * insignificant delimiter preceding time for convenience, and
     * handle '+' the same way for consistency between positive and
     * negative time zones.
     */
    if (is_field_set (state, TM_ABS_HOUR) &&
	is_field_set (state, TM_ABS_MIN) &&
	n1 == 2 && n2 == 2 && n3 == 0 &&
	(state->delim == '+' || state->delim == '-')) {
	return set_user_tz (state, state->delim, v1, v2);
    }

    if (!is_valid_time (v1, v2, v3))
	return -PARSE_TIME_ERR_INVALIDTIME;

    return set_abs_time (state, v1, v2, n3 ? v3 : 0);
}

/* strtoul helper that assigns length. */
static unsigned long
strtoul_len (const char *s, const char **endp, size_t *len)
{
    unsigned long val = strtoul (s, (char **) endp, 10);

    *len = *endp - s;
    return val;
}

/*
 * Parse a (group of) number(s). Return < 0 on error, number of parsed
 * chars on success.
 */
static ssize_t
parse_number (struct state *state, const char *s)
{
    int r;
    unsigned long v1, v2, v3 = 0;
    size_t n1, n2, n3 = 0;
    const char *p = s;
    char sep;

    v1 = strtoul_len (p, &p, &n1);

    if (is_sep (*p) && isdigit ((unsigned char) *(p + 1))) {
	sep = *p;
	v2 = strtoul_len (p + 1, &p, &n2);
    } else {
	/* A single number. */
	r = parse_single_number (state, v1, n1);
	if (r)
	    return r;

	return p - s;
    }

    /* A group of two or three numbers? */
    if (*p == sep && isdigit ((unsigned char) *(p + 1)))
	v3 = strtoul_len (p + 1, &p, &n3);

    if (is_time_sep (sep))
	r = parse_time (state, sep, v1, v2, v3, n1, n2, n3);
    else
	r = parse_date (state, sep, v1, v2, v3, n1, n2, n3);

    if (r)
	return r;

    return p - s;
}

/*
 * Parse delimiter(s). Throw away all except the last one, which is
 * stored for parsing the next non-delimiter. Return < 0 on error,
 * number of parsed chars on success.
 *
 * XXX: We might want to be more strict here.
 */
static ssize_t
parse_delim (struct state *state, const char *s)
{
    const char *p = s;

    /*
     * Skip non-alpha and non-digit, and store the last for further
     * processing.
     */
    while (*p && !isalnum ((unsigned char) *p)) {
	set_delim (state, *p);
	p++;
    }

    return p - s;
}

/*
 * Parse a date/time string. Return < 0 on error, number of parsed
 * chars on success.
 */
static ssize_t
parse_input (struct state *state, const char *s)
{
    const char *p = s;
    ssize_t n;
    int r;

    while (*p) {
	if (isalpha ((unsigned char) *p)) {
	    n = parse_keyword (state, p);
	} else if (isdigit ((unsigned char) *p)) {
	    n = parse_number (state, p);
	} else {
	    n = parse_delim (state, p);
	}

	if (n <= 0) {
	    if (n == 0)
		n = -PARSE_TIME_ERR;

	    return n;
	}

	p += n;
    }

    /* Parse postponed number, if any. */
    r = handle_postponed_number (state, TM_NONE);
    if (r < 0)
	return r;

    return p - s;
}

/*
 * Processing the parsed input.
 */

/*
 * Initialize reference time to tm. Use time zone in state if
 * specified, otherwise local time. Use now for reference time if
 * non-NULL, otherwise current time.
 */
static int
initialize_now (struct state *state, struct tm *tm, const time_t *now)
{
    time_t t;

    if (now) {
	t = *now;
    } else {
	if (time (&t) == (time_t) -1)
	    return -PARSE_TIME_ERR_LIB;
    }

    if (is_field_set (state, TM_TZ)) {
	/* Some other time zone. */

	/* Adjust now according to the TZ. */
	t += get_field (state, TM_TZ) * 60;

	/* It's not gm, but this doesn't mess with the TZ. */
	if (gmtime_r (&t, tm) == NULL)
	    return -PARSE_TIME_ERR_LIB;
    } else {
	/* Local time. */
	if (localtime_r (&t, tm) == NULL)
	    return -PARSE_TIME_ERR_LIB;
    }

    return 0;
}

/*
 * Normalize tm according to mktime(3). Both mktime(3) and
 * localtime_r(3) use local time, but they cancel each other out here,
 * making this function agnostic to time zone.
 */
static int
normalize_tm (struct tm *tm)
{
    time_t t = mktime (tm);

    if (t == (time_t) -1)
	return -PARSE_TIME_ERR_LIB;

    if (!localtime_r (&t, tm))
	return -PARSE_TIME_ERR_LIB;

    return 0;
}

/* Get field out of a struct tm. */
static int
tm_get_field (const struct tm *tm, enum field field)
{
    switch (field) {
    case TM_ABS_SEC:	return tm->tm_sec;
    case TM_ABS_MIN:	return tm->tm_min;
    case TM_ABS_HOUR:	return tm->tm_hour;
    case TM_ABS_MDAY:	return tm->tm_mday;
    case TM_ABS_MON:	return tm->tm_mon + 1; /* 0- to 1-based */
    case TM_ABS_YEAR:	return 1900 + tm->tm_year;
    case TM_ABS_WDAY:	return tm->tm_wday;
    case TM_ABS_ISDST:	return tm->tm_isdst;
    default:
	assert (false);
	break;
    }

    return 0;
}

/* Modify hour according to am/pm setting. */
static int
fixup_ampm (struct state *state)
{
    int hour, hdiff = 0;

    if (!is_field_set (state, TM_AMPM))
	return 0;

    if (!is_field_set (state, TM_ABS_HOUR))
	return -PARSE_TIME_ERR_TIMEFORMAT;

    hour = get_field (state, TM_ABS_HOUR);
    if (!is_valid_12hour (hour))
	return -PARSE_TIME_ERR_INVALIDTIME;

    if (get_field (state, TM_AMPM)) {
	/* 12pm is noon. */
	if (hour != 12)
	    hdiff = 12;
    } else {
	/* 12am is midnight, beginning of day. */
	if (hour == 12)
	    hdiff = -12;
    }

    mod_field (state, TM_REL_HOUR, -hdiff);

    return 0;
}

/* Combine absolute and relative fields, and round. */
static int
create_output (struct state *state, time_t *t_out, const time_t *ref,
	       int round)
{
    struct tm tm = { .tm_isdst = -1 };
    struct tm now;
    time_t t;
    enum field f;
    int r;
    int week_round = PARSE_TIME_NO_ROUND;

    r = initialize_now (state, &now, ref);
    if (r)
	return r;

    /* Initialize fields flagged as "now" to reference time. */
    for (f = TM_ABS_SEC; f != TM_NONE; f = next_abs_field (f)) {
	if (state->set[f] == FIELD_NOW) {
	    state->tm[f] = tm_get_field (&now, f);
	    state->set[f] = FIELD_SET;
	}
    }

    /*
     * If WDAY is set but MDAY is not, we consider WDAY relative
     *
     * XXX: This fails on stuff like "two months monday" because two
     * months ago wasn't the same day as today. Postpone until we know
     * date?
     */
    if (is_field_set (state, TM_ABS_WDAY) &&
	!is_field_set (state, TM_ABS_MDAY)) {
	int wday = get_field (state, TM_ABS_WDAY);
	int today = tm_get_field (&now, TM_ABS_WDAY);
	int rel_days;

	if (today > wday)
	    rel_days = today - wday;
	else
	    rel_days = today + 7 - wday;

	/* This also prevents special week rounding from happening. */
	mod_field (state, TM_REL_DAY, rel_days);

	unset_field (state, TM_ABS_WDAY);
    }

    r = fixup_ampm (state);
    if (r)
	return r;

    /*
     * Iterate fields from most accurate to least accurate, and set
     * unset fields according to requested rounding.
     */
    for (f = TM_ABS_SEC; f != TM_NONE; f = next_abs_field (f)) {
	if (round != PARSE_TIME_NO_ROUND) {
	    enum field r = abs_to_rel_field (f);

	    if (is_field_set (state, f) || is_field_set (state, r)) {
		if (round >= PARSE_TIME_ROUND_UP && f != TM_ABS_SEC) {
		    mod_field (state, r, -1);
		    if (round == PARSE_TIME_ROUND_UP_INCLUSIVE)
			mod_field (state, TM_REL_SEC, 1);
		}
		round = PARSE_TIME_NO_ROUND; /* No more rounding. */
	    } else {
		if (f == TM_ABS_MDAY &&
		    is_field_set (state, TM_REL_WEEK)) {
		    /* Week is most accurate. */
		    week_round = round;
		    round = PARSE_TIME_NO_ROUND;
		} else {
		    set_field (state, f, field_epoch (f));
		}
	    }
	}

	if (!is_field_set (state, f))
	    set_field (state, f, tm_get_field (&now, f));
    }

    /* Special case: rounding with week accuracy. */
    if (week_round != PARSE_TIME_NO_ROUND) {
	/* Temporarily set more accurate fields to now. */
	set_field (state, TM_ABS_SEC, tm_get_field (&now, TM_ABS_SEC));
	set_field (state, TM_ABS_MIN, tm_get_field (&now, TM_ABS_MIN));
	set_field (state, TM_ABS_HOUR, tm_get_field (&now, TM_ABS_HOUR));
	set_field (state, TM_ABS_MDAY, tm_get_field (&now, TM_ABS_MDAY));
    }

    /*
     * Set all fields. They may contain out of range values before
     * normalization by mktime(3).
     */
    tm.tm_sec = get_field (state, TM_ABS_SEC) - get_field (state, TM_REL_SEC);
    tm.tm_min = get_field (state, TM_ABS_MIN) - get_field (state, TM_REL_MIN);
    tm.tm_hour = get_field (state, TM_ABS_HOUR) - get_field (state, TM_REL_HOUR);
    tm.tm_mday = get_field (state, TM_ABS_MDAY) -
		 get_field (state, TM_REL_DAY) - 7 * get_field (state, TM_REL_WEEK);
    tm.tm_mon = get_field (state, TM_ABS_MON) - get_field (state, TM_REL_MON);
    tm.tm_mon--; /* 1- to 0-based */
    tm.tm_year = get_field (state, TM_ABS_YEAR) - get_field (state, TM_REL_YEAR) - 1900;

    /*
     * It's always normal time.
     *
     * XXX: This is probably not a solution that universally
     * works. Just make sure DST is not taken into account. We don't
     * want rounding to be affected by DST.
     */
    tm.tm_isdst = -1;

    /* Special case: rounding with week accuracy. */
    if (week_round != PARSE_TIME_NO_ROUND) {
	/* Normalize to get proper tm.wday. */
	r = normalize_tm (&tm);
	if (r < 0)
	    return r;

	/* Set more accurate fields back to zero. */
	tm.tm_sec = 0;
	tm.tm_min = 0;
	tm.tm_hour = 0;
	tm.tm_isdst = -1;

	/* Monday is the true 1st day of week, but this is easier. */
	if (week_round >= PARSE_TIME_ROUND_UP) {
	    tm.tm_mday += 7 - tm.tm_wday;
	    if (week_round == PARSE_TIME_ROUND_UP_INCLUSIVE)
		tm.tm_sec--;
	} else {
	    tm.tm_mday -= tm.tm_wday;
	}
    }

    if (is_field_set (state, TM_TZ)) {
	/* tm is in specified TZ, convert to UTC for timegm(3). */
	tm.tm_min -= get_field (state, TM_TZ);
	t = timegm (&tm);
    } else {
	/* tm is in local time. */
	t = mktime (&tm);
    }

    if (t == (time_t) -1)
	return -PARSE_TIME_ERR_LIB;

    *t_out = t;

    return 0;
}

/* Internally, all errors are < 0. parse_time_string() returns errors > 0. */
#define EXTERNAL_ERR(r) (-r)

int
parse_time_string (const char *s, time_t *t, const time_t *ref, int round)
{
    struct state state = { .last_field = TM_NONE };
    int r;

    if (!s || !t)
	return EXTERNAL_ERR (-PARSE_TIME_ERR);

    r = parse_input (&state, s);
    if (r < 0)
	return EXTERNAL_ERR (r);

    r = create_output (&state, t, ref, round);
    if (r < 0)
	return EXTERNAL_ERR (r);

    return 0;
}

debug log:

solving 861a705 ...
found 861a705 in https://yhetil.org/notmuch/0296be2a3899653549b3f95e1aa1a4a0632e92e7.1350164594.git.jani@nikula.org/

applying [1/1] https://yhetil.org/notmuch/0296be2a3899653549b3f95e1aa1a4a0632e92e7.1350164594.git.jani@nikula.org/
diff --git a/parse-time-string/parse-time-string.c b/parse-time-string/parse-time-string.c
new file mode 100644
index 0000000..861a705

Checking patch parse-time-string/parse-time-string.c...
Applied patch parse-time-string/parse-time-string.c cleanly.

index at:
100644 861a70506acad209d5d5676100e34285b618e062	parse-time-string/parse-time-string.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).