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

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



On 12/17/23, tomas@tuxteam.de <tomas@tuxteam.de> wrote:
> On Sun, Dec 17, 2023 at 10:12:11AM +0000, Albretch Mueller wrote:
>> dt00=$(date +%Y%m%d%H%M%S)
>> echo "// __ \$dt00: |${dt00}|"
>>
>> ... after some long processing for which seconds would be exact
>> enough, then I would like to get the seconds elapsed since dt00, like
>> this:
>>
>> dt02=$(date +%Y%m%d%H%M%S)
>> echo "// __ \$dt02: |${dt02}|"
>
> [...]
>>  because bash Arithmetic is 10-based.
>
> You wouldn't expect bash to intuit such a crooky arithmetic as
> Gregorian datetime (and then, based on what? The number beginning
> with 2023? Sounds kind of exciting).

 Actually, my basic idea is if you can encode a date using formatting
options this utility provides, then you should be able to decode it
using the same options that same utility provides, no? ... and then
get "seconds since 1970-01-01" which wouldn't matter when you are
computing the difference between two 10-based whole numbers which can
be represented exactly in binary Arithmetic.

> If you could live with a datetime format which date can understand
> (instead of your cobbled-up pseudo numeric monster above) ...

 Thank you. I learned something out of it, but I used such "cobbled-up
pseudo numeric monster" because, in kind of a poor-man's "measured"
way, I use size, line count, last mod and sha256 metadata in file
names baselining the system's state. I avoid spaces and any other
characters you can't use in most fs' and/or may be interpreted by
shells or OSs in their own ways.

On 12/17/23, Greg Wooledge <greg@wooledge.org> wrote:
> On Sun, Dec 17, 2023 at 10:12:11AM +0000, Albretch Mueller wrote:
>> ... after some long processing for which seconds would be exact
>> enough, then I would like to get the seconds elapsed since dt00
>
> Are you working in bash, or sh?

 bash

> Bash offers some alternatives, to avoid having to fork two date(1)
> processes, light as those are.  The first is:
>
>     unicorn:~$ printf -v dt00 '%(%s)T' -1
>     unicorn:~$ echo "$dt00"
>     1702821082

 but this is not what I meant and I am sure you can once again impress
everyone here with your bash skills/wisdom. Is there such a thing as:

 dt00=$(date +%Y%m%d%H%M%S)
 printf -(whatever the formatting for seconds would be) "${dt00}%Y%m%d%H%M%S"

 I dealing with my "exposed mode" kinds of states ;-). I use a
computer which initial state is kept as part of the file name as I
explained, then as part of another ("exposed" or "private") reboot I
want to check that file, so I would not be able to then simply do $((
dt02 - dt00 ))

 After rebooting (separately for "exposed" or "private" mode):
 a) those kinds of name measured files are checked,
 b) if everything is fine you would keep some basic statistics (last
difference; as well as, incrementally, average, stddev, skewness and
kurtosis of differences so far)
 c) delete the file(s) previous to the latest one
 ...

 and, no, I am not expecting for bash to do that kind of statistics
for me, but I would love to see once again how wrong I am.

On 12/17/23, Andy Smith <andy@strugglers.net> wrote:
> น Pedants at this point may feel the need to launch launch into a
> sub-thread about how subtracting one epoch time from another doesn't
> always produce an accurate duration. I can't stop you, but it won't
> be news to me.

 No calendar issue here. All is needed is turning dates into seconds
to get the time diff in seconds. I am not trying to start a
sub-thread, but how on earth would that not always produce an accurate
duration?

 lbrtchx


Reply to: