Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get epoch time?

Status
Not open for further replies.

Thameem

Programmer
Sep 18, 2002
30
US
I need to get epoch time using perl. It shouldn't use any system dependant parameters to get the time. (that is, it shouldn't be created using localtime or any other system related time modules).
Can any one help me to get this?
 
Not sure what you want here. I assume you want the epoch time of the system where the Perl script is being executed. Why don't you want to use localtime? If you are worried about portability, localtime should work on all Perl platforms. See example below:
Code:
my ($sec, $min, $hr, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime(0);
$month++;
$year += 1900;
$sec = '0' . $sec if ($sec < 10);
$min = '0' . $min if ($min < 10);
$hr = '0' . $hr if ($hr < 10);
print &quot;The epoch was $month/$day_of_month/$year $hr:$min:$sec\n&quot;;
 
thanks raider,
But it is not epoch calculation. because you are setting year to 1900 (epoch should be 1970, jan 1). Anyway I found out the way, that is time() will return epoch.
 
Unix system date in PERL
################

$date= `date`;
print &quot;Unix system date is $date&quot;;

$day = `date +%d`;
print &quot;$day&quot;;

$month = `date +%m`;
print &quot;$month&quot;;

$year = `date +%y`;
print &quot;$year&quot;;

$time=`date +%H%M%S`;
print &quot;$time&quot;;




 
mms1,

Whilst these are all useful representations of the time, none of these give epoch seconds. As thameem noted, epoch is returned by the time function in perl. From the perlfunc man page:

time:
Returns the number of non-leap seconds since whatever time the system considers to be the epoch (that's 00:00:00, January 1, 1904 for MacOS, and 00:00:00 UTC, January 1, 1970 for most other systems). Suitable for feeding to gmtime() and localtime().

Cheers, NEIL s-)
 
Thameem,

Sorry for the confusion, I thought you wanted the date and time of the epoch. If I used 'gmtime' instead of 'localtime', the code would give you that epoch date and time 01/01/1970 00:00:00. Note that my code did not set the year to 1900. The line '$year += 1900;' adds 1900 to the year. This is needed because the localtime function returns the number of years since 1900.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top