| 1 | package Unix::Uptime::BSD; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | our $VERSION='0.36'; | ||
| 7 | $VERSION = eval $VERSION; | ||
| 8 | |||
| 9 | use DateTime::Format::Strptime; | ||
| 10 | |||
| 11 | sub uptime { | ||
| 12 | my $class = shift; | ||
| 13 | |||
| 14 | local $ENV{PATH} .= ':/usr/local/sbin:/usr/sbin:/sbin'; | ||
| 15 | my $boottime = `sysctl kern.boottime`; | ||
| 16 | |||
| 17 | $boottime =~ s/^\s*kern\.boottime\s*=\s*//; | ||
| 18 | # OpenBSD: | ||
| 19 | # kern.boottime=Mon Jan 5 14:00:50 2009 | ||
| 20 | # Darwin: | ||
| 21 | # kern.boottime = Wed Jan 21 12:49:52 2009 | ||
| 22 | # NetBSD: | ||
| 23 | # kern.boottime = Mon Jan 5 15:30:35 2009 | ||
| 24 | my $strp = DateTime::Format::Strptime->new( | ||
| 25 | pattern => '%a%t%b%t%d%t%T%t%Y', | ||
| 26 | ); | ||
| 27 | my $dt = $strp->parse_datetime($boottime) | ||
| 28 | or die "Failed to parse kern.boottime: ",$strp->errstr; | ||
| 29 | return time() - $dt->epoch(); | ||
| 30 | } | ||
| 31 | |||
| 32 | no warnings qw(once); | ||
| 33 | *uptime_hires = \&uptime; | ||
| 34 | |||
| 35 | use base 'Unix::Uptime::BSD::Load'; | ||
| 36 | |||
| 37 | 1; | ||
| 38 | |||
| 39 | __END__ |