Merge branch 'add-datefmt'
[sunny256-utils.git] / datefmt
bloba1b0951b6a5297c768226d756e193f763c543270
1 #!/usr/bin/env perl
3 #==============================================================================
4 # datefmt
5 # File ID: cbb80cc6-a45e-11ea-944f-4f45262dc9b5
7 # Convert number of seconds to date format.
9 # Character set: UTF-8
10 # ©opyleft 2020– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of file for
12 # legal stuff.
13 #==============================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'help' => 0,
24 'quiet' => 0,
25 'verbose' => 0,
26 'version' => 0,
30 our $progname = $0;
31 $progname =~ s/^.*\/(.*?)$/$1/;
32 our $VERSION = '0.1.0';
34 Getopt::Long::Configure('bundling');
35 GetOptions(
37 'help|h' => \$Opt{'help'},
38 'quiet|q+' => \$Opt{'quiet'},
39 'verbose|v+' => \$Opt{'verbose'},
40 'version' => \$Opt{'version'},
42 ) || die("$progname: Option error. Use -h for help.\n");
44 $Opt{'verbose'} -= $Opt{'quiet'};
45 $Opt{'help'} && usage(0);
46 if ($Opt{'version'}) {
47 print_version();
48 exit(0);
51 exit(main());
53 sub main {
54 my $Retval = 0;
56 if (defined($ARGV[0])) {
57 my $i = 0;
58 while (defined($ARGV[$i])) {
59 print(parse_line($ARGV[$i]) . "\n");
60 $i++;
62 } else {
63 while (my $line = <STDIN>) {
64 print(parse_line($line));
67 return $Retval;
70 sub parse_line {
71 my $l = shift;
72 $l =~ s/^(\s*)(\d+)(.*?)$/sprintf("%s%s%s", $1, sec2date($2), $3)/e;
73 return $l;
76 sub sec2date {
77 my $s = shift;
78 ($s =~ /^\d+$/) || return $s;
79 my $ss = $s;
80 my %secc = (
81 'min' => 60,
82 'hour' => 3600,
83 'day' => 86400
85 $secc{'year'} = $secc{'day'} * 365.25;
86 my $retval = "";
87 my $years = int($ss / $secc{'year'});
88 $ss -= $years * $secc{'year'};
89 my $days = int(($ss % $secc{'year'}) / $secc{'day'});
90 my $hours = int(($ss % $secc{'day'}) / $secc{'hour'});
91 my $mins = int(($ss % $secc{'hour'}) / $secc{'min'});
92 my $secs = $ss % $secc{'min'};
94 return sprintf("%us", $secs) if ($s < $secc{'min'});
95 return sprintf("%um:%02us", $mins, $secs) if ($s < $secc{'hour'});
96 return sprintf("%uh:%02um:%02us",
97 $hours, $mins, $secs) if ($s < $secc{'day'});
98 return sprintf("%ud:%02uh:%02um:%02us",
99 $days, $hours, $mins, $secs) if ($s < $secc{'year'});
100 return sprintf("%uy:%ud:%02uh:%02um:%02us",
101 $years, $days, $hours, $mins, $secs);
104 sub print_version {
105 # Print program version
106 print("$progname $VERSION\n");
107 return;
110 sub usage {
111 # Send the help message to stdout
112 my $Retval = shift;
114 if ($Opt{'verbose'}) {
115 print("\n");
116 print_version();
118 print(<<"END");
120 Convert number of seconds to date format. If no arguments are specified,
121 read from stdin.
123 Usage: $progname [options] [seconds [...]]
125 Options:
127 -h, --help
128 Show this help.
129 -q, --quiet
130 Be more quiet. Can be repeated to increase silence.
131 -v, --verbose
132 Increase level of verbosity. Can be repeated.
133 --version
134 Print version information.
137 exit($Retval);
140 sub msg {
141 # Print a status message to stderr based on verbosity level
142 my ($verbose_level, $Txt) = @_;
144 if ($Opt{'verbose'} >= $verbose_level) {
145 print(STDERR "$progname: $Txt\n");
147 return;
150 __END__
152 # This program is free software; you can redistribute it and/or modify it under
153 # the terms of the GNU General Public License as published by the Free Software
154 # Foundation; either version 2 of the License, or (at your option) any later
155 # version.
157 # This program is distributed in the hope that it will be useful, but WITHOUT
158 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
159 # FOR A PARTICULAR PURPOSE.
160 # See the GNU General Public License for more details.
162 # You should have received a copy of the GNU General Public License along with
163 # this program.
164 # If not, see L<http://www.gnu.org/licenses/>.
166 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :