git-push-to-gitlab: Don't create or push `synced/master`
[sunny256-utils.git] / klokke
blob27433302b4a3ff8a644e258fcd32d186f1594e79
1 #!/usr/bin/env perl
3 #=======================================================================
4 # klokke
5 # File ID: 37c2594c-f743-11dd-b452-000475e441b9
7 # Continuously print date and timestamp, mostly used to check the sync
8 # between computers.
10 # Character set: UTF-8
11 # ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
12 # License: GNU General Public License version 2 or later, see end of
13 # file for legal stuff.
14 #=======================================================================
16 use strict;
17 use warnings;
18 use Getopt::Long;
19 use Time::HiRes qw{ gettimeofday usleep };
21 local $| = 1;
23 our %Opt = (
25 'epoch' => 0,
26 'help' => 0,
27 'quiet' => 0,
28 'verbose' => 0,
29 'version' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.2.1';
37 Getopt::Long::Configure('bundling');
38 GetOptions(
40 'epoch|e' => \$Opt{'epoch'},
41 'help|h' => \$Opt{'help'},
42 'quiet|q+' => \$Opt{'quiet'},
43 'verbose|v+' => \$Opt{'verbose'},
44 'version' => \$Opt{'version'},
46 ) || die("$progname: Option error. Use -h for help.\n");
48 $Opt{'verbose'} -= $Opt{'quiet'};
49 $Opt{'help'} && usage(0);
50 if ($Opt{'version'}) {
51 print_version();
52 exit(0);
55 exit(main());
57 sub main {
58 # {{{
59 my $Retval = 0;
61 my ($Old, $Str) = ("", "");
62 my $time_str = $Opt{'epoch'} ? "%s" : "%04u-%02u-%02u %02u:%02u:%02uZ";
64 while (1) {
65 do {
66 usleep(10);
67 my ($Epoch, $Fract) = gettimeofday();
68 my ($Sec, $Min, $Hour, $Day, $Mon, $Year, $Wday, $Yday, $is_dst) =
69 gmtime($Epoch);
70 $Str = sprintf($time_str,
71 $Opt{'epoch'} ? $Epoch
72 : ($Year+1900, $Mon+1, $Day, $Hour, $Min, $Sec));
73 } while ($Str eq $Old);
74 print("$Str\r");
75 $Old = $Str;
76 usleep(900_000);
79 return $Retval;
80 # }}}
81 } # main()
83 sub print_version {
84 # Print program version {{{
85 print("$progname $VERSION\n");
86 return;
87 # }}}
88 } # print_version()
90 sub usage {
91 # Send the help message to stdout {{{
92 my $Retval = shift;
94 if ($Opt{'verbose'}) {
95 print("\n");
96 print_version();
98 print(<<"END");
100 Continuously print date and timestamp, mostly used to check the sync
101 between computers.
103 Usage: $progname [options]
105 Options:
107 -e, --epoch
108 Display Unix time (seconds since 1970-01-01 00:00:00 UTC) instead of
109 the default ISO 8601 format.
110 -h, --help
111 Show this help.
112 -q, --quiet
113 Be more quiet. Can be repeated to increase silence.
114 -v, --verbose
115 Increase level of verbosity. Can be repeated.
116 --version
117 Print version information.
120 exit($Retval);
121 # }}}
122 } # usage()
124 sub msg {
125 # Print a status message to stderr based on verbosity level {{{
126 my ($verbose_level, $Txt) = @_;
128 if ($Opt{'verbose'} >= $verbose_level) {
129 print(STDERR "$progname: $Txt\n");
131 return;
132 # }}}
133 } # msg()
135 __END__
137 # This program is free software; you can redistribute it and/or modify
138 # it under the terms of the GNU General Public License as published by
139 # the Free Software Foundation; either version 2 of the License, or (at
140 # your option) any later version.
142 # This program is distributed in the hope that it will be useful, but
143 # WITHOUT ANY WARRANTY; without even the implied warranty of
144 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
145 # See the GNU General Public License for more details.
147 # You should have received a copy of the GNU General Public License
148 # along with this program.
149 # If not, see L<http://www.gnu.org/licenses/>.
151 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :