Skip to content

Instantly share code, notes, and snippets.

@wh13371
Last active August 29, 2015 14:14
Show Gist options
  • Save wh13371/dbf9239c470ac10071f8 to your computer and use it in GitHub Desktop.
Save wh13371/dbf9239c470ac10071f8 to your computer and use it in GitHub Desktop.
perl - a half decent datetime ("yyyy_mm_dd_HH_mm_ss.ffffff") method
#! /usr/bin/perl
# a Perl script with a "now" method to return a (yyyy_mm_dd_HH_mm_ss.ffffff) timestamp - Linux & Windows filename valid
use strict;
use warnings;
use POSIX; # for "strftime"
use Time::HiRes; # for "Time::HiRes::gettimeofday"
use 5.010;
# return a half decent <datetime> string i.e. "2015_02_02_16_56_40.194733"
sub now
{
my ($seconds, $microseconds) = Time::HiRes::gettimeofday;
return strftime(q/%Y_%m_%d_%H_%M_%S./, localtime($seconds)) . sprintf("%06d", $microseconds);
}
while(1)
{
my $t = now();
say $t;
# demonstrate file creation based on returned "datetime" format
my $filename = $t . ".log";
open OUT, ">", "$filename" or die $!;
print OUT $t;
close (OUT);
sleep(1);
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment