git-push-to-gitlab: Don't create or push `synced/master`
[sunny256-utils.git] / datefmt
blobbd012a9280cfec3a4489b8ac7f4887c346989c68
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.2.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{
74 (\s*)
75 (-?)
76 (\d+)
77 (.*?)
80 sprintf("%s%s%s%s", $1, $2, sec2date($3), $4)
81 }ex;
82 return $l;
85 sub sec2date {
86 my $s = shift;
87 ($s =~ /^\d+$/) || return $s;
88 my $ss = $s;
89 my %secc = (
90 'min' => 60,
91 'hour' => 3600,
92 'day' => 86400
94 $secc{'year'} = $secc{'day'} * 365.2425;
95 my $retval = "";
96 my $years = int($ss / $secc{'year'});
97 $ss -= $years * $secc{'year'};
98 my $days = int(($ss % $secc{'year'}) / $secc{'day'});
99 my $hours = int(($ss % $secc{'day'}) / $secc{'hour'});
100 my $mins = int(($ss % $secc{'hour'}) / $secc{'min'});
101 my $secs = $ss % $secc{'min'};
103 return sprintf("%us", $secs) if ($s < $secc{'min'});
104 return sprintf("%um:%02us", $mins, $secs) if ($s < $secc{'hour'});
105 return sprintf("%uh:%02um:%02us",
106 $hours, $mins, $secs) if ($s < $secc{'day'});
107 return sprintf("%ud:%02uh:%02um:%02us",
108 $days, $hours, $mins, $secs) if ($s < $secc{'year'});
109 return sprintf("%uy:%ud:%02uh:%02um:%02us",
110 $years, $days, $hours, $mins, $secs);
113 sub print_version {
114 # Print program version
115 print("$progname $VERSION\n");
116 return;
119 sub usage {
120 # Send the help message to stdout
121 my $Retval = shift;
123 if ($Opt{'verbose'}) {
124 print("\n");
125 print_version();
127 print(<<"END");
129 Convert number of seconds to date format. If no arguments are specified,
130 read from stdin.
132 Usage: $progname [options] [seconds [...]]
134 Options:
136 -h, --help
137 Show this help.
138 -q, --quiet
139 Be more quiet. Can be repeated to increase silence.
140 -v, --verbose
141 Increase level of verbosity. Can be repeated.
142 --version
143 Print version information.
146 exit($Retval);
149 sub msg {
150 # Print a status message to stderr based on verbosity level
151 my ($verbose_level, $Txt) = @_;
153 if ($Opt{'verbose'} >= $verbose_level) {
154 print(STDERR "$progname: $Txt\n");
156 return;
159 __END__
161 # This program is free software; you can redistribute it and/or modify it under
162 # the terms of the GNU General Public License as published by the Free Software
163 # Foundation; either version 2 of the License, or (at your option) any later
164 # version.
166 # This program is distributed in the hope that it will be useful, but WITHOUT
167 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
168 # FOR A PARTICULAR PURPOSE.
169 # See the GNU General Public License for more details.
171 # You should have received a copy of the GNU General Public License along with
172 # this program.
173 # If not, see L<http://www.gnu.org/licenses/>.
175 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :