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

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



On Sun, Dec 17, 2023 at 03:28:58PM +0000, Albretch Mueller wrote:
> 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"

No, there is nothing in bash which scans date/time strings.  You'd need
to use an external tool for this.  GNU date is one such tool.

>  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 ))

You've moved the goalposts.  Now the state is not being kept WITHIN
a script, but in a FILE that's used by two separate scripts (one that
writes it, and one that has to interpret it).

This is a very different kind of problem.

You'll need to choose a tool or a language that can parse your chosen
storage format.  This might be GNU date.  This might be Perl, Python,
Tcl, or some other language that has appropriate facilities for parsing
date/time strings.

You'll also be strongly encouraged to choose a storage format that
facilitates such parsing.  You might even want to work backwards --
figure out how you're going to parse the string, then choose the
format that works best with that specific parser.


Reply to: