File: moment.c

package info (click to toggle)
libtime-moment-perl 0.41-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,024 kB
  • ctags: 1,354
  • sloc: perl: 6,312; ansic: 2,936; makefile: 3
file content (1263 lines) | stat: -rw-r--r-- 35,023 bytes parent folder | download | duplicates (2)
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
#include "moment.h"
#include "dt_core.h"
#include "dt_accessor.h"
#include "dt_arithmetic.h"
#include "dt_util.h"
#include "dt_length.h"
#include "dt_easter.h"

static const int32_t kPow10[10] = {
    1,
    10,
    100,
    1000,
    10000,
    100000,
    1000000,
    10000000,
    100000000,
    1000000000,
};

static void
THX_moment_check_self(pTHX_ const moment_t *mt) {
    if (mt->sec < MIN_RANGE || mt->sec > MAX_RANGE)
        croak("Time::Moment is out of range");
}

static moment_t
THX_moment_from_local(pTHX_ int64_t sec, IV nsec, IV offset) {
    moment_t r;

    r.sec    = sec;
    r.nsec   = (int32_t)nsec;
    r.offset = (int32_t)offset;
    THX_moment_check_self(aTHX_ &r);
    return r;
}

static moment_t
THX_moment_from_instant(pTHX_ int64_t sec, IV nsec, IV offset) {
    moment_t r;

    r.sec    = sec + offset * 60;
    r.nsec   = (int32_t)nsec;
    r.offset = (int32_t)offset;
    THX_moment_check_self(aTHX_ &r);
    return r;
}

int64_t
moment_instant_rd_seconds(const moment_t *mt) {
    return mt->sec - mt->offset * 60;
}

int
moment_instant_rd(const moment_t *mt) {
    return (int)(moment_instant_rd_seconds(mt) / SECS_PER_DAY);
}

void
moment_to_instant_rd_values(const moment_t *mt, IV *rdn, IV *sod, IV *nos) {
    const int64_t sec = moment_instant_rd_seconds(mt);
    *rdn = (IV)(sec / SECS_PER_DAY);
    *sod = (IV)(sec % SECS_PER_DAY);
    *nos = (IV)mt->nsec;
}

int64_t
moment_local_rd_seconds(const moment_t *mt) {
    return mt->sec;
}

int
moment_local_rd(const moment_t *mt) {
    return (int)(moment_local_rd_seconds(mt) / SECS_PER_DAY);
}

dt_t
moment_local_dt(const moment_t *mt) {
    return dt_from_rdn(moment_local_rd(mt));
}

void
moment_to_local_rd_values(const moment_t *mt, IV *rdn, IV *sod, IV *nos) {
    const int64_t sec = moment_local_rd_seconds(mt);
    *rdn = (IV)(sec / SECS_PER_DAY);
    *sod = (IV)(sec % SECS_PER_DAY);
    *nos = (IV)mt->nsec;
}

static void
THX_check_year(pTHX_ int64_t v) {
    if (v < 1 || v > 9999)
        croak("Parameter 'year' is out of the range [1, 9999]");
}

static void
THX_check_quarter(pTHX_ int64_t v) {
    if (v < 1 || v > 4)
        croak("Parameter 'quarter' is out of the range [1, 4]");
}

static void
THX_check_month(pTHX_ int64_t v) {
    if (v < 1 || v > 12)
        croak("Parameter 'month' is out of the range [1, 12]");
}

static void
THX_check_week(pTHX_ int64_t v) {
    if (v < 1 || v > 53)
        croak("Parameter 'week' is out of the range [1, 53]");
}

static void
THX_check_day_of_year(pTHX_ int64_t v) {
    if (v < 1 || v > 366)
        croak("Parameter 'day' is out of the range [1, 366]");
}

static void
THX_check_day_of_quarter(pTHX_ int64_t v) {
    if (v < 1 || v > 92)
        croak("Parameter 'day' is out of the range [1, 92]");
}

static void
THX_check_day_of_month(pTHX_ int64_t v) {
    if (v < 1 || v > 31)
        croak("Parameter 'day' is out of the range [1, 31]");
}

static void
THX_check_day_of_week(pTHX_ int64_t v) {
    if (v < 1 || v > 7)
        croak("Parameter 'day' is out of the range [1, 7]");
}

static void
THX_check_hour(pTHX_ int64_t v) {
    if (v < 0 || v > 23)
        croak("Parameter 'hour' is out of the range [1, 23]");
}

static void
THX_check_minute(pTHX_ int64_t v) {
    if (v < 0 || v > 59)
        croak("Parameter 'minute' is out of the range [1, 59]");
}

static void
THX_check_minute_of_day(pTHX_ int64_t v) {
    if (v < 0 || v > 1439)
        croak("Parameter 'minute' is out of the range [1, 1439]");
}

static void
THX_check_second(pTHX_ int64_t v) {
    if (v < 0 || v > 59)
        croak("Parameter 'second' is out of the range [1, 59]");
}

static void
THX_check_second_of_day(pTHX_ int64_t v) {
    if (v < 0 || v > 86399)
        croak("Parameter 'second' is out of the range [0, 86_399]");
}

static void
THX_check_millisecond(pTHX_ int64_t v) {
    if (v < 0 || v > 999)
        croak("Parameter 'millisecond' is out of the range [0, 999]");
}

static void
THX_check_microsecond(pTHX_ int64_t v) {
    if (v < 0 || v > 999999)
        croak("Parameter 'microsecond' is out of the range [0, 999_999]");
}

static void
THX_check_nanosecond(pTHX_ int64_t v) {
    if (v < 0 || v > 999999999)
        croak("Parameter 'nanosecond' is out of the range [0, 999_999_999]");
}

static void
THX_check_offset(pTHX_ int64_t v) {
    if (v < -1080 || v > 1080)
        croak("Parameter 'offset' is out of the range [-1080, 1080]");
}

static void
THX_check_epoch_seconds(pTHX_ int64_t v) {
    if (!VALID_EPOCH_SEC(v))
        croak("Parameter 'seconds' is out of range");
}

static void
THX_check_rata_die_day(pTHX_ int64_t v) {
    if (v < MIN_RATA_DIE_DAY || v > MAX_RATA_DIE_DAY)
        croak("Parameter 'rdn' is out of range");
}

static void
THX_check_unit_years(pTHX_ int64_t v) {
    if (v < MIN_UNIT_YEARS || v > MAX_UNIT_YEARS)
        croak("Parameter 'years' is out of range");
}

static void
THX_check_unit_months(pTHX_ int64_t v) {
    if (v < MIN_UNIT_MONTHS || v > MAX_UNIT_MONTHS)
        croak("Parameter 'months' is out of range");
}

static void
THX_check_unit_weeks(pTHX_ int64_t v) {
    if (v < MIN_UNIT_WEEKS || v > MAX_UNIT_WEEKS)
        croak("Parameter 'weeks' is out of range");
}

static void
THX_check_unit_days(pTHX_ int64_t v) {
    if (v < MIN_UNIT_DAYS || v > MAX_UNIT_DAYS)
        croak("Parameter 'days' is out of range");
}

static void
THX_check_unit_hours(pTHX_ int64_t v) {
    if (v < MIN_UNIT_HOURS || v > MAX_UNIT_HOURS)
        croak("Parameter 'hours' is out of range");
}

static void
THX_check_unit_minutes(pTHX_ int64_t v) {
    if (v < MIN_UNIT_MINUTES || v > MAX_UNIT_MINUTES)
        croak("Parameter 'minutes' is out of range");
}

static void
THX_check_unit_seconds(pTHX_ int64_t v) {
    if (v < MIN_UNIT_SECONDS || v > MAX_UNIT_SECONDS)
        croak("Parameter 'seconds' is out of range");
}

static void
THX_check_unit_milliseconds(pTHX_ int64_t v) {
    if (v < MIN_UNIT_MILLIS || v > MAX_UNIT_MILLIS)
        croak("Parameter 'milliseconds' is out of range");
}

static void
THX_check_unit_microseconds(pTHX_ int64_t v) {
    if (v < MIN_UNIT_MICROS || v > MAX_UNIT_MICROS)
        croak("Parameter 'microseconds' is out of range");
}

moment_t
THX_moment_from_epoch(pTHX_ int64_t sec, IV nsec, IV offset) {

    THX_check_epoch_seconds(aTHX_ sec);
    THX_check_nanosecond(aTHX_ nsec);
    THX_check_offset(aTHX_ offset);

    sec += UNIX_EPOCH;
    return THX_moment_from_instant(aTHX_ sec, nsec, offset);
}

moment_t
THX_moment_from_epoch_nv(pTHX_ NV sec, IV precision) {
    static const NV SEC_MIN = -62135596801.0; /*  0000-12-31T23:59:59Z */
    static const NV SEC_MAX = 253402300800.0; /* 10000-01-01T00:00:00Z */
    NV s, f, n, denom;

    if (precision < 0 || precision > 9)
        croak("Parameter 'precision' is out of the range [0, 9]");

    if (!(sec > SEC_MIN && sec < SEC_MAX))
        croak("Parameter 'seconds' is out of range");

    f = n = Perl_fmod(sec, 1.0);
    s = Perl_floor(sec - f);
    if (n < 0)
        n += 1.0;
    s = s + Perl_floor(f - n);
    denom = Perl_pow(10.0, (NV)precision);
    n = (Perl_floor(n * denom + 0.5) / denom) * 1E9;
    return THX_moment_from_epoch(aTHX_ (int64_t)s, (IV)(n + 0.5), 0);
}

static int
THX_moment_from_sd(pTHX_ NV sd, NV epoch, IV precision, int64_t *sec, int32_t *nsec) {
    static const NV SD_MIN = -146097 * 50;
    static const NV SD_MAX =  146097 * 50;
    NV d1, d2, f1, f2, f, d, s, denom;

    if (precision < 0 || precision > 9)
        croak("Parameter 'precision' is out of the range [0, 9]");

    if (!(sd > SD_MIN && sd < SD_MAX))
        return -1;

    if (!(epoch > SD_MIN && epoch < SD_MAX))
        croak("Parameter 'epoch' is out of range");

    if (sd >= epoch) {
        d1 = sd;
        d2 = epoch;
    }
    else {
        d1 = epoch;
        d2 = sd;
    }

    f1 = Perl_fmod(d1, 1.0);
    f2 = Perl_fmod(d2, 1.0);
    d1 = Perl_floor(d1 - f1);
    d2 = Perl_floor(d2 - f2);

    f = Perl_fmod(f1 + f2, 1.0);
    if (f < 0.0)
        f += 1.0;

    d = d1 + d2 + Perl_floor(f1 + f2 - f);
    f *= 86400;
    s = Perl_floor(f);

    if (d < 1 || d > 3652059)
        return -2;

    denom = Perl_pow(10.0, (NV)precision);
    f = (Perl_floor((f - s) * denom + 0.5) / denom) * 1E9;

    *sec = (int64_t)d * 86400 + (int32_t)s;
    *nsec = (int32_t)(f + 0.5);

    if (*nsec >= NANOS_PER_SEC) {
        *nsec -= NANOS_PER_SEC;
        *sec += 1;
    }
    return 0;
}

