I'll use YYYY-MM-DD notation.
You were born 1983-02-06 07:00.
Your friend was born 1984-02-07 01:00.
There was no leap day between your birthdays, which makes the calculation a little simpler. The most recent leap day before your birthday was 1980-02-29; the earliest leap day after your friend's birthday was 1984-02-29. (No, 1982 was not a leap year, and even if it had been that leap day still wouldn't have been between your birthdays).
The moment exactly one calendar year after your birthday was 1984-02-06 07:00; that calendar year is exactly 365 days. Your friend was born 1 day minus 6 hours after that moment. So the difference between your births is 365 days plus 1 day minus 6 hours, or 365 days, 18 hours (365.75 days).
And here's a bash script that computes the results:
#!/bin/bash
date0='1983-02-06 07:00'
date1='1984-02-07 01:00'
# Compute date as seconds since 1970-01-01 00:00:00 UTC
seconds0=$(date +%s -d "$date0")
seconds1=$(date +%s -d "$date1")
# Do some math. Note that this does only integer arithmetic.
# It happens to work in this case since we're dealing with whole
# hours, but this is not a general solution.
(( seconds = seconds1 - seconds0 ))
(( days = seconds / 86400 ))
(( hours = seconds % 86400 / 3600 ))
# Show the results.
echo "$date0 = $seconds0"
echo "$date1 = $seconds1"
echo "The dates differ by $seconds seconds or $days days and $hours hours"
The output is:
1983-02-06 07:00 = 413391600
1984-02-07 01:00 = 444992400
The dates differ by 31600800 seconds or 365 days and 18 hours
No comments:
Post a Comment