[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: difference in seconds between two formatted dates ...



On Mon 18 Dec 2023 at 06:02:48 (+0000), Albretch Mueller wrote:
> On 12/18/23, David Wright <deblis@lionunicorn.co.uk> wrote:
> > Another problem in what you posted is that you sometimes run date
> > in your local timezone (generally for the "now" times), but you
> > append +00:00 as the timezone for those --date strings that you
> > construct from several substrings. You need to use UT throughout.
> 
>  What difference would that make when all I need is a time difference?
> The way I understand such time format issues is that it needs to just
> be the same in the two dates.
> 
> >> ANyway, my hack around it wasn't effortful at
> >> all.
> >
> > I don't know whether you're referring to something already posted,
> > or some new script written in the wake of what you've read here.
> 
>  That silly hack which lousy bash script I posted within its test case

OK, I tried running it (attached). What should it show?

Cheers,
David.
# date --help
set -x
date --version 2>&1 | head -n 1

s00=$(date +%s)
dt00=$(date +%Y%m%d%H%M%S)

dttm00ISeks="${dt00:0:4}-${dt00:4:2}-${dt00:6:2}T${dt00:8:2}:${dt00:10:2}:${dt00:12:2}+00:00"
ISeks00=$(date -d"${dttm00ISeks}" +%s)

if [[ $s00 -eq $ISeks00 ]]; then
    echo "// __ \$ISeks00: |$ISeks00|, \$dttm00ISeks: |${dttm00ISeks}| conversion OK!"
else
    echo "// __ \$s00: |$s00|, \$dt00: |${dt00}|"
    echo "// __ \$ISeks00: |$ISeks00|, \$dttm00ISeks: |${dttm00ISeks}| conversion NOT OK!"
fi

# 63 weeks 5 days 11 hours 0 minutes et 43 seconds later
s02=$(( s00 + 63*7*24*60*60 + 5*24*60*60 + 11*60*60 + 0*60 + 43))

dt02=$(date --date @${s02} +%Y%m%d%H%M%S)
echo "// __ \$s02: |$s02|, \$dt02: |${dt02}|"

dttm02ISeks="${dt02:0:4}-${dt02:4:2}-${dt02:6:2}T${dt02:8:2}:${dt02:10:2}:${dt02:12:2}+00:00"
ISeks02=$(date -d"${dttm02ISeks}" +%s)

if [[ $s02 -eq $ISeks02 ]]; then
    echo "// __ \$ISeks02: |$ISeks02|, \$dttm02ISeks: |${dttm02ISeks}| conversion OK!"
    _diffs=$(( s02 - s00 ))
    _diffISeks=$(( ISeks02 - ISeks00 ))
    if [[ $_diffs -eq $_diffISeks ]]; then
        echo "// __ differences also OK!"
    else
        echo "// __  differences NOT OK! \$_diffs: |$_diffs|, \$_diffISeks: |${_diffISeks}|"
    fi
else
    echo "// __ \$s02: |$s02|, \$dt02: |${dt02}|"
    echo "// __ \$ISeks02: |$ISeks02|, \$dttm02ISeks: |${dttm02ISeks}| conversion NOT OK!"
fi

set +x
date (GNU coreutils) 8.32
// __ $s00: |1702924186|, $dt00: |20231218122946|
// __ $ISeks00: |1702902586|, $dttm00ISeks: |2023-12-18T12:29:46+00:00| conversion NOT OK!
// __ $s02: |1741498229|, $dt02: |20250308233029|
// __ $s02: |1741498229|, $dt02: |20250308233029|
// __ $ISeks02: |1741476629|, $dttm02ISeks: |2025-03-08T23:30:29+00:00| conversion NOT OK!
+ date --version
+ head -n 1
++ date +%s
+ s00=1702924186
++ date +%Y%m%d%H%M%S
+ dt00=20231218122946
+ dttm00ISeks=2023-12-18T12:29:46+00:00
++ date -d2023-12-18T12:29:46+00:00 +%s
+ ISeks00=1702902586
+ [[ 1702924186 -eq 1702902586 ]]
+ echo '// __ $s00: |1702924186|, $dt00: |20231218122946|'
+ echo '// __ $ISeks00: |1702902586|, $dttm00ISeks: |2023-12-18T12:29:46+00:00| conversion NOT OK!'
+ s02=1741498229
++ date --date @1741498229 +%Y%m%d%H%M%S
+ dt02=20250308233029
+ echo '// __ $s02: |1741498229|, $dt02: |20250308233029|'
+ dttm02ISeks=2025-03-08T23:30:29+00:00
++ date -d2025-03-08T23:30:29+00:00 +%s
+ ISeks02=1741476629
+ [[ 1741498229 -eq 1741476629 ]]
+ echo '// __ $s02: |1741498229|, $dt02: |20250308233029|'
+ echo '// __ $ISeks02: |1741476629|, $dttm02ISeks: |2025-03-08T23:30:29+00:00| conversion NOT OK!'
+ set +x

Reply to: