#!/usr/bin/perl -w # # $Id: epoch2date,v 1.5 2003/08/01 02:10:56 jmates Exp $ # # The author disclaims all copyrights and releases this script into the # public domain. # # Converts epoch times to human parsable dates. Accepts list of epoch # dates (integers) on command line. Use xargs(1) or similar to take # input from a file. # # epoch2date `perl -le 'print time'` # echo 1059673440 | xargs epoch2date require 5; use strict; use File::Basename qw(basename); my $basename = basename $0; use POSIX qw(strftime); use Getopt::Std; my %opts; getopts('ef:glq', \%opts); if (not @ARGV or (exists $opts{'g'} and exists $opts{'l'})) { die "usage: $basename [-q] [-f fmt] [-g|-l] [-e] epoch [epoch ...]\n"; } my $format = $opts{'f'} || "%Y-%m-%d %H:%M:%S"; # gmtime or localtime, depending on program name or options supplied my $function = 'localtime'; $function = 'gmtime' if $basename eq 'epoch2gmdate'; $function = 'localtime' if exists $opts{'l'}; $function = 'gmtime' if exists $opts{'g'}; for my $epoch (@ARGV) { # scrub input unless ($epoch =~ m/^\d+$/) { warn "warning: skipping non-integer input: $epoch\n" unless exists $opts{'q'}; next; } # prefix output with original epoch value if needed print $epoch, ' ' if exists $opts{'e'}; print strftime $format, eval "$function $epoch"; print "\n"; } =pod SCRIPT CATEGORIES Utilities =cut