summaryrefslogtreecommitdiffstats
path: root/pendulum/parsing/_iso8601.c
blob: 1322423ac22190e99fa9d8bed756773475cd487b (plain)
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
/* ------------------------------------------------------------------------- */

#include <Python.h>
#include <datetime.h>
#include <structmember.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
#endif


/* ------------------------------------------------------------------------- */

#define EPOCH_YEAR 1970

#define DAYS_PER_N_YEAR 365
#define DAYS_PER_L_YEAR 366

#define USECS_PER_SEC 1000000

#define SECS_PER_MIN 60
#define SECS_PER_HOUR (60 * SECS_PER_MIN)
#define SECS_PER_DAY (SECS_PER_HOUR * 24)

// 400-year chunks always have 146097 days (20871 weeks).
#define DAYS_PER_400_YEARS 146097L
#define SECS_PER_400_YEARS ((int64_t)DAYS_PER_400_YEARS * (int64_t)SECS_PER_DAY)

// The number of seconds in an aligned 100-year chunk, for those that
// do not begin with a leap year and those that do respectively.
const int64_t SECS_PER_100_YEARS[2] = {
    (uint64_t)(76L * DAYS_PER_N_YEAR + 24L * DAYS_PER_L_YEAR) * SECS_PER_DAY,
    (uint64_t)(75L * DAYS_PER_N_YEAR + 25L * DAYS_PER_L_YEAR) * SECS_PER_DAY
};

// The number of seconds in an aligned 4-year chunk, for those that
// do not begin with a leap year and those that do respectively.
const int32_t SECS_PER_4_YEARS[2] = {
    (4 * DAYS_PER_N_YEAR + 0 * DAYS_PER_L_YEAR) * SECS_PER_DAY,
    (3 * DAYS_PER_N_YEAR + 1 * DAYS_PER_L_YEAR) * SECS_PER_DAY
};

// The number of seconds in non-leap and leap years respectively.
const int32_t SECS_PER_YEAR[2] = {
    DAYS_PER_N_YEAR * SECS_PER_DAY,
    DAYS_PER_L_YEAR * SECS_PER_DAY
};

#define MONTHS_PER_YEAR 12

// The month lengths in non-leap and leap years respectively.
const int32_t DAYS_PER_MONTHS[2][13] = {
    {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

// The day offsets of the beginning of each (1-based) month in non-leap
// and leap years respectively.
// For example, in a leap year there are 335 days before December.
const int32_t MONTHS_OFFSETS[2][14] = {
    {-1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
    {-1, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
};

const int DAY_OF_WEEK_TABLE[12] = {
    0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4
};

#define TM_SUNDAY 0
#define TM_MONDAY 1
#define TM_TUESDAY 2
#define TM_WEDNESDAY 3
#define TM_THURSDAY 4
#define TM_FRIDAY 5
#define TM_SATURDAY 6

#define TM_JANUARY 0
#define TM_FEBRUARY 1
#define TM_MARCH 2
#define TM_APRIL 3
#define TM_MAY 4
#define TM_JUNE 5
#define TM_JULY 6
#define TM_AUGUST 7
#define TM_SEPTEMBER 8
#define TM_OCTOBER 9
#define TM_NOVEMBER 10
#define TM_DECEMBER 11

// Parsing errors
const int PARSER_INVALID_ISO8601 = 0;
const int PARSER_INVALID_DATE = 1;
const int PARSER_INVALID_TIME = 2;
const int PARSER_INVALID_WEEK_DATE = 3;
const int PARSER_INVALID_WEEK_NUMBER = 4;
const int PARSER_INVALID_WEEKDAY_NUMBER = 5;
const int PARSER_INVALID_ORDINAL_DAY_FOR_YEAR = 6;
const int PARSER_INVALID_MONTH_OR_DAY = 7;
const int PARSER_INVALID_MONTH = 8;
const int PARSER_INVALID_DAY_FOR_MONTH = 9;
const int PARSER_INVALID_HOUR = 10;
const int PARSER_INVALID_MINUTE = 11;
const int PARSER_INVALID_SECOND = 12;
const int PARSER_INVALID_SUBSECOND = 13;
const int PARSER_INVALID_TZ_OFFSET = 14;
const int PARSER_INVALID_DURATION = 15;
const int PARSER_INVALID_DURATION_FLOAT_YEAR_MONTH_NOT_SUPPORTED = 16;

const char PARSER_ERRORS[17][80] = {
    "Invalid ISO 8601 string",
    "Invalid date",
    "Invalid time",
    "Invalid week date",
    "Invalid week number",
    "Invalid weekday number",
    "Invalid ordinal day for year",
    "Invalid month and/or day",
    "Invalid month",
    "Invalid day for month",
    "Invalid hour",
    "Invalid minute",
    "Invalid second",
    "Invalid subsecond",
    "Invalid timezone offset",
    "Invalid duration",
    "Float years and months are not supported"
};

/* ------------------------------------------------------------------------- */


int p(int y) {
    return y + y/4 - y/100 + y/400;
}

int is_leap(int year) {
    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

int week_day(int year, int month, int day) {
    int y;
    int w;

    y = year - (month < 3);

    w = (p(y) + DAY_OF_WEEK_TABLE[month - 1] + day) % 7;

    if (!w) {
        w = 7;
    }

    return w;
}

int days_in_year(int year) {
    if (is_leap(year)) {
        return DAYS_PER_L_YEAR;
    }

    return DAYS_PER_N_YEAR;
}

int is_long_year(int year) {
    return (p(year) % 7 == 4) || (p(year - 1) % 7 == 3);
}


/* ------------------------ Custom Types ------------------------------- */


/*
 * class FixedOffset(tzinfo):
 */
typedef struct {
    PyObject_HEAD
    int offset;
    char *tzname;
} FixedOffset;

/*
 * def __init__(self, offset):
 *     self.offset = offset
*/
static int FixedOffset_init(FixedOffset *self, PyObject *args, PyObject *kwargs) {
    int offset;
    char *tzname = NULL;

    static char *kwlist[] = {"offset", "tzname", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s", kwlist, &offset, &tzname))
        return -1;

    self->offset = offset;
    self->tzname = tzname;

    return 0;
}

/*
 * def utcoffset(self, dt):
 *     return timedelta(seconds=self.offset * 60)
 */
static PyObject *FixedOffset_utcoffset(FixedOffset *self, PyObject *args) {
    return PyDelta_FromDSU(0, self->offset, 0);
}

/*
 * def dst(self, dt):
 *     return timedelta(seconds=self.offset * 60)
 */
static PyObject *FixedOffset_dst(FixedOffset *self, PyObject *args) {
    return PyDelta_FromDSU(0, self->offset, 0);
}

/*
 * def tzname(self, dt):
 *     sign = '+'
 *     if self.offset < 0:
 *         sign = '-'
 *     return f"{sign}{self.offset / 60}:{self.offset % 60}"
 */
static PyObject *FixedOffset_tzname(FixedOffset *self, PyObject *args) {
    if (self->tzname != NULL) {
        return PyUnicode_FromString(self->tzname);
    }

    char sign = '+';
    int offset = self->offset;

    if (offset < 0) {
        sign = '-';
        offset *= -1;
    }

    return PyUnicode_FromFormat(
        "%c%02d:%02d",
        sign,
        offset / SECS_PER_HOUR,
        offset / SECS_PER_MIN % SECS_PER_MIN
    );
}

/*
 * def __repr__(self):
 *     return self.tzname()
 */
static PyObject *FixedOffset_repr(FixedOffset *self) {
    return FixedOffset_tzname(self, NULL);
}

/*
 * Class member / class attributes
 */
static PyMemberDef FixedOffset_members[] = {
    {"offset", T_INT, offsetof(FixedOffset, offset), 0, "UTC offset"},
    {NULL}
};

/*
 * Class methods
 */
static PyMethodDef FixedOffset_methods[] = {
    {"utcoffset", (PyCFunction)FixedOffset_utcoffset, METH_VARARGS, ""},
    {"dst",       (PyCFunction)FixedOffset_dst,       METH_VARARGS, ""},
    {"tzname",    (PyCFunction)FixedOffset_tzname,    METH_VARARGS, ""},
    {NULL}
};

static PyTypeObject FixedOffset_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "FixedOffset_type",             /* tp_name */
    sizeof(FixedOffset),                    /* tp_basicsize */
    0,                                      /* tp_itemsize */
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_as_async */
    (reprfunc)FixedOffset_repr,             /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash  */
    0,                                      /* tp_call */
    (reprfunc)FixedOffset_repr,             /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
    "TZInfo with fixed offset",             /* tp_doc */
};

/*
 * Instantiate new FixedOffset_type object
 * Skip overhead of calling PyObject_New and PyObject_Init.
 * Directly allocate object.
 */
static PyObject *new_fixed_offset_ex(int offset, char *name, PyTypeObject *type) {
    FixedOffset *self = (FixedOffset *) (type->tp_alloc(type, 0));

    if (self != NULL) {
        self->offset = offset;
        self->tzname = name;
    }

    return (PyObject *) self;
}

#define new_fixed_offset(offset, name) new_fixed_offset_ex(offset, name, &FixedOffset_type)


/*
 * class Duration():
 */
typedef struct {
    PyObject_HEAD
    int years;
    int months;
    int weeks;
    int days;
    int hours;
    int minutes;
    int seconds;
    int microseconds;
} Duration;

/*
 * def __init__(self, years, months, days, hours, minutes, seconds, microseconds):
 *     self.years = years
 *     # ...
*/
static int Duration_init(Duration *self, PyObject *args, PyObject *kwargs) {
    int years;
    int months;
    int weeks;
    int days;
    int hours;
    int minutes;
    int seconds;
    int microseconds;

    if (!PyArg_ParseTuple(args, "iiiiiiii", &years, &months, &weeks, &days, &hours, &minutes, &seconds, &microseconds))
        return -1;

    self->years = years;
    self->months = months;
    self->weeks = weeks;
    self->days = days;
    self->hours = hours;
    self->minutes = minutes;
    self->seconds = seconds;
    self->microseconds = microseconds;

    return 0;
}

/*
 * def __repr__(self):
 *     return '{} years {} months {} days {} hours {} minutes {} seconds {} microseconds'.format(
 *         self.years, self.months, self.days, self.minutes, self.hours, self.seconds, self.microseconds
 *     )
 */
static PyObject *Duration_repr(Duration *self) {
    return PyUnicode_FromFormat(
        "%d years %d months %d weeks %d days %d hours %d minutes %d seconds %d microseconds",
        self->years,
        self->months,
        self->weeks,
        self->days,
        self->hours,
        self->minutes,
        self->seconds,
        self->microseconds
    );
}

/*
 * Instantiate new Duration_type object
 * Skip overhead of calling PyObject_New and PyObject_Init.
 * Directly allocate object.
 */
static PyObject *new_duration_ex(int years, int months, int weeks, int days, int hours, int minutes, int seconds, int microseconds, PyTypeObject *type) {
    Duration *self = (Duration *) (type->tp_alloc(type, 0));

    if (self != NULL) {
        self->years = years;
        self->months = months;
        self->weeks = weeks;
        self->days = days;
        self->hours = hours;
        self->minutes = minutes;
        self->seconds = seconds;
        self->microseconds = microseconds;
    }

    return (PyObject *) self;
}

/*
 * Class member / class attributes
 */
static PyMemberDef Duration_members[] = {
    {"years", T_INT, offsetof(Duration, years), 0, "years in duration"},
    {"months", T_INT, offsetof(Duration, months), 0, "months in duration"},
    {"weeks", T_INT, offsetof(Duration, weeks), 0, "weeks in duration"},
    {"days", T_INT, offsetof(Duration, days), 0, "days in duration"},
    {"remaining_days", T_INT, offsetof(Duration, days), 0, "days in duration"},
    {"hours", T_INT, offsetof(Duration, hours), 0, "hours in duration"},
    {"minutes", T_INT, offsetof(Duration, minutes), 0, "minutes in duration"},
    {"seconds", T_INT, offsetof(Duration, seconds), 0, "seconds in duration"},
    {"remaining_seconds", T_INT, offsetof(Duration, seconds), 0, "seconds in duration"},
    {"microseconds", T_INT, offsetof(Duration, microseconds), 0, "microseconds in duration"},
    {NULL}
};

static PyTypeObject Duration_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "Duration",                             /* tp_name */
    sizeof(Duration),                       /* tp_basicsize */
    0,                                      /* tp_itemsize */
    0,                                      /* tp_dealloc */
    0,                                      /* tp_print */
    0,                                      /* tp_getattr */
    0,                                      /* tp_setattr */
    0,                                      /* tp_as_async */
    (reprfunc)Duration_repr,                /* tp_repr */
    0,                                      /* tp_as_number */
    0,                                      /* tp_as_sequence */
    0,                                      /* tp_as_mapping */
    0,                                      /* tp_hash  */
    0,                                      /* tp_call */
    (reprfunc)Duration_repr,                /* tp_str */
    0,                                      /* tp_getattro */
    0,                                      /* tp_setattro */
    0,                                      /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
    "Duration",                             /* tp_doc */
};

#define new_duration(years, months, weeks, days, hours, minutes, seconds, microseconds) new_duration_ex(years, months, weeks, days, hours, minutes, seconds, microseconds, &Duration_type)

typedef struct {
    int is_date;
    int is_time;
    int is_datetime;
    int is_duration;
    int is_period;
    int ambiguous;
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
    int microsecond;
    int offset;
    int has_offset;
    char *tzname;
    int years;
    int months;
    int weeks;
    int days;
    int hours;
    int minutes;
    int seconds;
    int microseconds;
    int error;
} Parsed;


Parsed* new_parsed() {
    Parsed *parsed;

    if((parsed = malloc(sizeof *parsed)) != NULL) {
        parsed->is_date = 0;
        parsed->is_time = 0;
        parsed->is_datetime = 0;
        parsed->is_duration = 0;
        parsed->is_period = 0;

        parsed->ambiguous = 0;
        parsed->year = 0;
        parsed->month = 1;
        parsed->day = 1;
        parsed->hour = 0;
        parsed->minute = 0;
        parsed->second = 0;
        parsed->microsecond = 0;
        parsed->offset = 0;
        parsed->has_offset = 0;
        parsed->tzname = NULL;

        parsed->years = 0;
        parsed->months = 0;
        parsed->weeks = 0;
        parsed->days = 0;
        parsed->hours = 0;
        parsed->minutes = 0;
        parsed->seconds = 0;
        parsed->microseconds = 0;

        parsed->error = -1;
    }

    return parsed;
}


/* -------------------------- Functions --------------------------*/

Parsed* _parse_iso8601_datetime(char *str, Parsed *parsed) {
    char* c;
    int monthday = 0;
    int week = 0;
    int weekday = 1;
    int ordinal;
    int tz_sign = 0;
    int leap = 0;
    int separators = 0;
    int time = 0;
    int i;
    int j;

    // Assuming date only for now
    parsed->is_date = 1;

    c = str;

    for (i = 0; i < 4; i++) {
        if (*c >= '0' && *c <= '9') {
            parsed->year = 10 * parsed->year + *c++ - '0';
        } else {
            parsed->error = PARSER_INVALID_ISO8601;

            return NULL;
        }
    }

    leap = is_leap(parsed->year);

    // Optional separator
    if (*c == '-') {
        separators++;
        c++;
    }

    // Checking for week dates
    if (*c == 'W') {
        c++;

        i = 0;
        while (*c != '\0' && *c != ' ' && *c != 'T') {
            if (*c == '-') {
                separators++;
                c++;
                continue;
            }

            week = 10 * week + *c++ - '0';

            i++;
        }

        switch (i) {
            case 2:
                // Only week number
                break;
            case 3:
                // Week with weekday
                if (!(separators == 0 || separators == 2)) {
                    // We should have 2 or no separator
                    parsed->error = PARSER_INVALID_WEEK_DATE;

                    return NULL;
                }

                weekday = week % 10;
                week /= 10;

                break;
            default:
                // Any other case is wrong
                parsed->error = PARSER_INVALID_WEEK_DATE;

                return NULL;
        }

        // Checks
        if (week > 53 || (week > 52 && !is_long_year(parsed->year))) {
            parsed->error = PARSER_INVALID_WEEK_NUMBER;

            return NULL;
        }

        if (weekday > 7) {
            parsed->error = PARSER_INVALID_WEEKDAY_NUMBER;

            return NULL;
        }

        // Calculating ordinal day
        ordinal = week * 7 + weekday - (week_day(parsed->year, 1, 4) + 3);

        if (ordinal < 1) {
            // Previous year
            ordinal += days_in_year(parsed->year - 1);
            parsed->year -= 1;
            leap = is_leap(parsed->year);
        }

        if (ordinal > days_in_year(parsed->year)) {
            // Next year
            ordinal -= days_in_year(parsed->year);
            parsed->year += 1;
            leap = is_leap(parsed->year);
        }

        for (j = 1; j < 14; j++) {
            if (ordinal <= MONTHS_OFFSETS[leap][j]) {
                parsed->day = ordinal - MONTHS_OFFSETS[leap][j - 1];
                parsed->month = j - 1;

                break;
            }
        }
    } else {
        // At this point we need to check the number
        // of characters until the end of the date part
        // (or the end of the string).
        //
        // If two, we have only a month if there is a separator, it may be a time otherwise.
        // If three, we have an ordinal date.
        // If four, we have a complete date
        i = 0;
        while (*c != '\0' && *c != ' ' && *c != 'T') {
            if (*c == '-') {
                separators++;
                c++;
                continue;
            }

            if (!(*c >= '0' && *c <='9')) {
                parsed->error = PARSER_INVALID_DATE;

                return NULL;
            }

            monthday = 10 * monthday + *c++ - '0';

            i++;
        }

        switch (i) {
            case 0:
                // No month/day specified (only a year)
                break;
            case 2:
                if (!separators) {
                    // The date looks like 201207
                    // which is invalid for a date
                    // But it might be a time in the form hhmmss
                    parsed->ambiguous = 1;
                } else if (separators > 1) {
                    parsed->error = PARSER_INVALID_DATE;

                    return NULL;
                }

                parsed->month = monthday;
                break;
            case 3:
                // Ordinal day
                if (separators > 1) {
                    parsed->error = PARSER_INVALID_DATE;

                    return NULL;
                }

                if (monthday < 1 || monthday > MONTHS_OFFSETS[leap][13]) {
                    parsed->error = PARSER_INVALID_ORDINAL_DAY_FOR_YEAR;

                    return NULL;
                }

                for (j = 1; j < 14; j++) {
                    if (monthday <= MONTHS_OFFSETS[leap][j]) {
                        parsed->day = monthday - MONTHS_OFFSETS[leap][j - 1];
                        parsed->month = j - 1;

                        break;
                    }
                }

                break;
            case 4:
                // Month and day
                parsed->month = monthday / 100;
                parsed->day = monthday % 100;

                break;
            default:
                parsed->error = PARSER_INVALID_MONTH_OR_DAY;

                return NULL;
        }
    }

    // Checks
    if (separators && !monthday && !week) {
        parsed->error = PARSER_INVALID_DATE;

        return NULL;
    }

    if (parsed->month > 12) {
        parsed->error = PARSER_INVALID_MONTH;

        return NULL;
    }

    if (parsed->day > DAYS_PER_MONTHS[leap][parsed->month]) {
        parsed->error = PARSER_INVALID_DAY_FOR_MONTH;

        return NULL;
    }

    separators = 0;
    if (*c == 'T' || *c == ' ') {
        if (parsed->ambiguous) {
            parsed->error = PARSER_INVALID_DATE;

            return NULL;
        }

        // We have time so we have a datetime
        parsed->is_datetime = 1;
        parsed->is_date = 0;

        c++;

        // Grabbing time information
        i = 0;
        while (*c != '\0' && *c != '.' && *c != ',' && *c != 'Z' && *c != '+' && *c != '-') {
            if (*c == ':') {
                separators++;
                c++;
                continue;
            }

            if (!(*c >= '0' && *c <='9')) {
                parsed->error = PARSER_INVALID_TIME;

                return NULL;
            }

            time = 10 * time + *c++ - '0';
            i++;
        }

        switch (i) {
            case 2:
                // Hours only
                if (separators > 0) {
                    // Extraneous separators
                    parsed->error = PARSER_INVALID_TIME;

                    return NULL;
                }

                parsed->hour = time;
                break;
            case 4:
                // Hours and minutes
                if (separators > 1) {
                    // Extraneous separators
                    parsed->error = PARSER_INVALID_TIME;

                    return NULL;
                }

                parsed->hour = time / 100;
                parsed->minute = time % 100;
                break;
            case 6:
                // Hours, minutes and seconds
                if (!(separators == 0 || separators == 2)) {
                    // We should have either two separators or none
                    parsed->error = PARSER_INVALID_TIME;

                    return NULL;
                }

                parsed->hour = time / 10000;
                parsed->minute = time / 100 % 100;
                parsed->second = time % 100;
                break;
            default:
                // Any other case is wrong
                parsed->error = PARSER_INVALID_TIME;

                return NULL;
        }

        // Checks
        if (parsed->hour > 23) {
            parsed->error = PARSER_INVALID_HOUR;

            return NULL;
        }

        if (parsed->minute > 59) {
            parsed->error = PARSER_INVALID_MINUTE;

            return NULL;
        }

        if (parsed->second > 59) {
            parsed->error = PARSER_INVALID_SECOND;

            return NULL;
        }

        // Subsecond
        if (*c == '.' || *c == ',') {
            c++;

            time = 0;
            i = 0;
            while (*c != '\0' && *c != 'Z' && *c != '+' && *c != '-') {
                if (!(*c >= '0' && *c <='9')) {
                    parsed->error = PARSER_INVALID_SUBSECOND;

                    return NULL;
                }

                time = 10 * time + *c++ - '0';
                i++;
            }

            // adjust to microseconds
            if (i > 6) {
                parsed->microsecond = time / pow(10, i - 6);
            } else if (i <= 6) {
                parsed->microsecond = time * pow(10, 6 - i);
            }
        }

        // Timezone
        if (*c == 'Z') {
            parsed->has_offset = 1;
            parsed->tzname = "UTC";
            c++;
        } else if (*c == '+' || *c == '-') {
            tz_sign = 1;
            if (*c == '-') {
                tz_sign = -1;
            }

            parsed->has_offset = 1;
            c++;

            i = 0;
            time = 0;
            separators = 0;
            while (*c != '\0') {
                if (*c == ':') {
                    separators++;
                    c++;
                    continue;
                }

                if (!(*c >= '0' && *c <= '9')) {
                    parsed->error = PARSER_INVALID_TZ_OFFSET;

                    return NULL;
                }

                time = 10 * time + *c++ - '0';
                i++;
            }

            switch (i) {
                case 2:
                    // hh Format
                    if (separators) {
                        // Extraneous separators
                        parsed->error = PARSER_INVALID_TZ_OFFSET;

                        return NULL;
                    }

                    parsed->offset = tz_sign * (time * 3600);
                    break;
                case 4:
                    // hhmm Format
                    if (separators > 1) {
                        // Extraneous separators
                        parsed->error = PARSER_INVALID_TZ_OFFSET;

                        return NULL;
                    }

                    parsed->offset = tz_sign * ((time / 100 * 3600) + (time % 100 * 60));
                    break;
                default:
                    // Wrong format
                    parsed->error = PARSER_INVALID_TZ_OFFSET;

                    return NULL;
            }
        }
    }

    // At this point we should be at the end of the string
    // If not, the string is invalid
    if (*c != '\0') {
        parsed->error = PARSER_INVALID_ISO8601;

        return NULL;
    }

    return parsed;
}


Parsed* _parse_iso8601_duration(char *str, Parsed *parsed) {
    char* c;
    int value = 0;
    int grabbed = 0;
    int in_time = 0;
    int in_fraction = 0;
    int fraction_length = 0;
    int has_fractional = 0;
    int fraction = 0;
    int has_ymd = 0;
    int has_week = 0;
    int has_month = 0;
    int has_day = 0;
    int has_hour = 0;
    int has_minute = 0;
    int has_second = 0;

    c = str;

    // Removing P operator
    c++;

    parsed->is_duration = 1;

    for (; *c != '\0'; c++) {
        switch (*c) {
            case 'Y':
                if (!grabbed || in_time || has_week || has_ymd) {
                    // No value grabbed
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                if (fraction) {
                    parsed->error = PARSER_INVALID_DURATION_FLOAT_YEAR_MONTH_NOT_SUPPORTED;

                    return NULL;
                }

                parsed->years = value;

                grabbed = 0;
                value = 0;
                fraction = 0;
                in_fraction = 0;
                has_ymd = 1;

                break;
            case 'M':
                if (!grabbed || has_week) {
                    // No value grabbed
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                if (in_time) {
                    if (has_second) {
                        parsed->error = PARSER_INVALID_DURATION;

                        return NULL;
                    }

                    if (has_fractional) {
                        parsed->error = PARSER_INVALID_DURATION;

                        return NULL;
                    }

                    parsed->minutes = value;
                    if (fraction) {
                        parsed->seconds = fraction * 6;
                        has_fractional = 1;
                    }

                    has_minute = 1;
                } else {
                    if (fraction) {
                        parsed->error = PARSER_INVALID_DURATION_FLOAT_YEAR_MONTH_NOT_SUPPORTED;

                        return NULL;
                    }

                    if (has_month || has_day) {
                        parsed->error = PARSER_INVALID_DURATION;

                        return NULL;
                    }

                    parsed->months = value;
                    has_ymd = 1;
                    has_month = 1;
                }

                grabbed = 0;
                value = 0;
                fraction = 0;
                in_fraction = 0;

                break;
            case 'D':
                if (!grabbed || in_time || has_week) {
                    // No value grabbed
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                if (has_day) {
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                parsed->days = value;
                if (fraction) {
                    parsed->hours = fraction * 2.4;
                    has_fractional = 1;
                }

                grabbed = 0;
                value = 0;
                fraction = 0;
                in_fraction = 0;
                has_ymd = 1;
                has_day = 1;

                break;
            case 'T':
                if (grabbed) {
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                in_time = 1;

                break;
            case 'H':
                if (!grabbed || !in_time || has_week) {
                    // No value grabbed
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                if (has_hour || has_second || has_minute) {
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                if (has_fractional) {
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                parsed->hours = value;
                if (fraction) {
                    parsed->minutes = fraction * 6;
                    has_fractional = 1;
                }

                grabbed = 0;
                value = 0;
                fraction = 0;
                in_fraction = 0;
                has_hour = 1;

                break;
            case 'S':
                if (!grabbed || !in_time || has_week) {
                    // No value grabbed
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                if (has_second) {
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                if (has_fractional) {
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                if (fraction) {
                    parsed->seconds = value;
                    if (fraction_length > 6) {
                        parsed->microseconds = fraction / pow(10, fraction_length - 6);
                    } else {
                        parsed->microseconds = fraction * pow(10, 6 - fraction_length);
                    }
                    has_fractional = 1;
                } else {
                    parsed->seconds = value;
                }

                grabbed = 0;
                value = 0;
                fraction = 0;
                in_fraction = 0;
                has_second = 1;

                break;
            case 'W':
                if (!grabbed || in_time || has_ymd) {
                    // No value grabbed
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                parsed->weeks = value;
                if (fraction) {
                    float days;
                    days = fraction * 0.7;
                    parsed->hours = (int) ((days - (int) days) * 24);
                    parsed->days = (int) days;
                }

                grabbed = 0;
                value = 0;
                fraction = 0;
                in_fraction = 0;
                has_week = 1;

                break;
            case '.':
                if (!grabbed || has_fractional) {
                    // No value grabbed
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                in_fraction = 1;

                break;
            case ',':
                if (!grabbed || has_fractional) {
                    // No value grabbed
                    parsed->error = PARSER_INVALID_DURATION;

                    return NULL;
                }

                in_fraction = 1;

                break;
            default:
                if (*c >= '0' && *c <='9') {
                    if (in_fraction) {
                        fraction = 10 * fraction + *c - '0';
                        fraction_length++;
                    } else {
                        value = 10 * value + *c - '0';
                        grabbed = 1;
                    }
                    break;
                }

                parsed->error = PARSER_INVALID_DURATION;

                return NULL;
        }
    }

    return parsed;
}


PyObject* parse_iso8601(PyObject *self, PyObject *args) {
    char* str;
    PyObject *obj;
    PyObject *tzinfo;
    Parsed *parsed = new_parsed();

    if (!PyArg_ParseTuple(args, "s", &str)) {
        PyErr_SetString(
            PyExc_ValueError, "Invalid parameters"
        );
        free(parsed);
        return NULL;
    }

    if (*str == 'P') {
        // Duration (or interval)
        if (_parse_iso8601_duration(str, parsed) == NULL) {
            PyErr_SetString(
                PyExc_ValueError, PARSER_ERRORS[parsed->error]
            );

            free(parsed);
            return NULL;
        }
    } else if (_parse_iso8601_datetime(str, parsed) == NULL) {
        PyErr_SetString(
            PyExc_ValueError, PARSER_ERRORS[parsed->error]
        );

        free(parsed);
        return NULL;
    }

    if (parsed->is_date) {
        // Date only
        if (parsed->ambiguous) {
            // We can "safely" assume that the ambiguous
            // date was actually a time in the form hhmmss
            parsed->hour = parsed->year / 100;
            parsed->minute = parsed->year % 100;
            parsed->second = parsed->month;

            obj = PyDateTimeAPI->Time_FromTime(
                parsed->hour, parsed->minute, parsed->second, parsed->microsecond,
                Py_BuildValue(""),
                PyDateTimeAPI->TimeType
            );
        } else {
            obj = PyDateTimeAPI->Date_FromDate(
                parsed->year, parsed->month, parsed->day,
                PyDateTimeAPI->DateType
            );
        }
    } else if (parsed->is_datetime) {
        if (!parsed->has_offset) {
            tzinfo = Py_BuildValue("");
        } else {
            tzinfo = new_fixed_offset(parsed->offset, parsed->tzname);
        }

        obj = PyDateTimeAPI->DateTime_FromDateAndTime(
            parsed->year,
            parsed->month,
            parsed->day,
            parsed->hour,
            parsed->minute,
            parsed->second,
            parsed->microsecond,
            tzinfo,
            PyDateTimeAPI->DateTimeType
        );

        Py_DECREF(tzinfo);
    } else if (parsed->is_duration) {
        obj = new_duration(
            parsed->years, parsed->months, parsed->weeks, parsed->days,
            parsed->hours, parsed->minutes, parsed->seconds, parsed->microseconds
        );
    } else {
        free(parsed);
        return NULL;
    }

    free(parsed);

    return obj;
}


/* ------------------------------------------------------------------------- */

static PyMethodDef helpers_methods[] = {
    {
        "parse_iso8601",
        (PyCFunction) parse_iso8601,
        METH_VARARGS,
        PyDoc_STR("Parses a ISO8601 string into a tuple.")
    },
    {NULL}
};


/* ------------------------------------------------------------------------- */

static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    "_iso8601",
    NULL,
    -1,
    helpers_methods,
    NULL,
    NULL,
    NULL,
    NULL,
};

PyMODINIT_FUNC
PyInit__iso8601(void)
{
    PyObject *module;

    PyDateTime_IMPORT;

    module = PyModule_Create(&moduledef);

    if (module == NULL)
        return NULL;

    // FixedOffset declaration
    FixedOffset_type.tp_new = PyType_GenericNew;
    FixedOffset_type.tp_base = PyDateTimeAPI->TZInfoType;
    FixedOffset_type.tp_methods = FixedOffset_methods;
    FixedOffset_type.tp_members = FixedOffset_members;
    FixedOffset_type.tp_init = (initproc)FixedOffset_init;

    if (PyType_Ready(&FixedOffset_type) < 0)
        return NULL;

    // Duration declaration
    Duration_type.tp_new = PyType_GenericNew;
    Duration_type.tp_members = Duration_members;
    Duration_type.tp_init = (initproc)Duration_init;

    if (PyType_Ready(&Duration_type) < 0)
        return NULL;

    Py_INCREF(&FixedOffset_type);
    Py_INCREF(&Duration_type);

    PyModule_AddObject(module, "TZFixedOffset", (PyObject *)&FixedOffset_type);
    PyModule_AddObject(module, "Duration", (PyObject *)&Duration_type);

    return module;
}