1752785 Members
5822 Online
108789 Solutions
New Discussion юеВ

Re: How to convert date

 
SOLVED
Go to solution
tikual
Advisor

How to convert date

Hi all,

Do anyone know how to convert the date format from MMYYDD to the seconds from 1970? And how to revert it back to MMYYDD?

Thank you so much!
10 REPLIES 10
tikual
Advisor

Re: How to convert date

Sorry, it should be MMDDYY.
Michael Tully
Honored Contributor

Re: How to convert date

look for A.Clay Stephenson's 'caljd' perl script. It's a beauty.
Thanks Clay!

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x026250011d20d6118ff40090279cd0f9,00.html
Anyone for a Mutiny ?
tikual
Advisor

Re: How to convert date

But, I just wanted to add few command lines to my script rather than using his great script. Is it no other methods??
T G Manikandan
Honored Contributor

Re: How to convert date

I am not sure whether Robin's suggestion in this link can help you

Might be useful
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x9b9f663ce855d511abcd0090277a778c,00.html
T G Manikandan
Honored Contributor

Re: How to convert date

john korterman
Honored Contributor

Re: How to convert date

Hi,
tricky to do in a few lines. Perl can return the seconds:
# perl -e 'print time,"\n"'
1052472882

which can be converted to something more humane with a few lines of shell code, e
.g.:
# NUM_SECS=1052472882
# echo "0d${NUM_SECS}=Y" | adb
2003 May 9 09:34:42

but notice that the TZ setting influences the result of the above, e.g. on my system:
# TZ=MET-1METDST
# NUM_SECS=0
# echo "0d${NUM_SECS}=Y" | adb
1970 Jan 1 01:00:00

where you might have expected 00:00:00 hours!
Hope it helps.

regards,
John K.
it would be nice if you always got a second chance
tikual
Advisor

Re: How to convert date

Thanks Manikandan,

But I couldn't run the script(date2epoch.pl) successfully. The error is like this:

syntax error in file ./date2epoch.pl at line 3, next 2 tokens "use Time"
Illegal modulus of constant zero in file ./date2epoch.pl at line 4, next 2 token
s "months="
Illegal expression (MODULO) as lvalue in file ./date2epoch.pl at line 4, next 2
tokens "qw("
syntax error in file ./date2epoch.pl at line 4, next 2 tokens "qw("
Illegal modulus of constant zero in file ./date2epoch.pl at line 5, next 2 token
s "days="
Illegal expression (MODULO) as lvalue in file ./date2epoch.pl at line 5, next 2
tokens "qw("
syntax error in file ./date2epoch.pl at line 5, next 2 tokens "qw("
Execution of ./date2epoch.pl aborted due to compilation errors.

Thanks John K.

perl -e 'print time,"\n"' is my wanted. But, how about when I want to specified a date as MMDDYY to be seconds.
Robin Wakefield
Honored Contributor
Solution

Re: How to convert date

Hi,

Here's a couple of "one-liners":

# perl -e 'use Time::Local;$ARGV[0]=~/(..)(..)(..)/;$2 < 70 ? $year=$2+100 : $year=$2;print timegm(0,0,0,$3,$1-1,$year),"\n"' 050309
1052438400

# perl -e '($mday,$mon,$year)=(gmtime(shift))[3,4,5];$year-=100 if ($year>=100);printf("%02d%02d%02d\n",$mon+1,$year,$mday)' 1052438400
050309

You need to use a more up-to-date version of perl, that's why you're getting the errors.

rgds, Robin
T G Manikandan
Honored Contributor

Re: How to convert date

Robin:

Think of the devil and here he is.