moment_t
THX_moment_from_rd(pTHX_ NV jd, NV epoch, IV precision, IV offset) {
    int64_t sec;
    int32_t nsec;
    int r;

    THX_check_offset(aTHX_ offset);

    r = THX_moment_from_sd(aTHX_ jd, epoch, precision, &sec, &nsec);
    if (r < 0) {
        if (r == -1)
            croak("Parameter 'rd' is out of range");
        else
            croak("Rata Die is out of range");
    }
    return THX_moment_from_local(aTHX_ sec, nsec, offset);
}

moment_t
THX_moment_from_jd(pTHX_ NV jd, NV epoch, IV precision) {
    int64_t sec;
    int32_t nsec;
    int r;

    r = THX_moment_from_sd(aTHX_ jd, epoch, precision, &sec, &nsec);
    if (r < 0) {
        if (r == -1)
            croak("Parameter 'jd' is out of range");
        else
            croak("Julian date is out of range");
    }

    return THX_moment_from_instant(aTHX_ sec, nsec, 0);
}

moment_t
THX_moment_from_mjd(pTHX_ NV jd, NV epoch, IV precision) {
    int64_t sec;
    int32_t nsec;
    int r;

    r = THX_moment_from_sd(aTHX_ jd, epoch, precision, &sec, &nsec);
    if (r < 0) {
        if (r == -1)
            croak("Parameter 'mjd' is out of range");
        else
            croak("Modified Julian date is out of range");
    }

    return THX_moment_from_instant(aTHX_ sec, nsec, 0);
}

moment_t
THX_moment_new(pTHX_ IV Y, IV M, IV D, IV h, IV m, IV s, IV nsec, IV offset) {
    int64_t rdn, sec;

    THX_check_year(aTHX_ Y);
    THX_check_month(aTHX_ M);
    THX_check_day_of_month(aTHX_ D);
    if (D > 28) {
        int dim = dt_days_in_month((int)Y, (int)M);
        if (D > dim)
            croak("Parameter 'day' is out of the range [1, %d]", dim);
    }
    THX_check_hour(aTHX_ h);
    THX_check_minute(aTHX_ m);
    THX_check_second(aTHX_ s);
    THX_check_nanosecond(aTHX_ nsec);
    THX_check_offset(aTHX_ offset);

    rdn = dt_rdn(dt_from_ymd((int)Y, (int)M, (int)D));
    sec = ((rdn * 24 + h) * 60 + m) * 60 + s;
    return THX_moment_from_local(aTHX_ sec, nsec, offset);
}

static moment_t
THX_moment_with_local_dt(pTHX_ const moment_t *mt, const dt_t dt) {
    int64_t sec;

    sec = (int64_t)dt_rdn(dt) * 86400 + moment_second_of_day(mt);
    return THX_moment_from_local(aTHX_ sec, mt->nsec, mt->offset);
}

static moment_t
THX_moment_with_ymd(pTHX_ const moment_t *mt, int y, int m, int d) {

    if (d > 28) {
        int dim = dt_days_in_month(y, m);
        if (d > dim)
            d = dim;
    }
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_ymd(y, m, d));
}

static moment_t
THX_moment_with_year(pTHX_ const moment_t *mt, int64_t v) {
    int m, d;

    THX_check_year(aTHX_ v);
    dt_to_ymd(moment_local_dt(mt), NULL, &m, &d);
    return THX_moment_with_ymd(aTHX_ mt, (int)v, m, d);
}

static moment_t
THX_moment_with_quarter(pTHX_ const moment_t *mt, int64_t v) {
    int y, m, d;

    THX_check_quarter(aTHX_ v);
    dt_to_ymd(moment_local_dt(mt), &y, &m, &d);
    m = 1 + 3 * ((int)v - 1) + (m - 1) % 3;
    return THX_moment_with_ymd(aTHX_ mt, y, m, d);
}

static moment_t
THX_moment_with_month(pTHX_ const moment_t *mt, int64_t v) {
    int y, d;

    THX_check_month(aTHX_ v);
    dt_to_ymd(moment_local_dt(mt), &y, NULL, &d);
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_ymd(y, (int)v, d));
}

static moment_t
THX_moment_with_week(pTHX_ const moment_t *mt, int64_t v) {
    int y, w, d;

    THX_check_week(aTHX_ v);
    dt_to_ywd(moment_local_dt(mt), &y, NULL, &d);
    w = (int)v;
    if (w > 52) {
        int wiy = dt_weeks_in_year(y);
        if (w > wiy)
            croak("Parameter 'week' is out of the range [1, %d]", wiy);
    }
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_ywd(y, w, d));
}

static moment_t
THX_moment_with_day_of_month(pTHX_ const moment_t *mt, int64_t v) {
    int y, m, d;

    THX_check_day_of_month(aTHX_ v);
    dt_to_ymd(moment_local_dt(mt), &y, &m, NULL);
    d = (int)v;
    if (d > 28) {
        int dim = dt_days_in_month(y, m);
        if (d > dim)
            croak("Parameter 'day' is out of the range [1, %d]", dim);
    }
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_ymd(y, m, d));
}

