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

Time/Date libraries 1

Status
Not open for further replies.

columb

IS-IT--Management
Joined
Feb 5, 2004
Messages
1,231
Location
EU
I'm writing a routine to provide uptime for our systems. The data is stored as a number of files in /var/adm/uptime which look like
Code:
-rw-r--r--   1 root     system            0 Aug 31 08:27 1188566501
-rw-r--r--   1 root     system            0 Aug 31 08:39 1188567143
-rw-r--r--   1 root     system            0 Sep 03 04:57 1188567905
-rw-r--r--   1 root     system            0 Sep 03 05:30 1188813780
These files are created at boot time by a line in inittab which looks like
Code:
uptime:23456789:wait:touch /var/adm/uptime/$(perl -e 'print time')
and the 'current' one is touched every minute to modify the last update time. From this it's pretty trivial to report on uptime using a simple report like
Code:
#!/usr/bin/perl -w
use strict;

foreach my $file ( glob "/var/adm/uptime/[0-9]*" )
  {
  $file =~ m!.*/(.*)! and my $start = $1;
  my $end = +(stat $file)[9];
  print "Up between ", (scalar localtime $start), " and ", (scalar localtime $end), "\n";
  }
Now comes the question - management wants a report on uptime during a certain time period. To do this I need to convert start and end date paramters into seconds since the epoch. I've searched on CPAN and come up with Date::Manip but it looks like a hammer to crack a nut - even the author says that it shouldn't be used most of the time.

Can anyone recommend a simple way of converting YY/MM/DD to seconds since the epoch?

Ceci n'est pas une signature
Columb Healy
 
I've sort of fixed it myself - it's amazing how posting on TecTips can stimulate the imagination. It's a bit crude but
Code:
sub get_time
  {
  my $fname = shift;
  $fname =~ m!\d\d/\d\d/\d\d! or print_usage ( "$fname is an invalid date" );
  my ( $day, $month, $year ) = split ( /\//, $fname );
  system "touch ${month}${day}0000$year /tmp/uptime_tmp.$$";
  my $ret_time = +(stat "/tmp/uptime_tmp.$$")[9];
  unlink "/tmp/uptime_tmp.$$";
  return $ret_time;
  }
does what I want.

Ceci n'est pas une signature
Columb Healy
 
According to the "Llama Book" - Appendix B - use the Time::Local module. The example they give is:

use Time::Local;
my $time = timelocal($sec, $min, $hr, $day, $mon, $yr);

As they point out - read the documentation! because for instance, the values for $mon & $yr for January 2004 are not 1 & 2004.

I hope that helps.

Mike
 
Thanks Mike

That's exactly what I wanted.

Ceci n'est pas une signature
Columb Healy
 
No problem. I'm glad I could help.

(and thanks for the 'star').

Regards.

Mike
 
Time::Local treats the months like localtime() does, 0-1 instead of 1-12 so remember to subtract one from the month when converting from human readable time into epoch seconds.

For example if you have yy/mm/dd (08/07/04) the month after extracting it from the pattern would need to be 6 instead of 7.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
note: 0-1 above should be 0-11

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top