Convert Epoch Time to Standard Date and Time & Vice Versa | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Convert Epoch Time to Standard Date and Time & Vice Versa

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-11-2008
DrivesMeCrazy DrivesMeCrazy is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 12 January 2012, 1:07 PM EST
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
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

Convert Standard Date/Time to Epoch

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.
Sponsored Links
    #2  
Old 11-11-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 4 April 2014, 3:58 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 17 Times in 16 Posts
works for me:
Code:
[yogeshs@helptoldreal-lr yogeshs]$ perl -e 'use Time::Local; print timelocal(0,25,1,11,11,2008), "\n";'
1228938900
[yogeshs@helptoldreal-lr yogeshs]$ perl -e 'print scalar(localtime(1228938900)), "\n"'
Thu Dec 11 01:25:00 2008
[yogeshs@helptoldreal-lr yogeshs]$

can you post a similar example where the epoch value you got is incorrect?
Sponsored Links
    #3  
Old 11-12-2008
DrivesMeCrazy DrivesMeCrazy is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 12 January 2012, 1:07 PM EST
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Yogesh Sawant View Post
works for me:
Code:
[yogeshs@helptoldreal-lr yogeshs]$ perl -e 'use Time::Local; print timelocal(0,25,1,11,11,2008), "\n";'
1228938900
[yogeshs@helptoldreal-lr yogeshs]$ perl -e 'print scalar(localtime(1228938900)), "\n"'
Thu Dec 11 01:25:00 2008
[yogeshs@helptoldreal-lr yogeshs]$

can you post a similar example where the epoch value you got is incorrect?
Hi Yogesh,

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

Really appreciate your help
    #4  
Old 02-06-2009
avklinux avklinux is offline
Registered User
 
Join Date: Dec 2008
Last Activity: 11 February 2010, 7:30 PM EST
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
can i use above function as below .?

Code:
i=1226424300;
perl -e 'print scalar(localtime("$i")), "\n"'

Thanks
Avklinux

Last edited by Yogesh Sawant; 02-15-2011 at 06:22 AM.. Reason: added code tags
Sponsored Links
    #5  
Old 02-07-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
 
Join Date: Jan 2008
Last Activity: 11 September 2009, 3:44 PM EDT
Posts: 729
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by DrivesMeCrazy View Post
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.
Thats it. 0-11 for the months just like localtime(). Good catch.
Sponsored Links
    #6  
Old 02-07-2009
ddreggors's Avatar
ddreggors ddreggors is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 22 July 2013, 9:20 AM EDT
Location: Florida
Posts: 160
Thanks: 5
Thanked 12 Times in 11 Posts
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 12:46 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using awk or nawk to convert epoch time to date format minigts Shell Programming and Scripting 5 10-27-2011 12:56 PM
Convert epoch to human readable date & time format Yaminib Shell Programming and Scripting 3 08-14-2010 09:57 AM
how to convert date time to epoch time in solaris anshuman0507 Shell Programming and Scripting 6 08-22-2009 10:28 PM
Convert Epoch time format to normal date time format in the same file rk4k Shell Programming and Scripting 3 11-19-2008 09:04 PM
Convert from standard epoch time from a shell script? LordJezo Shell Programming and Scripting 4 09-18-2005 11:48 PM



All times are GMT -4. The time now is 12:04 AM.