aboutsummaryrefslogtreecommitdiff
path: root/challenge-132/abigail/awk/ch-1.awk
blob: 59a0d20f401449a176e1102a16ca95c06ec31694 (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
#!/usr/bin/awk

#
# See ../README.md
#

#
# Run as: awk -f ch-1.awk < input-file
#

#
# Conversion functions from Wikipedia
#

function g2j (Y, M, D) {
    return (int ((1461 * (Y +    4800 + int ((M - 14) / 12))) /   4) +        \
            int ((367 * (M - 2 -   12 * int ((M - 14) / 12))) /  12) -        \
            int ((3 * int (((Y + 4900 + int ((M - 14) / 12))  / 100))) / 4) + \
            D - 32075)
}

function j2g (J) {
    e  = 4 * (J + 1401 + int (int ((4 * J + 274277) / 146097) * 3 / 4) - 38) + 3
    D =   int (((5 * (int ((e % 1461) / 4)) + 2) % 153) / 5) + 1
    M = ((int  ((5 * (int ((e % 1461) / 4)) + 2) / 153) + 2) % 12) + 1
    Y = int (e / 1461) - 4716 + int ((12 + 2 - M) / 12)
    return sprintf ("%04d/%02d/%02d", Y, M, D)
}

BEGIN {
    FS = "/"
    TODAY_Y = 2021
    TODAY_M =    9
    TODAY_D =   22

    julian_today = g2j(TODAY_Y, TODAY_M, TODAY_D)
}

{
    julian_then = g2j($1, $2, $3)
    print j2g(2 * julian_then  - julian_today) ", " \
          j2g(2 * julian_today - julian_then)
}