Operating System - HP-UX
1752564 Members
5596 Online
108788 Solutions
New Discussion

Input variable having YYYMMDD:HHMMSS format to convert to Epoch timestamp

 
SOLVED
Go to solution
SwissKnife
Frequent Advisor

Input variable having YYYMMDD:HHMMSS format to convert to Epoch timestamp

Hi, 

 

I search everywhere, man pages, google without no success.  I need to convert to Epoch Milliseconds an input string stored in a korn shell variable. The format is something like YYYYMMDD:HHMMSS.

 

A="20220124180000"

....

Z=???? 

echo "$Z"

should return 1643047200

 

I tried with the date native function and some intricated syntax but I'm not able to say my date as input is using that format ... 

 

Any ideas are very welcome.

Thanks in advance,

Kind regards, 

Den. 

 

 

 

4 REPLIES 4
Steven Schweda
Honored Contributor

Re: Input variable having YYYMMDD:HHMMSS format to convert to Epoch timestamp

> I tried with the date native function and some intricated syntax but
> I'm not able to say my date as input is using that format ...

   With my weak psychic powers, I can't see what you tried.

> should return 1643047200

   In which timezone?

   I don't have an HP-UX system up at the moment, but, on a handy Mac:

   "-f input_fmt new_date" seems to do what "man date" says that it
will.

$ date -j -f '%Y%m%d%H%M%S' '20220124180000'
Mon Jan 24 18:00:00 CST 2022

   "'+%s" as the "[+output_fmt]" option seems to do what "man strftime"
says that it will.

$ date -j -f '%Y%m%d%H%M%S' '20220124180000' '+%s'
1643068800

   UTC:

$ date -j -u -f '%Y%m%d%H%M%S' '20220124180000'
Mon Jan 24 18:00:00 UTC 2022

$ date -j -u -f '%Y%m%d%H%M%S' '20220124180000' '+%s'
1643047200

$ TZ=UTC date -j -f '%Y%m%d%H%M%S' '20220124180000'
Mon Jan 24 18:00:00 UTC 2022

$ TZ=UTC date -j -f '%Y%m%d%H%M%S' '20220124180000' '+%s'
1643047200

   As usual, many things are possible.

> [...] Milliseconds [...]

   "Milli"?

SwissKnife
Frequent Advisor

Re: Input variable having YYYMMDD:HHMMSS format to convert to Epoch timestamp

Hi Steven,

Thank you but I really need this for HP because options .... 

Your command did not work on HP-UX. 

 

Kind regards,

Denis. 

 

Steven Schweda
Honored Contributor

Re: Input variable having YYYMMDD:HHMMSS format to convert to Epoch timestamp

> Your command did not work on HP-UX.

   "did not work" is not a useful problem description.  It does not
say what you did.  It does not say what happened when you did it.  As
usual, showing actual actions (commands) with their actual results
(error messages, ...) can be more helpful than vague descriptions or
interpretations.  Copy+paste is your friend.

   _Which_ HP-UX?

      uname -a

> [...] I'm not able to say my date as input [...]

   "I'm not able" is about as good as "did not work".  "There is no
'date' option to allow me" might be a better description of the problem.

   If your HP-supplied "date" program doesn't let you specify an input
date+time, then you might need to find/provide a program which does.
For example, GNU "date" (part of GNU Coreutils) seems to have a
-d/--date option.  You might need to translate your date+time
specification a little to get GNU "date" to parse it.

      http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-8.27/

   Or find some other, better "date" program.  Or write your own program
to do the job?

Solution

Re: Input variable having YYYMMDD:HHMMSS format to convert to Epoch timestamp

Yeah, standard HP-UX date command definitely won't support those options. The coreutils GNU date should though.

For the OP, I'm not clear why you say you want the date output to be "in milliseconds" when the input granularity isn't going below seconds. If you really insist on this output, you may as well work out number of seconds from the epoch and then multiply by 1,000.


I am an HPE Employee
Accept or Kudo