Debian Codegraph @

examples/dm_date

raw
1 #!/usr/bin/perl -w
2 # Copyright (c) 2009-2026 Sullivan Beck. All rights reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the same terms as Perl itself.
5
6 ###############################################################################
7 ###############################################################################
8
9 require 5.010000;
10 use Date::Manip::Date;
11 use IO::File;
12 use strict;
13 use warnings;
14
15 ###############################################################################
16 # HELP
17 ###############################################################################
18
19 our($usage);
20 my $COM = $0;
21 $COM =~ s/^.*\///;
22
23 $usage=
24 "usage: $COM OPTIONS
25 -h/--help : Print help.
26
27 Options to tell which date to print (only one of
28 the following should be entered... if none are,
29 'now' is printed).
30
31 -d/--date=STRING : Print information about that date.
32 -f/--file=FILE : Print out each date (1/line) in FILE
33 -r/--reference=FILE
34 : Print out the last modification
35 time of FILE
36
37 Other options
38
39 -R/--rfc-2822 : Print the date in RFC 2822 format
40 (any +FORMAT string is ignored)
41 Mon, 07 Aug 2006 12:34:56 -0600
42 -u/--utc/--universal:
43 Converts date(s) to UT time.
44 ";
45
46 =pod
47
48 =head1 NAME
49
50 dm_date - print out the system date and time
51
52 =head1 SYNOPSIS
53
54 This performs the same operation as the unix 'date' command, but using
55 the Date::Manip module.
56
57 dm_date [OPTION]... [+FORMAT]
58
59 =head1 DESCRIPTION
60
61 This displays information about the current system time, or some
62 other time. Options are:
63
64 =over 4
65
66 =item -h, --help
67
68 Print online help.
69
70 =item -d STRING, --date=STRING
71
72 Display time described by STRING. STRING can be any string which can
73 be parsed by Date::Manip. Please refer to the Date::Manip::Date
74 documentation for details.
75
76 =item -f DATEFILE, --file=DATEFILE
77
78 This reads each line in DATEFILE, and extracts a date from it and
79 prints out the information.
80
81 Blank lines and lines starting with a pound (#) are ignored. Lines
82 not containing a valid date are also ignored.
83
84 =item -r FILE, --reference=FILE
85
86 Displays the last modification time of FILE.
87
88 =item -R, --rfc-2822
89
90 Displayc the date and time in RFC 2822 format.
91
92 Example: Mon, 07 Aug 2006 12:34:56 -0600
93
94 =item -u, --utc, --universal
95
96 Converts the date to UT (GMT) and prints out the information.
97
98 =back
99
100 Only one of -d, -f, or -r should be included. If more than
101 one is included, the first one from the list (-d, -f, -r)
102 is used and any other is ignored.
103
104 The format string starts with a plus (+) and contains any
105 of the format directives described in Date::Manip::Date.
106
107 =head1 KNOWN BUGS
108
109 None known.
110
111 =head1 BUGS AND QUESTIONS
112
113 Please refer to the Date::Manip::Problems documentation for
114 information on submitting bug reports or questions to the author.
115
116 =head1 SEE ALSO
117
118 Date::Manip::Date
119
120 =head1 LICENSE
121
122 This script is free software; you can redistribute it and/or
123 modify it under the same terms as Perl itself.
124
125 =head1 AUTHOR
126
127 Sullivan Beck (https://github.com/SBECK-github/)
128
129 =cut
130
131 ###############################################################################
132 # PARSE ARGUMENTS
133 ###############################################################################
134
135 our($date,$file,$ref,$rfc,$ut,$format);
136
137 while ($_ = shift) {
138
139 (print $usage), exit if ($_ eq "-h" || $_ eq "--help");
140
141 $date = shift, next if ($_ eq "-d" || $_ eq "--date");
142 $date = $1, next if ($_ =~ /^\-\-date=(.*)/);
143
144 $file = shift, next if ($_ eq "-f" || $_ eq "--file");
145 $file = $1, next if ($_ =~ /^\-\-file=(.*)/);
146
147 $ref = shift, next if ($_ eq "-r" || $_ eq "--reference");
148 $ref = $1, next if ($_ =~ /^\-\-reference=(.*)/);
149
150 $rfc = 1, next if ($_ eq "-R" || $_ eq "--rfc-2822");
151 $ut = 1, next if ($_ eq "-u" || $_ eq "--utc" || $_ eq "--universal");
152
153 $format = $_, last if ($_ =~ s/^\+//);
154 die $usage;
155 }
156
157 if ($rfc) {
158 $format ='%a, %d %b %Y %H:%M:%S %z';
159 }
160
161 if (@ARGV || ! $format) {
162 die $usage;
163 }
164
165 ############################################################################
166 # MAIN PROGRAM
167 ############################################################################
168
169
170 my $d = new Date::Manip::Date;
171
172 if ($date) {
173 my $err = $d->parse($date);
174 exit 1 if ($err);
175 $d->convert("GMT") if ($ut);
176 print $d->printf($format),"\n";
177
178 } elsif ($file) {
179 my $in = new IO::File;
180 $in->open($file) || die "ERROR: unable to open date file: $file\n\n$usage";
181 my @in = <$in>;
182 $in->close();
183 chomp(@in);
184
185 foreach my $date (@in) {
186 $date =~ s/\#.*//;
187 $date =~ s/\s*$//;
188 next if (! $date);
189
190 my $err = $d->parse($date);
191 next if ($err);
192 $d->convert("GMT") if ($ut);
193 print $d->printf($format),"\n";
194 }
195
196 } elsif ($ref) {
197 if (-e $ref) {
198 my $date = ( stat($ref) )[9];
199 my $err = $d->parse("epoch $date");
200 exit if ($err);
201 $d->convert("GMT") if ($ut);
202 print $d->printf($format),"\n";
203 }
204
205 } else {
206 my $err = $d->parse("now");
207 exit 1 if ($err);
208 $d->convert("GMT") if ($ut);
209 print $d->printf($format),"\n";
210 }
211
212
213 # Local Variables:
214 # mode: cperl
215 # indent-tabs-mode: nil
216 # cperl-indent-level: 3
217 # cperl-continued-statement-offset: 2
218 # cperl-continued-brace-offset: 0
219 # cperl-brace-offset: 0
220 # cperl-brace-imaginary-offset: 0
221 # cperl-label-offset: 0
222 # End: