perl - Determine time elapsed since last timestamp -


i write file timestamp , able determine if current time < 1 min timestamp.
using:

open $fh, '>', '~/.time_from_run';   $timestamp = localtime(time);     print $fh $timestamp;   close $fh;   

prints out string. best way this?

the easiest approach write output time() rather localtime() (incidentally, localtime(time) pointless - if omit parameter localtime(), use time() default). give number of seconds since start of 1970 (which big number - 1503061737). can read number , compare current value of time().

the (slight) downside approach numbers time() aren't understandable humans. if want read , understand values in .time_from_run compromise , write timestamp in understood format (iso-8601 obvious choice.

use time::piece;  open $fh, '>', '~/.time_from_run' or die $!;  print $fh localtime->datatime; 

you can read , compare time this:

use time::piece; use time::seconds;  open $fh, '<', '~/.time_from_run' or die !$;  chomp(my $then_str) = <$fh>;  $then = time::piece->strptime('%y%m%dt%h:%m:%s', $then_str);  if (localtime - $then < one_minute) {   print "elapsed time less minute\n"; } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -