|
Packit |
95306a |
#!/usr/bin/perl -w
|
|
Packit |
95306a |
# Copyright (c) 2009-2017 Sullivan Beck. All rights reserved.
|
|
Packit |
95306a |
# This program is free software; you can redistribute it and/or modify it
|
|
Packit |
95306a |
# under the same terms as Perl itself.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
###############################################################################
|
|
Packit |
95306a |
###############################################################################
|
|
Packit |
95306a |
|
|
Packit |
95306a |
require 5.010000;
|
|
Packit |
95306a |
use Date::Manip::Date;
|
|
Packit |
95306a |
use IO::File;
|
|
Packit |
95306a |
use strict;
|
|
Packit |
95306a |
use warnings;
|
|
Packit |
95306a |
|
|
Packit |
95306a |
###############################################################################
|
|
Packit |
95306a |
# HELP
|
|
Packit |
95306a |
###############################################################################
|
|
Packit |
95306a |
|
|
Packit |
95306a |
our($usage);
|
|
Packit |
95306a |
my $COM = $0;
|
|
Packit |
95306a |
$COM =~ s/^.*\///;
|
|
Packit |
95306a |
|
|
Packit |
95306a |
$usage=
|
|
Packit |
95306a |
"usage: $COM OPTIONS
|
|
Packit |
95306a |
-h/--help : Print help.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Options to tell which date to print (only one of
|
|
Packit |
95306a |
the following should be entered... if none are,
|
|
Packit |
95306a |
'now' is printed).
|
|
Packit |
95306a |
|
|
Packit |
95306a |
-d/--date=STRING : Print information about that date.
|
|
Packit |
95306a |
-f/--file=FILE : Print out each date (1/line) in FILE
|
|
Packit |
95306a |
-r/--reference=FILE
|
|
Packit |
95306a |
: Print out the last modification
|
|
Packit |
95306a |
time of FILE
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Other options
|
|
Packit |
95306a |
|
|
Packit |
95306a |
-R/--rfc-2822 : Print the date in RFC 2822 format
|
|
Packit |
95306a |
(any +FORMAT string is ignored)
|
|
Packit |
95306a |
Mon, 07 Aug 2006 12:34:56 -0600
|
|
Packit |
95306a |
-u/--utc/--universal:
|
|
Packit |
95306a |
Converts date(s) to UT time.
|
|
Packit |
95306a |
";
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=pod
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=head1 NAME
|
|
Packit |
95306a |
|
|
Packit |
95306a |
dm_date - print out the system date and time
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=head1 SYNOPSIS
|
|
Packit |
95306a |
|
|
Packit |
95306a |
This performs the same operation as the unix 'date' command, but using
|
|
Packit |
95306a |
the Date::Manip module.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
dm_date [OPTION]... [+FORMAT]
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=head1 DESCRIPTION
|
|
Packit |
95306a |
|
|
Packit |
95306a |
This displays information about the current system time, or some
|
|
Packit |
95306a |
other time. Options are:
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=over 4
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=item -h, --help
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Print online help.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=item -d STRING, --date=STRING
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Display time described by STRING. STRING can be any string which can
|
|
Packit |
95306a |
be parsed by Date::Manip. Please refer to the Date::Manip::Date
|
|
Packit |
95306a |
documentation for details.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=item -f DATEFILE, --file=DATEFILE
|
|
Packit |
95306a |
|
|
Packit |
95306a |
This reads each line in DATEFILE, and extracts a date from it and
|
|
Packit |
95306a |
prints out the information.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Blank lines and lines starting with a pound (#) are ignored. Lines
|
|
Packit |
95306a |
not containing a valid date are also ignored.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=item -r FILE, --reference=FILE
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Displays the last modification time of FILE.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=item -R, --rfc-2822
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Displayc the date and time in RFC 2822 format.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Example: Mon, 07 Aug 2006 12:34:56 -0600
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=item -u, --utc, --universal
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Converts the date to UT (GMT) and prints out the information.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=back
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Only one of -d, -f, or -r should be included. If more than
|
|
Packit |
95306a |
one is included, the first one from the list (-d, -f, -r)
|
|
Packit |
95306a |
is used and any other is ignored.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
The format string starts with a plus (+) and contains any
|
|
Packit |
95306a |
of the format directives described in Date::Manip::Date.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=head1 KNOWN BUGS
|
|
Packit |
95306a |
|
|
Packit |
95306a |
None known.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=head1 BUGS AND QUESTIONS
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Please refer to the Date::Manip::Problems documentation for
|
|
Packit |
95306a |
information on submitting bug reports or questions to the author.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=head1 SEE ALSO
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Date::Manip::Date
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=head1 LICENSE
|
|
Packit |
95306a |
|
|
Packit |
95306a |
This script is free software; you can redistribute it and/or
|
|
Packit |
95306a |
modify it under the same terms as Perl itself.
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=head1 AUTHOR
|
|
Packit |
95306a |
|
|
Packit |
95306a |
Sullivan Beck (sbeck@cpan.org)
|
|
Packit |
95306a |
|
|
Packit |
95306a |
=cut
|
|
Packit |
95306a |
|
|
Packit |
95306a |
###############################################################################
|
|
Packit |
95306a |
# PARSE ARGUMENTS
|
|
Packit |
95306a |
###############################################################################
|
|
Packit |
95306a |
|
|
Packit |
95306a |
our($date,$file,$ref,$rfc,$ut,$format);
|
|
Packit |
95306a |
|
|
Packit |
95306a |
while ($_ = shift) {
|
|
Packit |
95306a |
|
|
Packit |
95306a |
(print $usage), exit if ($_ eq "-h" || $_ eq "--help");
|
|
Packit |
95306a |
|
|
Packit |
95306a |
$date = shift, next if ($_ eq "-d" || $_ eq "--date");
|
|
Packit |
95306a |
$date = $1, next if ($_ =~ /^\-\-date=(.*)/);
|
|
Packit |
95306a |
|
|
Packit |
95306a |
$file = shift, next if ($_ eq "-f" || $_ eq "--file");
|
|
Packit |
95306a |
$file = $1, next if ($_ =~ /^\-\-file=(.*)/);
|
|
Packit |
95306a |
|
|
Packit |
95306a |
$ref = shift, next if ($_ eq "-r" || $_ eq "--reference");
|
|
Packit |
95306a |
$ref = $1, next if ($_ =~ /^\-\-reference=(.*)/);
|
|
Packit |
95306a |
|
|
Packit |
95306a |
$rfc = 1, next if ($_ eq "-R" || $_ eq "--rfc-2822");
|
|
Packit |
95306a |
$ut = 1, next if ($_ eq "-u" || $_ eq "--utc" || $_ eq "--universal");
|
|
Packit |
95306a |
|
|
Packit |
95306a |
$format = $_, last if ($_ =~ s/^\+//);
|
|
Packit |
95306a |
die $usage;
|
|
Packit |
95306a |
}
|
|
Packit |
95306a |
|
|
Packit |
95306a |
if ($rfc) {
|
|
Packit |
95306a |
$format ='%a, %d %b %Y %H:%M:%S %z';
|
|
Packit |
95306a |
}
|
|
Packit |
95306a |
|
|
Packit |
95306a |
if (@ARGV || ! $format) {
|
|
Packit |
95306a |
die $usage;
|
|
Packit |
95306a |
}
|
|
Packit |
95306a |
|
|
Packit |
95306a |
############################################################################
|
|
Packit |
95306a |
# MAIN PROGRAM
|
|
Packit |
95306a |
############################################################################
|
|
Packit |
95306a |
|
|
Packit |
95306a |
|
|
Packit |
95306a |
my $d = new Date::Manip::Date;
|
|
Packit |
95306a |
|
|
Packit |
95306a |
if ($date) {
|
|
Packit |
95306a |
my $err = $d->parse($date);
|
|
Packit |
95306a |
exit 1 if ($err);
|
|
Packit |
95306a |
$d->convert("GMT") if ($ut);
|
|
Packit |
95306a |
print $d->printf($format),"\n";
|
|
Packit |
95306a |
|
|
Packit |
95306a |
} elsif ($file) {
|
|
Packit |
95306a |
my $in = new IO::File;
|
|
Packit |
95306a |
$in->open($file) || die "ERROR: unable to open date file: $file\n\n$usage";
|
|
Packit |
95306a |
my @in = <$in>;
|
|
Packit |
95306a |
$in->close();
|
|
Packit |
95306a |
chomp(@in);
|
|
Packit |
95306a |
|
|
Packit |
95306a |
foreach my $date (@in) {
|
|
Packit |
95306a |
$date =~ s/\#.*//;
|
|
Packit |
95306a |
$date =~ s/\s*$//;
|
|
Packit |
95306a |
next if (! $date);
|
|
Packit |
95306a |
|
|
Packit |
95306a |
my $err = $d->parse($date);
|
|
Packit |
95306a |
next if ($err);
|
|
Packit |
95306a |
$d->convert("GMT") if ($ut);
|
|
Packit |
95306a |
print $d->printf($format),"\n";
|
|
Packit |
95306a |
}
|
|
Packit |
95306a |
|
|
Packit |
95306a |
} elsif ($ref) {
|
|
Packit |
95306a |
if (-e $ref) {
|
|
Packit |
95306a |
my $date = ( stat($ref) )[9];
|
|
Packit |
95306a |
my $err = $d->parse("epoch $date");
|
|
Packit |
95306a |
exit if ($err);
|
|
Packit |
95306a |
$d->convert("GMT") if ($ut);
|
|
Packit |
95306a |
print $d->printf($format),"\n";
|
|
Packit |
95306a |
}
|
|
Packit |
95306a |
|
|
Packit |
95306a |
} else {
|
|
Packit |
95306a |
my $err = $d->parse("now");
|
|
Packit |
95306a |
exit 1 if ($err);
|
|
Packit |
95306a |
$d->convert("GMT") if ($ut);
|
|
Packit |
95306a |
print $d->printf($format),"\n";
|
|
Packit |
95306a |
}
|
|
Packit |
95306a |
|
|
Packit |
95306a |
|
|
Packit |
95306a |
# Local Variables:
|
|
Packit |
95306a |
# mode: cperl
|
|
Packit |
95306a |
# indent-tabs-mode: nil
|
|
Packit |
95306a |
# cperl-indent-level: 3
|
|
Packit |
95306a |
# cperl-continued-statement-offset: 2
|
|
Packit |
95306a |
# cperl-continued-brace-offset: 0
|
|
Packit |
95306a |
# cperl-brace-offset: 0
|
|
Packit |
95306a |
# cperl-brace-imaginary-offset: 0
|
|
Packit |
95306a |
# cperl-label-offset: 0
|
|
Packit |
95306a |
# End:
|