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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert to epoch

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
0
0
US
I am pulling datetime out of a summary file. I have the begin time and end time. What I need to do is convert each to epoch so I can subtract and then convert into run time in 00:00:00 format.

I can do the conversion back no problem bout how would I convert Thu Mar 4 00:02:33 CST 2010 to epoch?

All help is greatly appreciated.
 
One way is to use Perl's Time::Local package which contains the timelocal function:

Code:
#!/usr/bin/perl -w
use Time::Local;
my $month = 3;
my $day = 4;
my $year = 2010;
my $seconds = 33;
my $hours = 0;
my $minutes = 2;

my $epochseconds = timelocal($seconds, $minutes, $hours, $day, $month - 1, $year);

print "$epochseconds\n";
 
You could incorporate into olded's code a hash table of month string/month value, as that is the only part you would need to convert.

Code:
my $month_string = 'Mar';

my %months = (
    'Jan' => "1",
    'Feb' => "2",
    'Mar' => "3"
);

my $month_value = $months{$month_string};

Chris
 
Thanks all, your recommendations were greatly appreciated and helpful!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top