![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Convert UTC time to Date | GNMIKE | Shell Programming and Scripting | 10 | 05-08-2009 07:07 AM |
| Convert milliseconds to standard time | chiru_h | Shell Programming and Scripting | 1 | 07-19-2007 01:45 PM |
| how to convert epoch time to readible format? | cin2000 | Shell Programming and Scripting | 11 | 12-19-2005 07:14 PM |
| Convert from standard epoch time from a shell script? | LordJezo | Shell Programming and Scripting | 4 | 09-18-2005 11:48 PM |
| binary to string conversion and vice versa | kinnaree | High Level Programming | 3 | 11-16-2002 09:02 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Convert Epoch Time to Standard Date and Time & Vice Versa
Hi guys,
I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their functions. Hence, I intend to go for Perl and include a one-liner command in the shell script for the conversion. As you can see below, I have no problem converting Epoch to Standard Date/Time. But for converting Standard Date/Time to Epoch, I am not getting the correct Epoch value. Am I missing anything here? ![]() Convert Epoch to Standard Date/Time Code:
bash-3.00# perl -e 'print scalar(localtime(1226424300)), "\n"' Wed Nov 12 01:25:00 2008 Code:
bash-3.00# perl -e 'use Time::Local; print timelocal(0,25,1,11,11,2008), "\n";' 1228929900 bash-3.00# perl -e 'use Time::Local; print timegm(0,25,1,11,11,2008), "\n";' 1228958700 Thanks in advance. |
|
||||
|
Quote:
Thanks for the reply. I found out that the month parameter for perl timelocal() need to "- 1". As in Jan to specify "0", Dec to specify 11. I got the value wrong, thats why epoch is not returning a consistent value. Code:
bash-3.00# perl -e 'print scalar(localtime(1226426701)), "\n"' Wed Nov 12 02:05:01 2008 bash-3.00# perl -e 'use Time::Local; print timelocal(1,5,2,12,10,2008), "\n"' 1226426701 |
|
||||
|
Thats it. 0-11 for the months just like localtime(). Good catch.
|
|
||||
|
Not sure about perl but in bash:
# epoch $ date +%s # If you have a time already in mind you can change the line above to use YOUR time and not system time like this: date -d "Sat Feb 7 00:37:06 EST 2009" +%s # result = 1233985026 # and back to regular time from epoch date --date "Jan 1, 1970 00:00:00 +0000 + `date +%s` seconds" #You can substitute `date +%s` in the line above with your own epoch time that you have already obtained date --date "Jan 1, 1970 00:00:00 +0000 + 1233985026 seconds" # result = Sat Feb 7 00:37:06 EST 2009 Last edited by ddreggors; 02-07-2009 at 01:46 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|