1745834 Members
4175 Online
108723 Solutions
New Discussion юеВ

Re: date conversion

 
SOLVED
Go to solution
dublonn
Occasional Advisor

date conversion

Hello,

I need to convert date from this format:
Tue Mar 23 18:31:08 EET 2010
in something like this
07DA0317101F2B00
07da-year
03-mounth
17-day
10-hour
1f-min
2b-sec
00-decsec
I tried to do it with this command
printf "%x" `date "+ %Y %m %d %H %M %S"`
but the result it's not what I want, it looks like this
7DA317101F2B
7da-year
3-mounth
17-day
10-hour
1f-min
2b-sec
Are missing some zerous :)
There is a way to have all zeros?

server: RP4440
sw:B 11.11

BR,
dublonn
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: date conversion

Hi:

Why not:

# print $(date "+%Y%m%d%H%M%S")

Regards!

...JRF...
dublonn
Occasional Advisor

Re: date conversion

hy,

It's work but I need in hexa not in decimal...
James R. Ferguson
Acclaimed Contributor
Solution

Re: date conversion

Hi:

OK, I see. How about:

# perl -e '@t=localtime(time);printf "%0x%02x%02x%02x%02x%02x\n",$t[5]+1900,$t[4]+1,$t[3],$t[2],$t[1],$t[0]'
7da03170d0c0a

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: date conversion

Hi:

For grins-and-giggles, we can shorten things:

# perl -e '@t=localtime;printf "%03x%02x%02x%02x%02x%02x\n",$t[5]+1900,$t[4]+1,@t[3,2,1,0]'

For that matter, you can convert any Epoch seconds to a hexadecimal number representing the date components. For me, in the Eastern US, 999,999,999 seconds after the Epoch yielded 0x7d10908152e27 or 20010908214639 (2001-09-08-21:46:39).

# perl -e '@t=localtime(999_999_999);printf "%03x%02x%02x%02x%02x%02x\n",$t[5]+1900,$t[4]+1,@t[3,2,1,0]'

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: date conversion

Hmmm,

dublonn, what problem are you really trying to solve (and why)?

Do you have the time itself as input, or do you have a piece of string in "Day Mon dd hh:mm:ss zon yyyy" format at input?

Frankly I suspect that the problem description is all wrong.
In my world it makes no sense at all to convert individual date & time components to hexadecimal other than to confuse the romanians (feeble joke).

And where does the deci-seconds component come from?

It wouldn't surprise me if the hex is supposed to be a simple formating of the time since epoch in seconds

>perl -e "$x=time();printf qq(%d %x\n),$x,$x"
1269403997 4ba9915d

That brings a 14 byte yyyymmddhhmmss (localtime reformatted) through a 10 digit decimal number, down to an 8 character hexadecimal string, utilizing all positions fully.

Your request seems to ask that for example the minute component instead of going from 00-59 (not using 60 - 99) uses 00-4B (not using 4C-FF). Why?

Hope this help clarify a little,
Cheers,
Hein

dublonn
Occasional Advisor

Re: date conversion

Thanks a lot James, your answer was briliant.
the right command for me is:

perl -e '@t=localtime;printf "%04x%02x%02x%02x%02x%02x%02x\n",$t[5]+1900,$t[4]+1,@t[3,2,1,0]'

the result is : 07da031809170c00
exactly what I need it.

Mr Heuvel,
I need date in this format because I have to synconize some eq from the network which support only hexa format and that type of format.

BR,
dublonn
Hein van den Heuvel
Honored Contributor

Re: date conversion

dublonn, I'm glad you have what you need, albeit technically not what you asked for.
Thin input date format was 32 bit- binary where the question asked about a piece of string: From this format Tue Mar 23...

>> I need date in this format because I have to synconize some eq from the network which support only hexa format and that type of format.

Thank you for explaining.

Best regards,
Hein.