#!/usr/bin/perl # # kronos - converts epochy times to time strings. # - without arguments, give GMT # - with arguments, converts (numeric) argument into localtimes # # usage: # kronos [-help | -Version | -gmt | -loc | -both ] ... # # Revision History # 1.0 Initial revision. 2012 # 1.1 Added -gmt, -local, -both. 140207 # 1.2 Added licensing info. 180616 # # Copyright 2012 Wayne Morrison # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ####################################################################### # # kronos use strict; use Getopt::Long qw(:config no_ignore_case_always); # # Version information. # my $NAME = "kronos"; my $VERS = "$NAME version: 1.2"; ############################################################################ # # Options fields. # my %opts = (); # Options. # # Command line arguments. # my @opts = ( 'gmt', # Use GMT instead of local. 'local', # Use localtime instead of GMT. 'both', # Use both times. 'help', # Give a help message. 'Version', # Display the program version. ); my $gmt = 0; # -gmt option flag. my $loc = 0; # -local option flag. my $both = 0; # -both option flag. ############################################################################ main(); exit(0); #----------------------------------------------------------------------------- # Routine: main() # sub main { my $timestr; # Time string. $| = 1; # # Munch on the options and arguments. # optsandargs(); # # Give the GMT of the current time if no arguments were given. # if(@ARGV == 0) { my $now = time; if($both) { $timestr = gmtime(); print "$now $timestr\tGMT\n"; $timestr = localtime(); print "$now $timestr\tlocal\n"; } else { $timestr = gmtime(); print "$now\t$timestr\n"; } } # # Give the GMT or localtime of the specified times if arguments # were given. # foreach my $kron (@ARGV) { if($gmt) { $timestr = gmtime($kron); if($both) { print "$kron $timestr\tGMT\n"; } else { print "$kron $timestr\n"; } } if($loc) { $timestr = localtime($kron); if($both) { print "$kron $timestr\tlocal\n"; } else { print "$kron $timestr\n"; } } } } #---------------------------------------------------------------------- # Routine: optsandargs() # # Purpose: Parse the command line for options and arguments. # sub optsandargs { # # Parse the options. # GetOptions(\%opts,@opts) || usage(); # # Check for some immediate-action options. # usage() if(defined($opts{'help'})); version() if(defined($opts{'Version'})); # # Check our time-selection options. # $gmt = $opts{'gmt'}; $loc = $opts{'local'}; $both = $opts{'both'}; # # If neither of our time-selection options was set, we'll # default to local time. # if(($gmt == 0) && ($loc == 0)) { $loc = 1; } # # If neither of our time-selection options was set, we'll # default to local time. # if($both == 1) { $gmt = 1; $loc = 1; } } #---------------------------------------------------------------------- # Routine: version() # # Purpose: Print the version number(s) and exit. # sub version { print STDERR "$VERS\n"; exit(0); } #---------------------------------------------------------------------- # Routine: usage() # # Purpose: Give usage message and exit. # sub usage { print STDERR "usage: kronos [options] ... \n"; print STDERR "\n"; print STDERR "\twhere [options] are:\n"; print STDERR "\t\t-gmt\n"; print STDERR "\t\t-local\n"; print STDERR "\t\t-both\n"; print STDERR "\n"; print STDERR "\t\t-help\n"; print STDERR "\t\t-Version\n"; exit(0); } 1; ############################################################################## =pod =head1 NAME B - Converts epoch times to time strings. =head1 SYNOPSIS kronos [options] =head1 DESCRIPTION B converts epoch times to time strings. The local time is used by default, but the I<-gmt> option will give the conversion to GMT. The I<-both> option will provide both conversion to GMT and local time. If B is given no options, the current time is displayed in GMT. This is done because the B command is an easy way to get the local time, and this format gives an easy way to get the GMT time. The epoch time is the number of seconds since the Epoch -- January 1, 1970, 00:00:00. =head1 OPTIONS B takes the following options: =over 4 =item I<-gmt> This option performs the time translations in GMT rather than local time. =item I<-local> This option performs the time translations in local time rather than GMT. =item I<-both> This option performs the time translations in GMT and local time. =item I<-Version> Display the version information for B. =item I<-help> Display a help message. =back =head1 LICENSE Copyright 2012 Wayne Morrison Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =head1 AUTHOR Wayne Morrison, wayne@waynemorrison.com =head1 SEE ALSO B =cut