static moment_t
THX_moment_with_day_of_quarter(pTHX_ const moment_t *mt, int64_t v) {
    int y, q, d;

    THX_check_day_of_quarter(aTHX_ v);
    dt_to_yqd(moment_local_dt(mt), &y, &q, NULL);
    d = (int)v;
    if (d > 90) {
        int diq = dt_days_in_quarter(y, q);
        if (d > diq)
            croak("Parameter 'day' is out of the range [1, %d]", diq);
    }
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_yqd(y, q, d));
}

static moment_t
THX_moment_with_day_of_year(pTHX_ const moment_t *mt, int64_t v) {
    int y, d;

    THX_check_day_of_year(aTHX_ v);
    dt_to_yd(moment_local_dt(mt), &y, NULL);
    d = (int)v;
    if (d > 365) {
        int diy = dt_days_in_year(y);
        if (v > diy)
            croak("Parameter 'day' is out of the range [1, %d]", diy);
    }
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_yd(y, d));
}

static moment_t
THX_moment_with_day_of_week(pTHX_ const moment_t *mt, int64_t v) {
    dt_t dt;

    THX_check_day_of_week(aTHX_ v);
    dt = moment_local_dt(mt);
    return THX_moment_with_local_dt(aTHX_ mt, dt - (dt_dow(dt) - v));
}

static moment_t
THX_moment_with_rata_die_day(pTHX_ const moment_t *mt, int64_t v) {
    dt_t dt;

    THX_check_rata_die_day(aTHX_ v);
    dt = dt_from_rdn((int)v);
    return THX_moment_with_local_dt(aTHX_ mt, dt);
}

static moment_t
THX_moment_with_hour(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_hour(aTHX_ v);
    sec = moment_local_rd_seconds(mt) + (v - moment_hour(mt)) * 3600;
    return THX_moment_from_local(aTHX_ sec, mt->nsec, mt->offset);
}

static moment_t
THX_moment_with_minute(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_minute(aTHX_ v);
    sec = moment_local_rd_seconds(mt) + (v - moment_minute(mt)) * 60;
    return THX_moment_from_local(aTHX_ sec, mt->nsec, mt->offset);
}

static moment_t
THX_moment_with_minute_of_day(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_minute_of_day(aTHX_ v);
    sec = moment_local_rd_seconds(mt) + (v - moment_minute_of_day(mt)) * 60;
    return THX_moment_from_local(aTHX_ sec, mt->nsec, mt->offset);
}

static moment_t
THX_moment_with_second(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_second(aTHX_ v);
    sec = moment_local_rd_seconds(mt) + (v - moment_second(mt));
    return THX_moment_from_local(aTHX_ sec, mt->nsec, mt->offset);
}

static moment_t
THX_moment_with_second_of_day(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_second_of_day(aTHX_ v);
    sec = moment_local_rd_seconds(mt) + (v - moment_second_of_day(mt));
    return THX_moment_from_local(aTHX_ sec, mt->nsec, mt->offset);
}

static moment_t
THX_moment_with_millisecond(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_millisecond(aTHX_ v);
    sec = moment_local_rd_seconds(mt);
    return THX_moment_from_local(aTHX_ sec, v * 1000000, mt->offset);
}

static moment_t
THX_moment_with_microsecond(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_microsecond(aTHX_ v);
    sec = moment_local_rd_seconds(mt);
    return THX_moment_from_local(aTHX_ sec, v * 1000, mt->offset);
}

static moment_t
THX_moment_with_nanosecond(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_nanosecond(aTHX_ v);
    sec = moment_local_rd_seconds(mt);
    return THX_moment_from_local(aTHX_ sec, v, mt->offset);
}

static moment_t
THX_moment_with_nanosecond_of_day(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;
    int32_t nsec;

    if (v < 0 || v > INT64_C(86400000000000))
        croak("Paramteter 'nanosecond' is out of the range [0, 86_400_000_000_000]");

    sec = moment_local_rd_seconds(mt) + v / NANOS_PER_SEC - moment_second_of_day(mt);
    nsec = v % NANOS_PER_SEC;
    return THX_moment_from_local(aTHX_ sec, nsec, mt->offset);
}

static moment_t
THX_moment_with_microsecond_of_day(pTHX_ const moment_t *mt, int64_t v) {
    if (v < 0 || v > INT64_C(86400000000))
        croak("Paramteter 'microsecond' is out of the range [0, 86_400_000_000]");
    return THX_moment_with_nanosecond_of_day(aTHX_ mt, v * 1000);
}

static moment_t
THX_moment_with_millisecond_of_day(pTHX_ const moment_t *mt, int64_t v) {
    if (v < 0 || v > INT64_C(86400000))
        croak("Paramteter 'millisecond' is out of the range [0, 86_400_000]");
    return THX_moment_with_nanosecond_of_day(aTHX_ mt, v * 1000000);
}

moment_t
THX_moment_with_field(pTHX_ const moment_t *mt, moment_component_t c, int64_t v) {
    switch (c) {
        case MOMENT_FIELD_YEAR:
            return THX_moment_with_year(aTHX_ mt, v);
        case MOMENT_FIELD_QUARTER_OF_YEAR:
            return THX_moment_with_quarter(aTHX_ mt, v);
        case MOMENT_FIELD_MONTH_OF_YEAR:
            return THX_moment_with_month(aTHX_ mt, v);
        case MOMENT_FIELD_WEEK_OF_YEAR:
            return THX_moment_with_week(aTHX_ mt, v);
        case MOMENT_FIELD_DAY_OF_MONTH:
            return THX_moment_with_day_of_month(aTHX_ mt, v);
        case MOMENT_FIELD_DAY_OF_QUARTER:
            return THX_moment_with_day_of_quarter(aTHX_ mt, v);
        case MOMENT_FIELD_DAY_OF_YEAR:
            return THX_moment_with_day_of_year(aTHX_ mt, v);
        case MOMENT_FIELD_DAY_OF_WEEK:
            return THX_moment_with_day_of_week(aTHX_ mt, v);
        case MOMENT_FIELD_HOUR_OF_DAY:
            return THX_moment_with_hour(aTHX_ mt, v);
        case MOMENT_FIELD_MINUTE_OF_HOUR:
            return THX_moment_with_minute(aTHX_ mt, v);
        case MOMENT_FIELD_MINUTE_OF_DAY:
            return THX_moment_with_minute_of_day(aTHX_ mt, v);
        case MOMENT_FIELD_SECOND_OF_MINUTE:
            return THX_moment_with_second(aTHX_ mt, v);
        case MOMENT_FIELD_SECOND_OF_DAY:
            return THX_moment_with_second_of_day(aTHX_ mt, v);
        case MOMENT_FIELD_MILLI_OF_SECOND:
            return THX_moment_with_millisecond(aTHX_ mt, v);
        case MOMENT_FIELD_MILLI_OF_DAY:
            return THX_moment_with_millisecond_of_day(aTHX_ mt, v);
        case MOMENT_FIELD_MICRO_OF_SECOND:
            return THX_moment_with_microsecond(aTHX_ mt, v);
        case MOMENT_FIELD_MICRO_OF_DAY:
            return THX_moment_with_microsecond_of_day(aTHX_ mt, v);
        case MOMENT_FIELD_NANO_OF_SECOND:
            return THX_moment_with_nanosecond(aTHX_ mt, v);
        case MOMENT_FIELD_NANO_OF_DAY:
            return THX_moment_with_nanosecond_of_day(aTHX_ mt, v);
        case MOMENT_FIELD_PRECISION:
            return THX_moment_with_precision(aTHX_ mt, v);
        case MOMENT_FIELD_RATA_DIE_DAY:
            return THX_moment_with_rata_die_day(aTHX_ mt, v);
    }
    croak("panic: THX_moment_with_component() called with unknown component (%d)", (int)c);
}

static moment_t
THX_moment_plus_months(pTHX_ const moment_t *mt, int64_t v) {
    dt_t dt;

    THX_check_unit_months(aTHX_ v);
    dt = dt_add_months(moment_local_dt(mt), (int)v, DT_LIMIT);
    return THX_moment_with_local_dt(aTHX_ mt, dt); 
}

static moment_t
THX_moment_plus_days(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_unit_days(aTHX_ v);
    sec = moment_local_rd_seconds(mt) + v * 86400;
    return THX_moment_from_local(aTHX_ sec, mt->nsec, mt->offset);
}

static moment_t
THX_moment_plus_seconds(pTHX_ const moment_t *mt, int64_t v) {
    int64_t sec;

    THX_check_unit_seconds(aTHX_ v);
    sec = moment_instant_rd_seconds(mt) + v;
    return THX_moment_from_instant(aTHX_ sec, mt->nsec, mt->offset);
}

static moment_t
THX_moment_plus_time(pTHX_ const moment_t *mt, int64_t sec, int64_t nsec, int sign) {

    sec  = sec + (nsec / NANOS_PER_SEC);
    nsec = nsec % NANOS_PER_SEC;

    sec  = moment_instant_rd_seconds(mt) + sec * sign;
    nsec = mt->nsec + nsec * sign;

    if (nsec < 0) {
        nsec += NANOS_PER_SEC;
        sec--;
    }
    else if (nsec >= NANOS_PER_SEC) {
        nsec -= NANOS_PER_SEC;
        sec++;
    }
    return THX_moment_from_instant(aTHX_ sec, (IV)nsec, mt->offset);
}

moment_t
THX_moment_plus_unit(pTHX_ const moment_t *mt, moment_unit_t u, int64_t v) {
    switch (u) {
        case MOMENT_UNIT_YEARS:
            THX_check_unit_years(aTHX_ v);
            return THX_moment_plus_months(aTHX_ mt, v * 12);
        case MOMENT_UNIT_MONTHS:
            THX_check_unit_months(aTHX_ v);
            return THX_moment_plus_months(aTHX_ mt, v);
        case MOMENT_UNIT_WEEKS:
            THX_check_unit_weeks(aTHX_ v);
            return THX_moment_plus_days(aTHX_ mt, v * 7);
        case MOMENT_UNIT_DAYS:
            THX_check_unit_days(aTHX_ v);
            return THX_moment_plus_days(aTHX_ mt, v);
        case MOMENT_UNIT_HOURS:
            THX_check_unit_hours(aTHX_ v);
            return THX_moment_plus_seconds(aTHX_ mt, v * 3600);
        case MOMENT_UNIT_MINUTES:
            THX_check_unit_minutes(aTHX_ v);
            return THX_moment_plus_seconds(aTHX_ mt, v * 60);
        case MOMENT_UNIT_SECONDS:
            THX_check_unit_seconds(aTHX_ v);
            return THX_moment_plus_seconds(aTHX_ mt, v);
        case MOMENT_UNIT_MILLIS:
            THX_check_unit_milliseconds(aTHX_ v);
            return THX_moment_plus_time(aTHX_ mt, v / 1000, (v % 1000) * 1000000, 1);
        case MOMENT_UNIT_MICROS:
            THX_check_unit_microseconds(aTHX_ v);
            return THX_moment_plus_time(aTHX_ mt, v / 1000000, (v % 1000000) * 1000, 1);
        case MOMENT_UNIT_NANOS:
            return THX_moment_plus_time(aTHX_ mt, 0, v, 1);
    }
    croak("panic: THX_moment_plus_unit() called with unknown unit (%d)", (int)u);
}

moment_t
THX_moment_minus_unit(pTHX_ const moment_t *mt, moment_unit_t u, int64_t v) {
    switch (u) {
        case MOMENT_UNIT_YEARS:
            THX_check_unit_years(aTHX_ v);
            return THX_moment_plus_months(aTHX_ mt, -v * 12);
        case MOMENT_UNIT_MONTHS:
            THX_check_unit_months(aTHX_ v);
            return THX_moment_plus_months(aTHX_ mt, -v);
        case MOMENT_UNIT_WEEKS:
            THX_check_unit_weeks(aTHX_ v);
            return THX_moment_plus_days(aTHX_ mt, -v * 7);
        case MOMENT_UNIT_DAYS:
            THX_check_unit_days(aTHX_ v);
            return THX_moment_plus_days(aTHX_ mt, -v);
        case MOMENT_UNIT_HOURS:
            THX_check_unit_hours(aTHX_ v);
            return THX_moment_plus_seconds(aTHX_ mt, -v * 3600);
        case MOMENT_UNIT_MINUTES:
            THX_check_unit_minutes(aTHX_ v);
            return THX_moment_plus_seconds(aTHX_ mt, -v * 60);
        case MOMENT_UNIT_SECONDS:
            THX_check_unit_seconds(aTHX_ v);
            return THX_moment_plus_seconds(aTHX_ mt, -v);
        case MOMENT_UNIT_MILLIS:
            THX_check_unit_milliseconds(aTHX_ v);
            return THX_moment_plus_time(aTHX_ mt, v / 1000, (v % 1000) * 1000000, -1);
        case MOMENT_UNIT_MICROS:
            THX_check_unit_microseconds(aTHX_ v);
            return THX_moment_plus_time(aTHX_ mt, v / 1000000, (v % 1000000) * 1000, -1);
        case MOMENT_UNIT_NANOS:
            return THX_moment_plus_time(aTHX_ mt, 0, v, -1);
    }
    croak("panic: THX_moment_minus_unit() called with unknown unit (%d)", (int)u);
}

moment_t
THX_moment_with_offset_same_instant(pTHX_ const moment_t *mt, IV offset) {
    int64_t sec;

    THX_check_offset(aTHX_ offset);
    sec = moment_instant_rd_seconds(mt);
    return THX_moment_from_instant(aTHX_ sec, mt->nsec, offset);
}

moment_t
THX_moment_with_offset_same_local(pTHX_ const moment_t *mt, IV offset) {
    int64_t sec;

    THX_check_offset(aTHX_ offset);
    sec = moment_local_rd_seconds(mt);
    return THX_moment_from_local(aTHX_ sec, mt->nsec, offset);
}

moment_t
THX_moment_with_precision(pTHX_ const moment_t *mt, int64_t precision) {
    int64_t sec;
    int32_t nsec;

    if (precision < -3 || precision > 9)
        croak("Parameter 'precision' is out of the range [-3, 9]");

    sec = moment_local_rd_seconds(mt);
    nsec = mt->nsec;
    if (precision <= 0) {
        nsec = 0;
        switch (precision) {
            case -1: sec -= sec % 60;       break;
            case -2: sec -= sec % 3600;     break;
            case -3: sec -= sec % 86400;    break;
        }
    }
    else {
        nsec -= nsec % kPow10[9 - precision];
    }
    return THX_moment_from_local(aTHX_ sec, nsec, mt->offset);
}

moment_duration_t
moment_subtract_moment(const moment_t *mt1, const moment_t *mt2) {
    const int64_t s1 = moment_instant_rd_seconds(mt1);
    const int64_t s2 = moment_instant_rd_seconds(mt2);
    moment_duration_t d;

    d.sec = s2 - s1;
    d.nsec = mt2->nsec - mt1->nsec;
    if (d.nsec < 0) {
        d.sec -= 1;
        d.nsec += NANOS_PER_SEC;
    }
    return d;
}

static int
moment_delta_days(const moment_t *mt1, const moment_t *mt2) {
    const dt_t dt1 = moment_local_dt(mt1);
    const dt_t dt2 = moment_local_dt(mt2);
    return dt2 - dt1;
}

static int
moment_delta_weeks(const moment_t *mt1, const moment_t *mt2) {
    return moment_delta_days(mt1, mt2) / 7;
}

static int
moment_delta_months(const moment_t *mt1, const moment_t *mt2) {
    const dt_t dt1 = moment_local_dt(mt1);
    const dt_t dt2 = moment_local_dt(mt2);
    return dt_delta_months(dt1, dt2, true);
}

static int
moment_delta_years(const moment_t *mt1, const moment_t *mt2) {
    return moment_delta_months(mt1, mt2) / 12;
}

static int64_t
THX_moment_delta_hours(pTHX_ const moment_t *mt1, const moment_t *mt2) {
    moment_duration_t d;
    d = moment_subtract_moment(mt1, mt2);
    return (d.sec / 3600);
}

static int64_t
THX_moment_delta_minutes(pTHX_ const moment_t *mt1, const moment_t *mt2) {
    moment_duration_t d;
    d = moment_subtract_moment(mt1, mt2);
    return (d.sec / 60);
}

static int64_t
THX_moment_delta_seconds(pTHX_ const moment_t *mt1, const moment_t *mt2) {
    moment_duration_t d;
    d = moment_subtract_moment(mt1, mt2);
    return d.sec;
}

static int64_t
THX_moment_delta_milliseconds(pTHX_ const moment_t *mt1, const moment_t *mt2) {
    moment_duration_t d;
    d = moment_subtract_moment(mt1, mt2);
    return d.sec * 1000 + (d.nsec / 1000000);
}

static int64_t
THX_moment_delta_microseconds(pTHX_ const moment_t *mt1, const moment_t *mt2) {
    moment_duration_t d;
    d = moment_subtract_moment(mt1, mt2);
    return d.sec * 1000000 + (d.nsec / 1000);
}

static int64_t
THX_moment_delta_nanoseconds(pTHX_ const moment_t *mt1, const moment_t *mt2) {
    static const int64_t kMaxSec = INT64_C(9223372035);
    moment_duration_t d;

    d = moment_subtract_moment(mt1, mt2);
    if (d.sec > kMaxSec || d.sec < -kMaxSec)
        croak("Nanosecond duration is too large to be represented in a 64-bit integer");
    return d.sec * 1000000000 + d.nsec;
}

int64_t
THX_moment_delta_unit(pTHX_ const moment_t *mt1, const moment_t *mt2, moment_unit_t u) {
    switch (u) {
        case MOMENT_UNIT_YEARS:
            return moment_delta_years(mt1, mt2);
        case MOMENT_UNIT_MONTHS:
            return moment_delta_months(mt1, mt2);
        case MOMENT_UNIT_WEEKS:
            return moment_delta_weeks(mt1, mt2);
        case MOMENT_UNIT_DAYS:
            return moment_delta_days(mt1, mt2);
        case MOMENT_UNIT_HOURS:
            return THX_moment_delta_hours(aTHX_ mt1, mt2);
        case MOMENT_UNIT_MINUTES:
            return THX_moment_delta_minutes(aTHX_ mt1, mt2);
        case MOMENT_UNIT_SECONDS:
            return THX_moment_delta_seconds(aTHX_ mt1, mt2);
        case MOMENT_UNIT_MILLIS:
            return THX_moment_delta_milliseconds(aTHX_ mt1, mt2);
        case MOMENT_UNIT_MICROS:
            return THX_moment_delta_microseconds(aTHX_ mt1, mt2);
        case MOMENT_UNIT_NANOS:
            return THX_moment_delta_nanoseconds(aTHX_ mt1, mt2);
        default:
            croak("panic: THX_moment_delta_unit() called with unknown unit (%d)", (int)u);
    }
}

moment_t
THX_moment_at_utc(pTHX_ const moment_t *mt) {
    return THX_moment_with_offset_same_instant(aTHX_ mt, 0);
}

moment_t
THX_moment_at_midnight(pTHX_ const moment_t *mt) {
    return THX_moment_with_millisecond_of_day(aTHX_ mt, 0);
}

moment_t
THX_moment_at_noon(pTHX_ const moment_t *mt) {
    return THX_moment_with_millisecond_of_day(aTHX_ mt, 12*60*60*1000);
}

moment_t
THX_moment_at_last_day_of_year(pTHX_ const moment_t *mt) {
    int y;

    dt_to_yd(moment_local_dt(mt), &y, NULL);
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_yd(y + 1, 0));
}

moment_t
THX_moment_at_last_day_of_quarter(pTHX_ const moment_t *mt) {
    int y, q;

    dt_to_yqd(moment_local_dt(mt), &y, &q, NULL);
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_yqd(y, q + 1, 0));
}

moment_t
THX_moment_at_last_day_of_month(pTHX_ const moment_t *mt) {
    int y, m;

    dt_to_ymd(moment_local_dt(mt), &y, &m, NULL);
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_ymd(y, m + 1, 0));
}

int
moment_compare_instant(const moment_t *m1, const moment_t *m2) {
    const int64_t s1 = moment_instant_rd_seconds(m1);
    const int64_t s2 = moment_instant_rd_seconds(m2);
    int r;

    r = (s1 > s2) - (s1 < s2);
    if (r == 0)
        r = (m1->nsec > m2->nsec) - (m1->nsec < m2->nsec);
    return r;
}

int
moment_compare_local(const moment_t *m1, const moment_t *m2) {
    const int64_t s1 = moment_local_rd_seconds(m1);
    const int64_t s2 = moment_local_rd_seconds(m2);
    int r;

    r = (s1 > s2) - (s1 < s2);
    if (r == 0)
        r = (m1->nsec > m2->nsec) - (m1->nsec < m2->nsec);
    return r;
}

int
THX_moment_compare_precision(pTHX_ const moment_t *m1, const moment_t *m2, IV precision) {
    int64_t n1, n2;
    int r;

    if (precision < -3 || precision > 9)
        croak("Parameter 'precision' is out of the range [-3, 9]");

    if (precision < 0) {
        int32_t n;

        n = 0;
        switch (precision) {
            case -1: n = 60;    break;
            case -2: n = 3600;  break;
            case -3: n = 86400; break;
        }
        n1 = moment_local_rd_seconds(m1);
        n2 = moment_local_rd_seconds(m2);
        n1 -= n1 % n;
        n2 -= n2 % n;
        n1 -= m1->offset * 60;
        n2 -= m2->offset * 60;
        r = (n1 > n2) - (n1 < n2);
    }
    else {
        n1 = moment_instant_rd_seconds(m1);
        n2 = moment_instant_rd_seconds(m2);
        r = (n1 > n2) - (n1 < n2);
        if (r == 0 && precision != 0) {
            n1 = m1->nsec - m1->nsec % kPow10[9 - precision];
            n2 = m2->nsec - m2->nsec % kPow10[9 - precision];
            r = (n1 > n2) - (n1 < n2);
        }
    }
    return r;
}

bool
moment_equals(const moment_t *m1, const moment_t *m2) {
    return memcmp(m1, m2, sizeof(moment_t)) == 0;
}

int64_t
moment_epoch(const moment_t *mt) {
    return (moment_instant_rd_seconds(mt) - UNIX_EPOCH);
}

int
moment_year(const moment_t *mt) {
    return dt_year(moment_local_dt(mt));
}

int
moment_month(const moment_t *mt) {
    return dt_month(moment_local_dt(mt));
}

int
moment_quarter(const moment_t *mt) {
    return dt_quarter(moment_local_dt(mt));
}

int
moment_week(const moment_t *mt) {
    return dt_woy(moment_local_dt(mt));
}

int
moment_day_of_year(const moment_t *mt) {
    return dt_doy(moment_local_dt(mt));
}

int
moment_day_of_quarter(const moment_t *mt) {
    return dt_doq(moment_local_dt(mt));
}

int
moment_day_of_month(const moment_t *mt) {
    return dt_dom(moment_local_dt(mt));
}

int
moment_day_of_week(const moment_t *mt) {
    return dt_dow(moment_local_dt(mt));
}

int
moment_hour(const moment_t *mt) {
    return (int)((moment_local_rd_seconds(mt) / 3600) % 24);
}

int
moment_minute(const moment_t *mt) {
    return (int)((moment_local_rd_seconds(mt) / 60) % 60);
}

int
moment_minute_of_day(const moment_t *mt) {
    return (int)((moment_local_rd_seconds(mt) / 60) % 1440);
}

int
moment_second(const moment_t *mt) {
    return (int)(moment_local_rd_seconds(mt) % 60);
}

int
moment_second_of_day(const moment_t *mt) {
    return (int)(moment_local_rd_seconds(mt) % 86400);
}

int
moment_millisecond(const moment_t *mt) {
    return (mt->nsec / 1000000);
}

int
moment_millisecond_of_day(const moment_t *mt) {
    return moment_second_of_day(mt) * 1000 + moment_millisecond(mt);
}

int
moment_microsecond(const moment_t *mt) {
    return (mt->nsec / 1000);
}

int64_t
moment_microsecond_of_day(const moment_t *mt) {
    const int64_t sod = moment_local_rd_seconds(mt) % 86400;
    return sod * 1000000 + (mt->nsec / 1000);
}

int
moment_nanosecond(const moment_t *mt) {
    return mt->nsec;
}

int64_t
moment_nanosecond_of_day(const moment_t *mt) {
    const int64_t sod = moment_local_rd_seconds(mt) % 86400;
    return sod * 1000000000 + mt->nsec;
}

NV
moment_jd(const moment_t *mt) {
    return moment_mjd(mt) + 2400000.5;
}

NV
moment_mjd(const moment_t *mt) {
    const int64_t s = moment_instant_rd_seconds(mt);
    const int64_t d = (s / SECS_PER_DAY) - 678576;
    const int64_t n = (s % SECS_PER_DAY) * NANOS_PER_SEC + mt->nsec;
    return (NV)d + (NV)n * (1E-9/60/60/24);
}

NV
moment_rd(const moment_t *mt) {
    const int64_t s = moment_local_rd_seconds(mt);
    const int64_t d = (s / SECS_PER_DAY);
    const int64_t n = (s % SECS_PER_DAY) * NANOS_PER_SEC + mt->nsec;
    return (NV)d + (NV)n * (1E-9/60/60/24);
}

int
moment_rata_die_day(const moment_t *mt) {
    return dt_rdn(moment_local_dt(mt));
}

int
moment_offset(const moment_t *mt) {
    return mt->offset;
}

int
moment_precision(const moment_t *mt) {
    int v, i;

    v = mt->nsec;
    if (v != 0) {
        for (i = 8; i > 0; i--) {
            if ((v % kPow10[i]) == 0)
                break;
        }
        return 9 - i;
    }
    v = moment_second_of_day(mt);
    if (v != 0) {
        if      ((v % 3600) == 0) return -2;
        else if ((v %   60) == 0) return -1;
        else                      return 0;
    }
    return -3;
}

int
moment_length_of_year(const moment_t *mt) {
    return dt_length_of_year(moment_local_dt(mt));
}

int
moment_length_of_quarter(const moment_t *mt) {
    return dt_length_of_quarter(moment_local_dt(mt));
}

int
moment_length_of_month(const moment_t *mt) {
    return dt_length_of_month(moment_local_dt(mt));
}

int
moment_length_of_week_year(const moment_t *mt) {
    return dt_length_of_week_year(moment_local_dt(mt));
}

bool
moment_is_leap_year(const moment_t *mt) {
    return dt_leap_year(moment_year(mt));
}

int
THX_moment_internal_western_easter(pTHX_ int64_t y) {
    THX_check_year(aTHX_ y);
    return dt_rdn(dt_from_easter((int)y, DT_WESTERN));
}

int
THX_moment_internal_orthodox_easter(pTHX_ int64_t y) {
    THX_check_year(aTHX_ y);
    return dt_rdn(dt_from_easter((int)y, DT_ORTHODOX));
}