-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatediff.sh
executable file
·1386 lines (1223 loc) · 46.5 KB
/
datediff.sh
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
#!/usr/bin/env ksh
# datediff.sh - Calculate time ranges between dates
# v0.26.1 jan/2025 mountaineerbr GPLv3+
[[ -n $BASH_VERSION ]] && shopt -s extglob #bash2.05b+/ksh93u+/zsh5+
[[ -n $ZSH_VERSION ]] && setopt NO_SH_GLOB KSH_GLOB KSH_ARRAYS SH_WORD_SPLIT GLOB_SUBST
HELP="NAME
${0##*/} - Calculate time ranges / intervals between dates
SYNOPSIS
${0##*/} [-Rrttuvvv] [-NUM] [-f\"FMT\"] \"DATE1\" \"DATE2\"
${0##*/} -FF [-vv] [[DAY_IN_WEEK] [DAY_IN_MONTH]] [START_DATE]
${0##*/} [-ee|-l] [-v] YEAR..
${0##*/} -m [-v] DATE..
DESCRIPTION
Calculate time interval (elapsed time) between DATE1 and DATE2
in various time units. The \`C-code date' programme is optionally
executed to process input dates in formats other than ISO-8601.
Other features include checking if YEAR is leap, printing Easter,
Carnaval, and Corpus Christi dates on a given YEAR, and the phase
of the moon at DATE.
DATE AND TIME CALCULATIONS
\`GNU date' accepts mostly free format human readable date strings.
If using \`FreeBSD date', input DATE strings must be ISO-8601
(\`YYYY-MM-DDThh:mm:ss') or other supported time formats, unless
option \`-f FMT' is set to the new input format.
When \`C-code date' is not available, then input must be formatted
as ISO-8601 or UNIX time.
If DATE is not set, defaults to \`now'. If only one DATE is set,
the first one is assumed to be \`now' or \`1970'.
To flag DATE as UNIX time, prepend an at sign \`@' to the values or
set option -r. Stdin input is expected to have one DATE string per
line or two ISO-8601 DATES separated by space in a single line.
Input is processed in a best effort basis.
DATE AND TIME OUTPUT
Output RANGES section displays intervals in different units of
time (years, or months, or weeks, or days, or hours, or minutes,
or seconds alone). It also displays a compound time range with
all the mentioned units taken into consideration to each other.
Single UNIT time periods can be displayed in table format with
option -t. Decimal plates may be set with option -NUM where
NUM is an integer. Result is subject to rounding.
When last positional parameter UNIT is exactly one of \`Y', \`MO',
\`W', \`D', \`H', \`M', or \`S', only a single UNIT interval
is printed out.
Output DATES section shows the input dates in ISO-8601 format or,
if option -R is set, RFC-5322 format.
Option -u sets or prints dates in Coordinated Universal Time (UTC).
Note this affects the \`C-code date' programme, too.
Options -vvv filter output layout for specific time-frames.
SPECIAL DATE FUNCTIONS
Option -e prints Easter dates for given YEARS (Western Church)
and option -ee also prints Carnaval and Corpus Christi dates.
Option -l checks if YEAR is leap. Set option -v to decrease verbose.
ISO-8601 system assumes proleptic Gregorian calendar, year zero,
and no leap seconds.
Option -m prints lunar phase for UTC DATE (\`YYYY[-MM[-DD]]').
Code snippet adapted from NetHack.
Option -F prints the date of next Friday the 13th, START_DATE must
be formatted as \`YYYY[-MM[-DD]]'. Set -FF to print the following
10 matches. Optionally, set day-in-week, such as Sunday, and
day-in-month as first and second positional parameters, respectively.
TIMEZONE OFFSETS
ISO DATE and POSIX \$TZ offsets are supported throughout this script.
Environment \$TZ is read as POSIX offset when it holds a positive
or negative decimal number, such as _+03_ or _UTC+03_. POSIX time
zone definition by the \$TZ variable takes a different form from
ISO-8601 standards, so that ISO DATE UTC-03 is equivalent to setting
\$TZ=UTC+03.
Timezone names and IDs (e.g. \`America/Sao_Paulo') are supported
by \`C-code date' warping.
ENVIRONMENT
CFACTOR Lunar phase correction factor. Defaults=\"-1892\".
DATE_CMD \`C-code date' programme command. GNU, BSD, AST and
Busybox \`date' are supported.
TZ POSIX-style time zone offset.
REFINEMENT RULES
\`Compound time range' calculations may result slightly different
intervals due to refinement logics. This script tried to mimic
\`Hroptatyr's datediff' refinement rules as often as possible.
Error rate of the main code is estimated to be lower than one
percent after extensive testing with selected and corner-case
sample dates. Check source code and repository for details.
DIAGNOSTICS
Options -d and -dd execute result checks against \`C-code datediff'
and \`C-code date' programmes. Requires \`datediff.debug.sh'.
Option -D disables \`C-code date' warping and -DD disables Bash/
Ksh \`printf %()T' warping, too.
WARRANTY
Licensed under the GNU General Public License 3 or better. This
software is distributed without support or bug corrections.
Bash2.05b+, Ksh93 or Zsh is required. \`Bc' or Ksh is required
for single-unit calculations. FreeBSD12+ or GNU \`date' is
optionally required.
Many thanks for all advice from c.u.shell!
PROJECT SOURCE
<https://gitlab.com/fenixdragao/shelldatediff>
<https://github.com/mountaineerbr/shellDatediff>
EXAMPLES
Leap year check:
${0##*/} -l {1990..2000}
Moon phases for January or full year:
${0##*/} -m 1996-01
${0##*/} -m 1996
Print following Friday, 13th:
${0##*/} -F Fri 13 1999
${0##*/} -F sun 9 1999-02-01
Single-unit time periods:
${0##*/} 1970-01-01 2000-02-02 y #(y)ears
Date intervals / time ranges:
echo 1970-01-01 2000-02-02 | ${0##*/}
${0##*/} 2020-01-03T14:30:10 2020-12-24T00:00:00
TZ=UTC+03 ${0##*/} 2020-01-03T14:30:10-06 2021-12-30T21:00:10-03
\`GNU date':
${0##*/} 'next monday'
${0##*/} '5min 34seconds'
${0##*/} '2019/6/28' '1Aug'
${0##*/} '2020-01-01 - 6months' '2020-01-01'
${0##*/} @1561243015 @1592865415
\`BSD date':
${0##*/} -f'%m/%d/%Y' '6/28/2019' '9/04/1970 '
${0##*/} -r 1561243015 1592865415
${0##*/} -- '-v +2d' '-v -3w'
OPTIONS
Special Date Functions
-e Easter date (Western Church).
-ee Carnaval, Easter and C. Christi dates.
-F, -FF Following Friday the 13th dates.
-h This help page.
-l Leap year check.
-m Lunar phase calendar.
Date and Time Calculations
-[0-9] Scale factor for single-unit results.
-DDdd Debug, check documentation for details.
-f FMT Input time format spec (BSD \`date').
-R Output RFC-5322 format dates.
-r, -@ Input dates are UNIX times.
-t, -tt Table display of single-unit intervals.
-u Set UTC time instead of local time.
-vvv Change print layout, verbose levels."
# TESTING SCRIPTS
# The project is hosted at <https://github.com/mountaineerbr/shellDatediff>.
# Complementary code for testing and a lot more notes are hosted there.
#
# Error rate for the compound range calculation stabilises at about .6%
# of all tested dates. A little over half of the errors is due to
# `C-code datediff' printing results with more than 4 weeks (instead of
# it adding to months). All other differences occur with ``end-of-month
# vs start-of-month'' dates, such as days `29, 30 or 31' of one date
# against days `1, 2 or 3' of the other date.
#
# For example the following dates:
# ``1988-01-31T07:00:00Z vs 1988-12-05T11:00:00Z''
# which results differ as:
# sh = 0Y 2M 4W 3D 4H 0MIN 0S
# dd = 0y 3m 0w 0d 4h 0min 0s
#
# Because the start date is ``Dec 31st'', one can consider a full month
# count only at ``Apr 30th'' or `Apr 31st' which does not exist but is
# equivalent to ``May 1st''. These results are considered correct because
# of different refinement rules.
#globs
SEP='Tt/.:+-'
EPOCH=1970-01-01T00:00:00
GLOBOPT='@(y|mo|w|d|h|m|s|Y|MO|W|D|H|M|S)'
GLOBUTC='*(+|-)@(?([Uu])[Tt][Cc]|?([Uu])[Cc][Tt]|?([Gg])[Mm][Tt]|Z|z)' #see bug ``*?(exp)'' in bash2.05b extglob; [UG] are marked optional for another hack in this script
GLOBTZ="?($GLOBUTC)?(+|-)@(2[0-4]|?([01])[0-9])?(?(:?([0-5])[0-9]|:60)?(:?([0-5])[0-9]|:60)|?(?([0-5])[0-9]|60)?(?([0-5])[0-9]|60))"
GLOBDATE='?(+|-)+([0-9])[/.-]@(1[0-2]|?(0)[1-9])[/.-]@(3[01]|?(0)[1-9]|[12][0-9])'
GLOBTIME="@(2[0-4]|?([01])[0-9]):?(?([0-5])[0-9]|60)?(:?([0-5])[0-9]|:60)?($GLOBTZ)"
#https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s07.html
#custom support for 24h clock and leap second
DAY_OF_WEEK=(Thursday Friday Saturday Sunday Monday Tuesday Wednesday)
MONTH_OF_YEAR=(January February March April May June July August September October November December)
YEAR_MONTH_DAYS=(31 28 31 30 31 30 31 31 30 31 30 31)
TIME_ISO8601_FMT='%Y-%m-%dT%H:%M:%S%z'
TIME_RFC5322_FMT='%a, %d %b %Y %H:%M:%S %z'
TIME_ISO8601_FMT_PF='%04d-%02d-%02dT%02d:%02d:%02d%.1s%02d:%02d:%02d'
TIME_RFC5322_FMT_PF='%.3s, %02d %.3s %04d %02d:%02d:%02d %.1s%02d:%02d:%02d'
CFACTOR="${CFACTOR--1892}" #moon phase correction factor
# Choose between GNU/BSD/AST/BUSYBOX date
# datefun.sh [-u|-R|-v[val]|-I[fmt]] [YYYY-MM-DD|@UNIX] [+OUTPUT_FORMAT]
# By defaults, input should be ISO8601 date or UNIX time (append @).
# Option -I `fmt' may be `date', `hours', `minutes' or `seconds' (added in FreeBSD12).
# Setting environment TZ=UTC is equivalent to -u. Accepts only one opt (such as -uIseconds).
function datefun
{
typeset options input_fmt globtest ar chars start variable_iso
input_fmt="${INPUT_FMT:-${TIME_ISO8601_FMT%??}}"
[[ $1 = -[uRIv]* ]] && options="$1" && shift
#ISO8601 variable length
globtest="*([$IFS])@($GLOBDATE?([$SEP])?(+([$SEP])$GLOBTIME)|$GLOBTIME)?([$SEP])*([$IFS])"
if [[ $1 = $globtest && -z $OPTF && -n $ASTDATE$BSDDATE$BUSYDATE ]] #ISO8601 variable length
then ar=(${1//[$SEP]/ })
[[ ${1//[$IFS]} = +([0-9])[:]* ]] && start=9 || start=0
((chars=(${#ar[@]}*2)+(${#ar[@]}-1) , variable_iso=1))
fi
if ((BSDDATE))
then if [[ -z $1 ]]
then set --
elif ((variable_iso))
then ${DATE_CMD} ${options} -j -f "${TIME_ISO8601_FMT:$start:$chars}" "${@/$GLOBUTC}" && return
fi
if [[ ${1:-+%} != @(+%|@|-f)* && $1${OPTF+x} != +([0-9])?(.[0-9][0-9]) ]] #[[[[[cc]yy]mm]dd]HH]MM[.ss]
then set -- -f"${input_fmt}" "$@"
elif [[ $1 = @* ]]
then set -- "-r${1#@}" "${@:2}"
fi
${DATE_CMD} ${options} -j "$@"
elif ((ASTDATE))
then [[ $1 = @(+%|-f)* || $2 = @(+%|-f)* || $options = *-R* ]] ||
options="$options -f ${TIME_ISO8601_FMT}"
if [[ $1 = @* ]]
then options="${options} -p %s"
set -- "${@/@/}"
elif ((!OPTF)) && [[ $1 != @(+%|-f)* ]] && [[ $2 = @(+%|-f)* ]]
then options="$options -p ${TIME_ISO8601_FMT:${start:-0}:${chars:-19}}"
fi
options="${options/@(-I@(date|hours|minutes|seconds)|-I)}"
[[ ${1:-+%} != @(+%|-d)* ]] && set -- -d"${1}" "${@:2}"
${DATE_CMD} ${options} "$@"
else
[[ ${1:-+%} != @(+%|-d)* ]] && set -- -d"${1}" "${@:2}"
if ((BUSYDATE&&variable_iso))
then set -- -D"${TIME_ISO8601_FMT:$start:$chars}" "$@"
elif ((BUSYDATE&&OPTF))
then set -- -D"${input_fmt}" "$@"
fi
[[ $DATE_CMD = *toybox* && $options = *-R* ]] && { set -- "$@" +"$TIME_RFC5322_FMT" ;options="${options/-R}" ;}
${DATE_CMD} ${options} "$@"
fi
}
#leap fun
function is_leapyear
{
typeset year
set -- "${1:-0}";
((year=10#${1##[+-]}));
case "$1" in -*) year=-$year;; esac;
((!(year % 4) && (year % 100 || !(year % 400) ) ))
}
#print the maximum number of days of a given month
#usage: month_maxday [MONTH] [YEAR]
#MONTH range 1-12; YEAR cannot be nought.
function month_maxday
{
typeset month year
set -- "${1:-1}" "${2:-0}";
((month=10#${1}));
((year= 10#${2##[+-]}));
case "$2" in -*) year=-$year;; esac;
if ((month==2)) && is_leapyear $year
then echo 29
else echo ${YEAR_MONTH_DAYS[month-1]}
fi
}
#number of days in a year, count as leap year only
#if date1's month is before or at February.
function year_days_adj
{
typeset month year
set -- "${1:-1}" "${2:-0}";
((month=10#${1}));
((year= 10#${2##[+-]}));
case "$2" in -*) year=-$year;; esac;
if ((month<=2)) && is_leapyear $year
then echo 366
else echo 365
fi
}
#check if input is a pos/neg integer year
function is_year
{
if [[ $1 = ?(-)+([0-9]) ]]
then return 0
else printf "err: YEAR must be in the format \`[-]YYYY' -- %s\n" "$1" >&2
fi
return 1
}
#verbose check if year is leap
function is_leapyear_verbose
{
typeset year
year="$1"
if is_leapyear $year
then ((OPTVERBOSE)) || printf 'leap year -- %4s\n' $year
else ((OPTVERBOSE)) || printf 'not leap year -- %4s\n' $year
false
fi
}
#https://stackoverflow.com/questions/32196629/my-shell-script-for-checking-leap-year-is-showing-error
#Easter date in a given year
#usage: easterf [YEAR]
function easterf
{
echo ${1:?year required} '[ddsf[lfp[too early
]Pq]s@1583>@
ddd19%1+sg100/1+d3*4/12-sx8*5+25/5-sz5*4/lx-10-sdlg11*20+lz+lx-30%
d[30+]s@0>@d[[1+]s@lg11<@]s@25=@d[1+]s@24=@se44le-d[30+]s@21>@dld+7%-7+
[March ]smd[31-[April ]sm]s@31<@psnlmPpsn1z>p]splpx' | dc
}
#Dershowitz' and Reingold' Calendrical Calculations book
#Carnaval and Corpus Christi in a given year
#usage: carnavalf [YEAR]
function carnavalf
{
typeset year month day unix1 unixEaster unixCarnaval unixCorpus
{ read day
read month year
} < <(easterf "$@")
((${#day}==2)) || day=0$day
month=$(monthconv $month)
unixEaster=$(get_unixf $year-$month-$day)
((unixCarnaval=unixEaster-(47*24*60*60) ))
((unixCorpus=unixEaster+(60*24*60*60) ))
if ((OPTRR))
then
printf '%.16s\t%.16s\t%.16s\n' \
"$(unix_toiso -R $unixCarnaval)" \
"$(unix_toiso -R $unixEaster)" \
"$(unix_toiso -R $unixCorpus)"
else printf '%.10s\t%.10s\t%.10s\n' \
"$(unix_toiso $unixCarnaval)" \
"$year-${month:0:3}-$day" \
"$(unix_toiso $unixCorpus)"
fi
}
#https://www.inf.ufrgs.br/~cabral/Pascoa.html
#https://www.inf.ufrgs.br/~cabral/tabela_pascoa.html
#http://ghiorzi.org/portug2.htm
#get the month number
function monthconv
{
case "$1" in
[Dd]ec*) echo 12;; [Nn]ov*) echo 11;;
[Oo]ct*) echo 10;; [Ss]ep*) echo 09;;
[Aa]ug*) echo 08;; [Jj]ul*) echo 07;;
[Jj]un*) echo 06;; [Mm]ay*) echo 05;;
[Aa]pr*) echo 04;; [Mm]ar*) echo 03;;
[Ff]eb*) echo 02;; [Jj]an*) echo 01;;
12) echo December;; 11) echo November;;
10) echo October;; *9) echo September;;
*8) echo August;; *7) echo July;;
*6) echo June;; *5) echo May;;
*4) echo April;; *3) echo March;;
*2) echo February;; *1) echo January;;
esac
}
#get the name of day in the week
#usage: get_day_in_week unix_time
function get_day_in_week
{
typeset unix
set -- "${1:-0}";
((unix=10#${1##[+-]}));
case "$1" in -*) unix=-$unix;; esac;
echo ${DAY_OF_WEEK[( ( (unix+(unix<0?1:0))/(24*60*60))%7 +(unix<0?6:7))%7]}
}
#get the value of a day in the year
#usage: get_day_in_year year month day
function get_day_in_year
{
typeset day month year month_test daysum
set -- "${1:-1}" "${2:-1}" "${3:-0}";
((day= 10#${1}));
((month=10#${2}));
((year= 10#${3##[+-]}));
case "$3" in -*) year=-$year;; esac;
for ((month_test=1;month_test<month;++month_test))
do ((daysum+=${YEAR_MONTH_DAYS[month_test-1]}))
done
((month>2)) && is_leapyear $year && ((daysum++))
echo $((day+daysum))
}
#return phase of the moon, UTC time
#usage: phase_of_the_moon year [month] [day]
function phase_of_the_moon #0-7, with 0: new, 4: full
{
typeset day month year diy goldn epact
set -- "${1:-1}" "${2:-1}" "${3:-1970}";
((day= 10#${1}));
((month=10#${2}));
((year= 10#${3##[+-]}));
case "$3" in -*) year=-$year;; esac;
((year+=CFACTOR)) #correction factor: -1892
diy=$(get_day_in_year "$day" "$month" "$year")
((goldn = (year % 19) + 1))
((epact = (11 * goldn + 18) % 30))
(((epact == 25 && goldn > 11) || epact == 24 )) && ((epact++))
case $(( ( ( ( ( (diy + epact) * 6) + 11) % 177) / 22) & 7)) in
0) set -- 'New Moon' ;; # ~.0
1) set -- 'Waxing Crescent' ;;
2) set -- 'First Quarter' ;; # ~.25
3) set -- 'Waxing Gibbous' ;;
4) set -- 'Full Moon' ;; # ~.5
5) set -- 'Waning Gibbous' ;;
6) set -- 'Last Quarter' ;; # ~.75
7) set -- 'Waning Crescent' ;;
esac
#Bash's integer division truncates towards zero as in C
case "$*" in "$PHASE_SKIP") return;; *) PHASE_SKIP="$*";; esac;
if ((OPTVERBOSE))
then printf '%s\n' "$*"
else printf '%04d-%02d-%02d %s\n' "$((year-CFACTOR))" "$month" "$day" "$*"
fi
}
#<https://nethack.org/>
#<https://aa.usno.navy.mil/data/MoonPhases>
#<https://aa.usno.navy.mil/faq/moon_phases>
#<http://astropixels.com/ephemeris/phasescat/phases1901.html>
#<https://www.nora.ai/competition/fishai-dataset-competition/about-the-dataset/>
#<https://www.kaggle.com/datasets/lsind18/full-moon-calendar-1900-2050>
#<https://www.fullmoon.info/en/fullmoon-calendar_1900-2050.html>
#Correction Factor was calculated comparing with USNO Navy data.
#also see explanation in Dershowitz and Reingold's: 8.1 Orthodox Easter
#print (current) time
#usage: get_timef [unix_time] [print_format]
function get_timef
{
typeset input fmt
input=${1#@} fmt="${2:-${TIME_ISO8601_FMT}}"
if ((OPTDD))
then echo $EPOCH ;false
elif [[ -n $ZSH_VERSION ]]
then zmodload -aF zsh/datetime b:strftime && strftime "$fmt" $input
elif [[ -n $BASH_VERSION ]]
then printf "%(${fmt})T\n" ${input:--1}
else printf "%(${fmt})T\n" ${input:+$(unix_toiso $input)}
fi
#zsh and bash accept unix time input, ksh accepts iso8601
}
#generate unix time arithmetically
#usage: get_unixf iso-8601_date
function get_unixf
{
GETUNIX=1 OPTVERBOSE=1 DATE_CMD=false OPTRR= TZ= mainf "$EPOCH" "$1"
}
#generate ISO8601 or RFC5322 from UNIX time
#usage: unix_toiso [-R] UNIX [+1|-1] [tzXh] [$tzXm] [tzXs] [+1|-1] [TZh] [$TZm] [TZs]
function unix_toiso
{
typeset unix unix_adj y_test mo_test d_test max_mday max_yday daysum optr neg_tz tzh tzm tzs TZ_neg TZ_pos TZh TZm TZs noTZs
[[ $1 = -R ]] && { optr=1 ;shift ;}
((unix=10#0${1##[+-]}));
case "$1" in -*) unix=-$unix;; esac;
neg_tz=${2:--1} tzh=$3 tzm=$4 tzs=$5
TZ_neg=${6:--1} TZh=$7 TZm=$8 TZs=$9
TZ_pos=${TZ_neg/-/+} TZ_pos=${TZ_pos##$TZ_neg} TZ_pos=${TZ_pos:-${TZ_neg/+/-}}
((unix+=( ( (tzh*60*60)+(tzm*60)+tzs)*neg_tz)-( ( (TZh*60*60)+(TZm*60)+TZs)*TZ_neg) ))
unix_adj=$unix
if ((unix<0))
then y_test=1969 mo_test=12 d_test=31
max_mday=31 max_yday=365
while ((unix<-max_yday*24*60*60))
do ((daysum+=max_yday, unix+=max_yday*24*60*60))
((--y_test))
max_yday=$(year_days_adj 1 $y_test)
done
while ((unix<-max_mday*24*60*60))
do ((daysum+=max_mday, unix+=max_mday*24*60*60))
((mo_test==1)) && ((--y_test))
((mo_test==1)) && mo_test=12 || ((--mo_test))
max_mday=$(month_maxday $mo_test $y_test)
done
((d_test=max_mday+( (unix+1)/(24*60*60) ) , daysum-=unix/(24*60*60) , unix%=24*60*60))
((unix)) && ((h_test=23+( (unix+1)/(60*60) ) , unix%=60*60))
((unix)) && ((m_test=59+( (unix+1)/60) , unix%=60))
((unix)) && ((s_test=60+unix))
else
y_test=1970 mo_test=1 d_test=1
max_mday=31 max_yday=365
while ((unix>=max_yday*24*60*60))
do ((daysum+=max_yday, unix-=max_yday*24*60*60))
((++y_test))
max_yday=$(year_days_adj 1 $y_test)
done
while ((unix>=max_mday*24*60*60))
do ((mo_test==12)) && ((++y_test))
((mo_test==12)) && mo_test=1 || ((++mo_test))
((daysum+=max_mday, unix-=max_mday*24*60*60))
max_mday=$(month_maxday $mo_test $y_test)
done
((d_test+=unix/(24*60*60) , daysum+=unix/(24*60*60) , unix%=24*60*60))
((h_test=unix/(60*60) , unix%=60*60))
((m_test=unix/60 , unix%=60))
((s_test=unix))
fi
((TZs)) || noTZs='?????'
if ((optr))
then printf "${TIME_RFC5322_FMT_PF%$noTZs}\n" \
"$(get_day_in_week $unix_adj)" \
"${d_test}" "$(monthconv $mo_test)" "$y_test" \
"$h_test" "$m_test" "$s_test" \
"$TZ_pos" "$TZh" "$TZm" ${TZs#0}
else
printf "${TIME_ISO8601_FMT_PF%$noTZs}\n" \
"$y_test" "$mo_test" "${d_test}" \
"$h_test" "$m_test" "$s_test" \
"$TZ_pos" "$TZh" "$TZm" ${TZs#0}
fi
}
#get friday 13th dates
#usage: friday_13th [weekday_name] [day] [start_year]
function friday_13th
{
typeset glob1 glob2 dow_name d_tgt diw_tgt day month year unix diw d_away maxday skip n
dow_name=("${DAY_OF_WEEK[@]}") ;DAY_OF_WEEK=(0 1 2 3 4 5 6)
glob1='[SsMmTtWwFf]*' glob2='?([0-3])[0-9]'
#set day of week and day of month
[[ $2 = $glob1 ]] && set -- "${@:2:1}" "${@:1:1}" "${@:3}"
if [[ $1 = $glob1 ]]
then case $1 in
[Ss][Aa]*) diw_tgt=${DAY_OF_WEEK[2]};;
[Ff]*) diw_tgt=${DAY_OF_WEEK[1]};;
[Tt][Hh]*) diw_tgt=${DAY_OF_WEEK[0]};;
[Ww]*) diw_tgt=${DAY_OF_WEEK[6]};;
[Tt]*) diw_tgt=${DAY_OF_WEEK[5]};;
[Mm]*) diw_tgt=${DAY_OF_WEEK[4]};;
[Ss]*) diw_tgt=${DAY_OF_WEEK[3]};;
esac ;shift
fi ;diw_tgt=${diw_tgt:-1}
[[ $1 = $glob2 ]] && { d_tgt=$1 && shift ;} || d_tgt=13
IFS="$IFS$SEP" ;set -- $@ ;(($#)) || set -- $(IFS=$' \t\n' get_timef) ;IFS=$' \t\n'
day="${3#0}" month="${2#0}" year="${1##*(0)}"
day="${day:-1}" month="${month:-1}" year="${year:-0}"
unix=$(datefun ${year}-${month}-${day} +%s) ||
unix=$(get_unixf ${year}-${month}-${day}) || return $?
while diw=$(get_day_in_week $((unix+(d_away*24*60*60) )) )
do if ((diw==diw_tgt && day==d_tgt))
then if ((!(d_away+OPTVERBOSE+OPTFF-1) ))
then printf "${TIME_RFC5322_FMT_PF:0:20} is today!\n" \
"${dow_name[diw_tgt]}" "$day" "${MONTH_OF_YEAR[month-1]}" "$year"
elif ((OPTVERBOSE))
then printf "${TIME_ISO8601_FMT_PF:0:14}\n" "$year" "$month" "$day"
else printf "${TIME_RFC5322_FMT_PF:0:20} is %4d days away\n" \
"${dow_name[diw_tgt]}" "$day" "${MONTH_OF_YEAR[month-1]}" "$year" "$d_away"
fi
((++n))
((OPTFF==1||(OPTFF==2&&n>=10) )) && break
fi
if ((day<d_tgt)) #days away
then ((d_away=d_tgt-day, day=d_tgt, skip=1))
elif maxday=$(month_maxday $month $year)
((day>d_tgt))
then ((d_away=(maxday-day+d_tgt), day=d_tgt))
else ((d_away+=maxday))
fi
if ((!skip))
then ((month==12)) && ((++year))
((month=(month==12?1:month+1) ))
fi ;skip=
done
}
#printing helper
#(A). check if floating point in $1 is `>0', set return signal and $SS to `s' when `>1.0'.
#usage: prHelpf 1.23
#(B). set padding of $1 length until [max] chars and set $SSS.
#usage: prHelpf 1.23 [max]
function prHelpf
{
typeset val valx int dec x z
#(B)
if (($# >1))
then SSS= x=$(( ${2} - ${#1} ))
for ((z=0;z<x;++z))
do SSS="$SSS "
done
fi
#(A)
SS= val=${1#-} val=${val#0} valx=${val//[0.]} int=${val%.*}
[[ $val = *.* ]] && dec=${val#*.} dec=${dec//0}
[[ -n $1 && -n $OPTT ]] || ((valx)) || return
(( int>1 || ( (int==1) && (dec) ) )) && SS=s
return 0
}
#datediff fun
function mainf
{
${DEBUG:+unset} \
typeset date1_iso8601 date2_iso8601 unix1 unix2 inputA inputB range neg_range yearA monthA dayA hourA minA secA tzA neg_tzA tzAh tzAm tzAs yearB monthB dayB hourB minB secB tzB neg_tzB tzBh tzBm tzBs years_between y_test leapcount daycount_leap_years daycount_years fullmonth_days fullmonth_days_save monthcount month_test month_tgt d1_mmd d2_mmd date1_month_max_day date3_month_max_day date1_year_days_adj d_left y mo w d h m s bc bcy bcmo bcw bcd bch bcm range_pr sh d_left_save d_sum date1_iso8601_pr date2_iso8601_pr yearAtz monthAtz dayAtz hourAtz minAtz secAtz yearBtz monthBtz dayBtz hourBtz minBtz secBtz range_check now globtest varname buf var ok ar ret n p q r v TZh TZm TZs TZ_neg TZ_pos spcr #SS SSS
(($# == 1)) && set -- '' "$1"
#warp `date' when available
if unix1=$(datefun "${1:-+%s}" ${1:++%s}) &&
unix2=$(datefun "${2:-+%s}" ${2:++%s})
then #sort dates
if ((unix1 > unix2))
then buf=$unix2 unix2=$unix1 unix1=$buf neg_range=-1
set -- "$2" "$1" "${@:3}"
fi
{
date1_iso8601=$(datefun -Iseconds @"$unix1")
date2_iso8601=$(datefun -Iseconds @"$unix2")
if [[ -z $OPTVERBOSE && -n $OPTRR ]]
then date1_iso8601_pr=$(datefun -R @"$unix1")
date2_iso8601_pr=$(datefun -R @"$unix2")
fi
} 2>/dev/null #avoid printing errs from FreeBSD<12 `date'
else unset unix1 unix2
#set default date -- AD
[[ -z $1 || -z $2 ]] && now=$(get_timef)
[[ -z $1 ]] && { set -- "${now}" "${@:2}" ;date1_iso8601="$now" ;}
[[ -z $2 ]] && { set -- "$1" "${now}" "${@:3}" ;date2_iso8601="$now" ;}
fi
#load ISO8601 dates from `date' or user input
inputA="${date1_iso8601:-$1}" inputB="${date2_iso8601:-$2}"
if [[ -z $unix2 ]] #time only input, no `date' pkg available
then [[ $inputA = *([0-9]):* ]] && inputA="${EPOCH:0:10}T${inputA}"
[[ $inputB = *([0-9]):* ]] && inputB="${EPOCH:0:10}T${inputB}"
fi
IFS="${IFS}${SEP}UuGgZz" read yearA monthA dayA hourA minA secA tzA <<<"${inputA##*(+|-)}"
IFS="${IFS}${SEP}UuGgZz" read yearB monthB dayB hourB minB secB tzB <<<"${inputB##*(+|-)}"
IFS="${IFS}${SEP/[Tt]}" read tzAh tzAm tzAs var <<<"${tzA##?($GLOBUTC?(+|-)|[+-])}"
IFS="${IFS}${SEP/[Tt]}" read tzBh tzBm tzBs var <<<"${tzB##?($GLOBUTC?(+|-)|[+-])}"
IFS="${IFS}${SEP/[Tt]}" read TZh TZm TZs var <<<"${TZ##?($GLOBUTC?(+|-)|[+-])}"
#fill in some defaults
monthA=${monthA:-1} dayA=${dayA:-1} monthB=${monthB:-1} dayB=${dayB:-1}
#support offset as `[+-]XXXX??'
globtest='[0-9][0-9][0-9][0-9]?([0-9][0-9])'
[[ $tzAh = $globtest ]] && tzAs=${tzAh:4:2} tzAm=${tzAh:2:2} tzAh=${tzAh:0:2}
[[ $tzBh = $globtest ]] && tzBs=${tzBh:4:2} tzBm=${tzBh:2:2} tzBh=${tzBh:0:2}
[[ ${TZh} = $globtest ]] && TZs=${TZh:4:2} TZm=${TZh:2:2} TZh=${TZh:0:2}
#set parameters as decimals ASAP
for varname in yearA monthA dayA hourA minA secA \
yearB monthB dayB hourB minB secB \
tzAh tzAm tzAs tzBh tzBm tzBs TZh TZm TZs
do eval "[[ \${$varname} = *[A-Za-z_]* ]] && continue" #avoid printing errs
eval "(($varname=10#0\${$varname##[+-]}))";
done
((yearA<40000)) || echo "warning: ${yearA}: YEAR" >&2; #slow
((yearB<40000)) || echo "warning: ${yearB}: YEAR" >&2;
#negative years
case "$inputA" in -?*) yearA=-$yearA;; esac;
case "$inputB" in -?*) yearB=-$yearB;; esac;
#
#iso8601 date string offset
[[ ${inputA%"${tzA##?($GLOBUTC?(+|-)|[+-])}"} = *?+ ]] && neg_tzA=+1 || neg_tzA=-1
[[ ${inputB%"${tzB##?($GLOBUTC?(+|-)|[+-])}"} = *?+ ]] && neg_tzB=+1 || neg_tzB=-1
((tzAh==0 && tzAm==0 && tzAs==0)) && neg_tzA=+1
((tzBh==0 && tzBm==0 && tzBs==0)) && neg_tzB=+1
#
#environment $TZ
[[ ${TZ##*$GLOBUTC} = +?* ]] && TZ_neg=+1 TZ_pos=-1 || TZ_neg=-1 TZ_pos=+1
[[ $TZh$TZm$TZs = *([0-9+-]) && -z $unix2 ]] || unset TZh TZm TZs
#24h clock and input leap second support (these $tz* parameters will be zeroed later)
((hourA==24)) && (( (neg_tzA>0 ? (tzAh-=hourA-23) : (tzAh+=hourA-23) ) , (hourA-=hourA-23) ))
((hourB==24)) && (( (neg_tzB>0 ? (tzBh-=hourB-23) : (tzBh+=hourB-23) ) , (hourB-=hourB-23) ))
((minA==60)) && (( (neg_tzA>0 ? (tzAm-=minA-59) : (tzAm+=minA-59) ) , (minA-=minA-59) ))
((minB==60)) && (( (neg_tzB>0 ? (tzBm-=minB-59) : (tzBm+=minB-59) ) , (minB-=minB-59) ))
((secA==60)) && (( (neg_tzA>0 ? (tzAs-=secA-59) : (tzAs+=secA-59) ) , (secA-=secA-59) ))
((secB==60)) && (( (neg_tzB>0 ? (tzBs-=secB-59) : (tzBs+=secB-59) ) , (secB-=secB-59) ))
#CHECK SCRIPT `GLOBS', TOO, as they may fail with weyrd dates and formats.
#check input validity
d1_mmd=$(month_maxday "$monthA" "$yearA") ;d2_mmd=$(month_maxday "$monthB" "$yearB")
if ! (( (yearA||yearA==0) && (yearB||yearB==0) && monthA && monthB && dayA && dayB )) ||
((
monthA>12 || monthB>12 || dayA>d1_mmd || dayB>d2_mmd
|| hourA>23 || hourB>23 || minA>59 || minB>59 || secA>59 || secB>59
))
then echo "err: illegal user input -- ISO-8601 DATE or UNIX TIME required" >&2;
return 2;
fi
#offset and $TZ support
if ((tzAh||tzAm||tzAs||tzBh||tzBm||tzBs||TZh||TZm||TZs))
then #check validity
if ((tzAh>24||tzBh>24||tzAm>60||tzBm>60||tzAs>60||tzBs>60))
then echo "warning: illegal offsets" >&2
unset tzA tzB tzAh tzAm tzAs tzBh tzBm tzBs
fi
if ((TZh>23||TZm>59||TZs>59))
then echo "warning: illegal environment \$TZ" >&2
unset TZh TZm TZs
fi #offset specs:
#<https://www.w3.org/TR/NOTE-datetime>
#<https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html>
#environment $TZ support #only for printing
if ((!OPTVERBOSE)) && ((TZh||TZm||TZs))
then [[ -z $tzA ]] && ((tzAh-=(TZh*TZ_neg), tzAm-=(TZm*TZ_neg), tzAs-=(TZs*TZ_neg) ))
[[ -z $tzB ]] && ((tzBh-=(TZh*TZ_neg), tzBm-=(TZm*TZ_neg), tzBs-=(TZs*TZ_neg) ))
else unset TZh TZm TZs
fi
#convert dates to UTC for internal range calculations
((tzAh||tzAm||tzAs)) && var="A" || var=""
((tzBh||tzBm||tzBs)) && var="$var B"
for v in $var #A B
do #secAtz secBtz
((sec${v}tz+=sec${v}-(tz${v}s*neg_tz${v}) )) #neg_tzA neg_tzB
if ((sec${v}tz<0))
then ((min${v}tz+=((sec${v}tz-59)/60) , sec${v}tz=(sec${v}tz%60+60)%60))
elif ((sec${v}tz>59))
then ((min${v}tz+=(sec${v}tz/60) , sec${v}tz%=60))
fi
#minAtz minBtz
((min${v}tz+=min${v}-(tz${v}m*neg_tz${v}) ))
if ((min${v}tz<0))
then ((hour${v}tz+=((min${v}tz-59)/60) , min${v}tz=(min${v}tz%60+60)%60))
elif ((min${v}tz>59))
then ((hour${v}tz+=(min${v}tz/60) , min${v}tz%=60))
fi
#hourAtz hourBtz
((hour${v}tz+=hour${v}-(tz${v}h*neg_tz${v}) ))
if ((hour${v}tz<0))
then ((day${v}tz+=((hour${v}tz-23)/24) , hour${v}tz=(hour${v}tz%24+24)%24))
elif ((hour${v}tz>23))
then ((day${v}tz+=(hour${v}tz/24) , hour${v}tz%=24))
fi
#dayAtz dayBtz
((day${v}tz+=day${v}))
if ((day${v}tz<1))
then var=$(month_maxday "$((month${v}==1 ? 12 : month${v}-1))" "$((year${v}))")
((day${v}tz+=var))
if ((month${v}>1))
then ((--month${v}tz))
else ((month${v}tz-=month${v}))
fi
elif var=$(month_maxday "$((month${v}))" "$((year${v}))")
((day${v}tz>var))
then ((++month${v}tz))
((day${v}tz%=var))
fi
#monthAtz monthBtz
((month${v}tz+=month${v}))
if ((month${v}tz<1))
then ((--year${v}tz))
((month${v}tz+=12))
elif ((month${v}tz>12))
then ((++year${v}tz))
((month${v}tz%=12))
fi
((year${v}tz+=year${v})) #yearAtz yearBtz
done
#modulus as (a%b + b)%b to avoid negative remainder.
#<https://www.geeksforgeeks.org/modulus-on-negative-numbers/>
if [[ -n $yearAtz ]]
then (( yearA=yearAtz , monthA=monthAtz , dayA=dayAtz,
hourA=hourAtz , minA=minAtz , secA=secAtz ,
tzAh=0 , tzAm=0 , tzAs=0
))
fi
if [[ -n $yearBtz ]]
then (( yearB=yearBtz , monthB=monthBtz , dayB=dayBtz,
hourB=hourBtz , minB=minBtz , secB=secBtz ,
tzBh=0 , tzBm=0 , tzBs=0
))
fi
elif [[ -z $unix2$OPTVERBOSE && $tzA$tzB$TZ = *+([A-Za-z_])* ]]
then #echo "warning: input DATE or \$TZ contains timezone ID or name. Support requires package \`date'" >&2
unset tzA tzB tzAh tzBh tzAm tzBm tzAs tzBs TZh TZm TZs
else unset tzA tzB tzAh tzBh tzAm tzBm tzAs tzBs TZh TZm TZs
fi #Offset is *from* UTC, while $TZ is *to* UTC.
#sort `UTC' dates (if no `date' package)
if [[ -z $unix2 ]] && ((
(yearA>yearB)
|| ( (yearA==yearB) && (monthA>monthB) )
|| ( (yearA==yearB) && (monthA==monthB) && (dayA>dayB) )
|| ( (yearA==yearB) && (monthA==monthB) && (dayA==dayB) && (hourA>hourB) )
|| ( (yearA==yearB) && (monthA==monthB) && (dayA==dayB) && (hourA==hourB) && (minA>minB) )
|| ( (yearA==yearB) && (monthA==monthB) && (dayA==dayB) && (hourA==hourB) && (minA==minB) && (secA>secB) )
))
then neg_range=-1
for varname in inputA yearA monthA dayA hourA minA secA \
yearAtz monthAtz dayAtz hourAtz minAtz secAtz \
tzA tzAh tzAm tzAs neg_tzA date1_iso8601 date1_iso8601_pr UNIX1
do #swap $varA/$varB or $var1/$var2 values
[[ $varname = *A* ]] && p=A q=B || p=1 q=2
eval "buf=\"\$$varname\""
eval "$varname=\"\$${varname/$p/$q}\" ${varname/$p/$q}=\"\$buf\""
done
unset varname p q
set -- "$2" "$1" "${@:3}"
fi
##Count leap years and sum leap and non leap years days,
for ((y_test=(yearA+1);y_test<yearB;++y_test))
do
#((y_test==0)) && continue #ISO8601 counts year zero, proleptic gregorian/julian do not
is_leapyear $y_test && ((++leapcount))
((++years_between))
((monthcount += 12))
done
##count days in non and leap years
(( daycount_leap_years = (366 * leapcount) ))
(( daycount_years = (365 * (years_between - leapcount) ) ))
#date2 days so far this year (this month)
#days in prior months `this' year
((month_tgt = (yearA==yearB ? monthA : 0) ))
for ((month_test=(monthB-1);month_test>month_tgt;--month_test))
do
if ((month_test==2)) && is_leapyear $yearB
then (( fullmonth_days += 29 ))
else (( fullmonth_days += ${YEAR_MONTH_DAYS[month_test-1]} ))
fi
((++monthcount))
done
#date1 days until end of `that' year
#days in prior months `that' year
((yearA==yearB)) ||
for ((month_test=(monthA+1);month_test<13;++month_test))
do
if ((month_test==2)) && is_leapyear $yearA
then (( fullmonth_days += 29 ))
else (( fullmonth_days += ${YEAR_MONTH_DAYS[month_test-1]} ))
fi
((++monthcount))
done
((fullmonth_days_save = fullmonth_days))
#some info about input dates and their context..
date3_month_max_day=$(month_maxday "$((monthB==1 ? 12 : monthB-1))" "$yearB")
date1_month_max_day=$(month_maxday "$monthA" "$yearA")
date1_year_days_adj=$(year_days_adj "$monthA" "$yearA")
#set years and months
(( y = years_between ))
(( mo = ( monthcount - ( (years_between) ? (years_between * 12) : 0) ) ))
#days left
if ((yearA==yearB && monthA==monthB))
then
((d_left = (dayB - dayA) ))
((d_left_save = d_left))
elif ((dayA<dayB))
then
((++mo))
((fullmonth_days += date1_month_max_day))
((d_left = (dayB - dayA) ))
((d_left_save = d_left))
elif ((dayA>dayB))
then #refinement rules (or hacks)
((d_left = ( (date3_month_max_day>=dayA) ? (date3_month_max_day-dayA) : (date1_month_max_day-dayA) ) + dayB ))
((d_left_save = (date1_month_max_day-dayA) + dayB ))
if ((dayA>date3_month_max_day && date3_month_max_day<date1_month_max_day && dayB>1))
then
((dayB>=dayA-date3_month_max_day)) && ##addon2 -- prevents negative days
((d_left -= date1_month_max_day-date3_month_max_day))
((d_left==0 && ( (24-hourA)+hourB<24 || ( (24-hourA)+hourB==24 && (60-minA)+minB<60 ) || ( (24-hourA)+hourB==24 && (60-minA)+minB==60 && (60-secA)+secB<60 ) ) && (++d_left) )) ##addon3 -- prevents breaking down a full month
if ((d_left < 0))
then if ((w))
then ((--w , d_left+=7))
elif ((mo))
then ((--mo , w=date3_month_max_day/7 , d_left+=date3_month_max_day%7))
elif ((y))
then ((--y , mo+=11 , w=date3_month_max_day/7 , d_left+=date3_month_max_day%7))
fi
fi
elif ((dayA>date3_month_max_day)) #dayB==1
then
((d_left = (date1_month_max_day - dayA + date3_month_max_day + dayB) ))
((w = d_left/7 , d_left%=7))
if ((mo))
then ((--mo))
elif ((y))
then ((--y , mo+=11))
fi
fi
else #`dayA' equals `dayB'
((++mo))
((fullmonth_days += date1_month_max_day))
#((d_left_save = d_left)) #set to 0
fi
((h += (24-hourA)+hourB))
if ((h && h<24))
then if ((d_left))
then ((--d_left , ++ok))
elif ((mo))
then ((--mo , d_left+=date3_month_max_day-1 , ++ok))
elif ((y))
then ((--y , mo+=11 , d_left+=date3_month_max_day-1 , ++ok))
fi
fi
((h %= 24))
((m += (60-minA)+minB))
if ((m && m<60))
then if ((h))