#!/usr/bin/perl # timestamp.pl - returns a time stamp for creating file names # # Dave Stoddard - 1/18/2001 # dgs@accelix.com # # Description # ----------- # This program is a simple utility for obtaining a timestamp for a file # name or other uses. It is used in conjunction with various shell scripts. # Four types of time stamps can be produced -- YYMMDD, YYMMDD-HHMMSS, # timestamp as of midnight, or current timestamp. The default is YYMMDD. # # Command Line Options # -------------------- # The following command line options are recognized by the program: # # -h : hours format -- provide time as YYMMDD-HHMM # -s : seconds format -- provide time as YYMMDD-HHMMSS # -t : timestamp format -- current time # -m : timestamp format -- from midnight # -x : timestamp format -- uses yyyy mm dd hh mm ss input on command line # -y : YMD format -- provide time as YYMMDD (default) # -z : seconds format -- time as YYMMDD-HHMMSS using cmd line timestamp # # All options are exclusive of one another. # # RCS Information # --------------- # $Id: timestamp.pl,v 1.1 2001/02/01 20:39:45 www Exp $ # # $Log: timestamp.pl,v $ # Revision 1.1 2001/02/01 20:39:45 www # Initial revision # require "ctime.pl"; # used to convert time to date string require "timelocal.pl"; # used to convert date to time value require "getopts.pl"; # handle command line options my ($sc,$mn,$hr,$dd,$mm,$yy,$wd,$yd,$isdst,$t); # --------------------------- # handle command line options # --------------------------- &Getopts ('hmstxyz'); if ($opt_h) { # YYMMDD-HHMM $t = ($ARGV[0] ? $ARGV[0] : time); DateString($t); print "$ymdhm\n"; exit 0; } if ($opt_m) { # timestamp for midnight $t = ($ARGV[0] ? $ARGV[0] : time); ($sc,$mn,$hr,$dd,$mm,$yy,$wd,$yd,$isdst) = localtime ($t); $time = timelocal (0,0,0,$dd,$mm,$yy); print "$time\n"; exit 0; } if ($opt_s) { # YYMMDD-HHMMSS $t = ($ARGV[0] ? $ARGV[0] : time); DateString($t); print "$ymdhms\n"; exit 0; } if ($opt_t) { # timestamp format (current) $t = ($ARGV[0] ? $ARGV[0] : time); print "$t\n"; exit 0; } if ($opt_x) { # timestamp format (current) $yy = $ARGV[0] - 1900; $mm = $ARGV[1] - 1; $dd = $ARGV[2]; $hr = $ARGV[3]; $mn = $ARGV[4]; $sc = $ARGV[5]; $time = timelocal ($sc,$mn,$hr,$dd,$mm,$yy); print "$time\n"; exit 0; } if ($opt_y) { # YYMMDD (default) $t = ($ARGV[0] ? $ARGV[0] : time); DateString($t); print "$ymd\n"; exit 0; } if ($opt_z) { # YYMMDD-HHMMSS DateString($ARGV[0]); print "$ymdhms\n"; exit 0; } # default $t = ($ARGV[0] ? $ARGV[0] : time); DateString($t); print "$ymd\n"; exit 0; ### ### DateString() creates various forms of date strings for this program. ### All strings returned are two digit month, day, hour, minute, and second. ### Year is four digits. ### sub DateString { $time = shift; ($sc,$mn,$hr,$dd,$mm,$yy,$wd,$yd,$isdst) = localtime ($time); ++$mm; if ($yy < 199) { $yy += 1900; } if ($mm < 10) { $mm = "0" . $mm; } if ($dd < 10) { $dd = "0" . $dd; } if ($hr < 10) { $hr = "0" . $hr; } if ($mn < 10) { $mn = "0" . $mn; } if ($sc < 10) { $sc = "0" . $sc; } $ymd = $yy . $mm . $dd; $ymdhm = $yy . $mm . $dd . "-" . $hr . $mn; $ymdhms = $yy . $mm . $dd . "-" . $hr . $mn . $sc; }