Skip to content

Instantly share code, notes, and snippets.

@danslimmon
Created October 2, 2012 19:02
Show Gist options
  • Save danslimmon/3822522 to your computer and use it in GitHub Desktop.
Save danslimmon/3822522 to your computer and use it in GitHub Desktop.
add_timestamps.pl
#!/usr/bin/perl
#
# What it does (example):
#
# dan@cleon:~/tools/server$ yes | ./add_timestamps.pl | head
# 2012-10-02 14:59:16.579109 y
# 2012-10-02 14:59:16.579180 y
# 2012-10-02 14:59:16.579210 y
# 2012-10-02 14:59:16.579240 y
# 2012-10-02 14:59:16.579269 y
# 2012-10-02 14:59:16.579299 y
# 2012-10-02 14:59:16.579329 y
# 2012-10-02 14:59:16.579370 y
# 2012-10-02 14:59:16.579390 y
# 2012-10-02 14:59:16.579420 y
use Time::localtime;
use Time::Local;
use Time::HiRes;
while (my $line = <>) {
my $time = Time::HiRes::clock_gettime(CLOCK_REALTIME) . "\n";
my $time_ipart = int $time;
my $time_fpart = $time - $time_ipart;
my $loctime = localtime($time_ipart);
my $time_str = sprintf("%04d-%02d-%02d %02d:%02d:%02d.%06d",
$loctime->year + 1900,
$loctime->mon + 1,
$loctime->mday,
$loctime->hour,
$loctime->min,
$loctime->sec,
int($time_fpart*1e6));
print "$time_str $line";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment