Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Convert Epoch Time to Standard Date and Time & Vice Versa

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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? Unix or Linux Image

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 Unix and Linux 11-11-2008
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 22 April 2015, 4:51 AM EDT
Location: Rossem, Tazenda
Posts: 1,211
Thanks: 6
Thanked 18 Times in 17 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 Unix and Linux 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 Unix and Linux 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 Unix and Linux 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 Unix and Linux 02-07-2009
ddreggors's Unix or Linux Image
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 08:32 PM.