The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-11-2008
DrivesMeCrazy DrivesMeCrazy is online now
Registered User
  
 

Join Date: Nov 2008
Posts: 14
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.
  #2 (permalink)  
Old 11-12-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
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?
  #3 (permalink)  
Old 11-12-2008
DrivesMeCrazy DrivesMeCrazy is online now
Registered User
  
 

Join Date: Nov 2008
Posts: 14
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 (permalink)  
Old 02-07-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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.
  #5 (permalink)  
Old 02-06-2009
avklinux avklinux is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 27
can i use above function as below .?
i=1226424300;
perl -e 'print scalar(localtime("$i")), "\n"'

Thanks
Avklinux
  #6 (permalink)  
Old 02-07-2009
ddreggors ddreggors is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 91
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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0