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

Date Converstion/Calc: Date String --> epoch --> different date string 1

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
I have a variable that contains date and times. I would like to convert these into epoch time, perform some basic calculations and convert the output back into human readable format. Here is a workflow with specific data and examples:

--------------------------------------------------------
--------------------------------------------------------
(1) Phase I: variable $date contains the following data:
Jan 08 10:06:18 EST
Jan 08 10:07:56 EST
Jan 08 10:07:23 EST
Jan 08 10:08:31 EST
Jan 08 10:08:11 EST
Jan 08 10:08:52 EST
Jan 08 10:09:44 EST
Jan 08 10:21:22 EST

(2) Phase II: convert each line into epoch
Jan 08 10:06:18 EST = 1231409178
.....
.....
Jan 08 10:21:22 EST = 1231410082

(3) Phase III: Perform some calculations:
ex: subtract first line from last line to get lag time:
1231410082 - 1231409178 = 904

(4) Phase IV: Convert the calculation to human readable time and print
"15.07 minutes"
--------------------------------------------------------
--------------------------------------------------------

Thanks,
Justin

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
The Time::Local module that comes with perl can handle that. Try some code and if you get stuck post back.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
What have you tried so far and where are you stuck? Consider using the Time::Local module to convert to seconds since the epoch; you'll need to do the month conversions yourself, and take note of the fact that that January is month 0, not 1.

Annihilannic.
 
Thats cool, no problem. [smile]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Using Time::Local I think this routine will do the trick for converting to epoch. However, how to I chop up my current date string into the format needed by timegm?

Current Date/Time format:
Jan 08 10:06:42 EST
Jan 09 02:21:18 EST
Jan 09 03:42:21 EST

Code:
sub  datetimeToEpoch {
     my $ref_datetime=shift;
     my $tz_adjust=shift;
     my $timeEpoch=0;
     
     # --- array format --
     # --- seconds (0 to 59), minutes (0 to 59), hours (0 to 23), day (1 to 31),month (1 to 12),year (YYYY)
     $timeEpoch=timegm($ref_datetime->[0], $ref_datetime->[1], $ref_datetime->[2], $ref_datetime->[3], ($ref_datetime->[4] - 1), ($ref_datetime->[5] - 1900));
     $timeEpoch+=($tz_adjust * 3600);
     
     return($timeEpoch);
     }

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Something like this?

Code:
my $date='Jan 08 10:06:42 EST';
my $months='JanFebMarAprMayJunJulAugSepOctNovDec';
my ($mon_alpha,$mday,$time,$tz) = split / /,$date;
my $mon = index($months,$mon_alpha) / 3 + 1;
my ($hours,$mins,$secs) = split /:/,$time;

(Makes assumptions about locale and includes no error checking)

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top