in reply to Converting POSIX format date/times to epoch seconds

See HTTP::Date. Yes, the name and choice of distribution for this piece of code if horrible, but it has a refreshingly minimal-frills API if your needs are very simple (ie no date math, just conversions).

Makeshifts last the longest.

  • Comment on Re: Converting POSIX format date/times to epoch seconds

Replies are listed 'Best First'.
Re^2: Converting POSIX format date/times to epoch seconds
by Anonymous Monk on Sep 02, 2014 at 18:11 UTC

    seems to fit the KISS method but the HTTP distribution is probably not the best. small enough to copy and customize if needed.

      I needed to convert the date from the WMIC command output so I added a block to the code (using a copy anyway for other reasons). it is small enough to customize and here is a small customization (which can probably be improved). I wanted to have a "better" way of determining up time - converting the boot time then taking the delta seemed to be a simple solution. FYI - Win32::GetTickCount was returning 13 days (in ms) on my machine which has been up for 150something days. using this change and time() function to get a delta produced a correct result.

      # WMIC (Windows command line) Date output format from `wmic os get + lastbootuptime` (($yr, $mon, $day, $hr, $min, $sec) = /^ (\d{4}) # year (\d\d) # numerical month (\d\d) # day (\d\d) # Hour (\d\d) # Min (\d\d) # Sec /x) ||

        for those who come here looking for the boot time, here is a program to convert the wmic format boot time to seconds then redisplay it. I cloned the HTTP::Date code then chopped it down to this. also being posted on http://www.perlmonks.org/?node_id=216892

        use strict; use Time::Local; sub wmic2time (;$) { my $wmic_time_in=$_[0]; if(!defined $wmic_time_in) { $wmic_time_in=join " ",`wmic os get lastbootuptime 2>&1`; $wmic_time_in=~s/\r\n/ /g; $wmic_time_in=~s/ * / /g; } $wmic_time_in=~s/LastBootUpTime //i; # caller may have remove +d this already but just in case.... my @d = $wmic_time_in =~ /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/x +; # year numerical month day Hour Min Sec return undef unless @d; $d[1]--; # month return eval { my $t = Time::Local::timelocal(reverse @d); $t < 0 ? undef : $t; }; } my $newtime=wmic2time; print "$newtime\n"; print scalar localtime $newtime . "\n";