| 1 | # Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2017, 2019 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Gtk2-Ex-Clock. | ||
| 4 | # | ||
| 5 | # Gtk2-Ex-Clock is free software; you can redistribute it and/or modify it | ||
| 6 | # under the terms of the GNU General Public License as published by the Free | ||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Gtk2-Ex-Clock is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Gtk2-Ex-Clock. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | |||
| 19 | # MAYBE: | ||
| 20 | # ->update to force redraw for time() or localtime TZ change. | ||
| 21 | # time() change would normally be external (as root), and generally not | ||
| 22 | # notified to running programs anyway. | ||
| 23 | # TZ change within the program would be a bit unusual. | ||
| 24 | # A forced redraw might be good enough usually. An update wouldn't redraw | ||
| 25 | # if in fact no text change. | ||
| 26 | # Some program-wide update all clocks would probably be more use than one by | ||
| 27 | # one. | ||
| 28 | |||
| 29 | |||
| 30 | package Gtk2::Ex::Clock; | ||
| 31 | 4 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | 0 | ▶ | use Gtk2 1.200; # version 1.200 for GDK_PRIORITY_REDRAW |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 35 | use POSIX (); | ||
| 36 | use POSIX::Wide 2; # version 2 for netbsd 646 alias | ||
| 37 | use List::Util qw(min); | ||
| 38 | use Scalar::Util; | ||
| 39 | use Time::HiRes; | ||
| 40 | use Glib::Ex::SourceIds; | ||
| 41 | |||
| 42 | # uncomment this to run the ### lines | ||
| 43 | #use Smart::Comments; | ||
| 44 | |||
| 45 | our $VERSION = 16; | ||
| 46 | |||
| 47 | use constant _DEFAULT_FORMAT => '%H:%M'; | ||
| 48 | |||
| 49 | use Glib::Object::Subclass | ||
| 50 | 'Gtk2::Label', | ||
| 51 | properties => [Glib::ParamSpec->string | ||
| 52 | ('format', | ||
| 53 | 'Format string', | ||
| 54 | 'An strftime() format string to display the time.', | ||
| 55 | _DEFAULT_FORMAT, | ||
| 56 | Glib::G_PARAM_READWRITE), | ||
| 57 | |||
| 58 | Glib::ParamSpec->scalar | ||
| 59 | ('timezone', | ||
| 60 | 'Timezone', | ||
| 61 | 'The timezone to use in the display, either a string for the TZ environment variable, or a DateTime::TimeZone object. An empty string or undef means the local timezone.', | ||
| 62 | Glib::G_PARAM_READWRITE), | ||
| 63 | |||
| 64 | Glib::ParamSpec->string | ||
| 65 | ('timezone-string', | ||
| 66 | 'Timezone string', | ||
| 67 | 'The timezone to use in the display, as a string for the TZ environment variable. An empty string or undef means the local timezone.', | ||
| 68 | (eval {Glib->VERSION(1.240);1} | ||
| 69 | ? undef # default | ||
| 70 | : ''), # no undef/NULL before Perl-Glib 1.240 | ||
| 71 | Glib::G_PARAM_READWRITE), | ||
| 72 | |||
| 73 | Glib::ParamSpec->int | ||
| 74 | ('resolution', | ||
| 75 | 'Resolution', | ||
| 76 | 'The resolution of the clock, in seconds, or 0 to decide this from the format string.', | ||
| 77 | 0, # min | ||
| 78 | 3600, # max | ||
| 79 | 0, # default | ||
| 80 | Glib::G_PARAM_READWRITE), | ||
| 81 | ]; | ||
| 82 | |||
| 83 | |||
| 84 | # _TIMER_MARGIN_MILLISECONDS is an extra period in milliseconds to add to | ||
| 85 | # the timer period requested. It's designed to ensure the timer doesn't | ||
| 86 | # fire before the target time boundary of 1 second or 1 minute, in case | ||
| 87 | # g_timeout_add() or the select() within it ends up rounding down to a clock | ||
| 88 | # tick boundary. | ||
| 89 | # | ||
| 90 | # In the unlikely event there's no sysconf() value for CLK_TCK, or no | ||
| 91 | # sysconf() func at all, assume the traditional 100 ticks/second, ie. a | ||
| 92 | # resolution of 10 milliseconds, giving a 20 ms margin. | ||
| 93 | # | ||
| 94 | use constant _TIMER_MARGIN_MILLISECONDS => do { | ||
| 95 | my $clk_tck = -1; # default -1 like the error return from sysconf() | ||
| 96 | ## no critic (RequireCheckingReturnValueOfEval) | ||
| 97 | eval { $clk_tck = POSIX::sysconf (POSIX::_SC_CLK_TCK()); }; | ||
| 98 | ### $clk_tck | ||
| 99 | if ($clk_tck <= 0) { $clk_tck = 100; } # default assume 100 Hz, 10ms tick | ||
| 100 | (2 * 1000.0 / $clk_tck) | ||
| 101 | }; | ||
| 102 | ### _TIMER_MARGIN_MILLISECONDS: _TIMER_MARGIN_MILLISECONDS() | ||
| 103 | |||
| 104 | |||
| 105 | sub INIT_INSTANCE { | ||
| 106 | my ($self) = @_; | ||
| 107 | $self->{'format'} = _DEFAULT_FORMAT; | ||
| 108 | $self->{'resolution'} = 0; # per pspec default | ||
| 109 | $self->{'decided_resolution'} = 60; # of _DEFAULT_FORMAT | ||
| 110 | $self->set_use_markup (1); | ||
| 111 | _update($self); # initial string for initial size | ||
| 112 | } | ||
| 113 | |||
| 114 | sub GET_PROPERTY { | ||
| 115 | my ($self, $pspec) = @_; | ||
| 116 | my $pname = $pspec->get_name; | ||
| 117 | |||
| 118 | if ($pname eq 'timezone_string') { | ||
| 119 | my $timezone = $self->{'timezone'}; | ||
| 120 | # For DateTime::TimeZone read back the ->name() string. | ||
| 121 | # Not yet documented. Is this a good idea? | ||
| 122 | if (Scalar::Util::blessed ($timezone)) { | ||
| 123 | $timezone = $timezone->name; | ||
| 124 | } | ||
| 125 | return $timezone; | ||
| 126 | } | ||
| 127 | |||
| 128 | return $self->{$pname}; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub SET_PROPERTY { | ||
| 132 | my ($self, $pspec, $newval) = @_; | ||
| 133 | ### Clock SET_PROPERTY: $pspec->get_name | ||
| 134 | |||
| 135 | my $pname = $pspec->get_name; | ||
| 136 | if ($pname eq 'timezone_string') { # alias | ||
| 137 | $pname = 'timezone'; | ||
| 138 | } | ||
| 139 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 140 | |||
| 141 | if ($pname eq 'timezone') { | ||
| 142 | if (Scalar::Util::blessed($newval) | ||
| 143 | && $newval->isa('DateTime::TimeZone')) { | ||
| 144 | require DateTime; | ||
| 145 | } elsif (defined $newval && $newval ne '') { | ||
| 146 | require Tie::TZ; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | if ($pname eq 'resolution' || $pname eq 'format') { | ||
| 151 | $self->{'decided_resolution'} | ||
| 152 | = $self->get('resolution') | ||
| 153 | || ($self->strftime_is_seconds($self->{'format'}) ? 1 : 60); | ||
| 154 | ### decided resolution: $self->{'decided_resolution'} | ||
| 155 | } | ||
| 156 | if ($pname eq 'timezone' || $pname eq 'format') { | ||
| 157 | _update ($self); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub _timer_callback { | ||
| 162 | my ($ref_weak_self) = @_; | ||
| 163 | ### _timer_callback() | ||
| 164 | my $self = $$ref_weak_self || return 0; # Glib::SOURCE_REMOVE | ||
| 165 | |||
| 166 | _update ($self); | ||
| 167 | |||
| 168 | # this timer should be removed by SourceIds anyway | ||
| 169 | return 0; # Glib::SOURCE_REMOVE | ||
| 170 | } | ||
| 171 | |||
| 172 | # set the label string and start or restart timer | ||
| 173 | sub _update { | ||
| 174 | my ($self) = @_; | ||
| 175 | ### Clock _update() | ||
| 176 | |||
| 177 | my $tod = Time::HiRes::time(); | ||
| 178 | my $format = $self->{'format'}; | ||
| 179 | my $timezone = $self->{'timezone'}; | ||
| 180 | |||
| 181 | my ($str, $minute, $second); | ||
| 182 | if (Scalar::Util::blessed($timezone) | ||
| 183 | && $timezone->isa('DateTime::TimeZone')) { | ||
| 184 | my $t = DateTime->from_epoch (epoch => $tod, time_zone => $timezone); | ||
| 185 | $str = $t->strftime ($format); | ||
| 186 | $minute = $t->minute; | ||
| 187 | $second = $t->second; | ||
| 188 | } else { | ||
| 189 | my @tm; | ||
| 190 | if (defined $timezone && $timezone ne '') { | ||
| 191 | ### using TZ: $timezone | ||
| 192 | no warnings 'once'; | ||
| 193 | local $Tie::TZ::TZ = $timezone; | ||
| 194 | @tm = localtime ($tod); | ||
| 195 | $str = POSIX::Wide::strftime ($format, @tm); | ||
| 196 | } else { | ||
| 197 | ### using current timezone | ||
| 198 | @tm = localtime ($tod); | ||
| 199 | $str = POSIX::Wide::strftime ($format, @tm); | ||
| 200 | } | ||
| 201 | $minute = $tm[1]; | ||
| 202 | $second = $tm[0]; | ||
| 203 | } | ||
| 204 | $self->set_label ($str); | ||
| 205 | |||
| 206 | # Decide how long in milliseconds until the next update. This is from the | ||
| 207 | # current $minute,$second,frac($tod) to the next multiple of | ||
| 208 | # $self->{'decided_resolution'} seconds, plus _TIMER_MARGIN_MILLISECONDS | ||
| 209 | # described above. | ||
| 210 | # | ||
| 211 | # If $self->{'decided_resolution'} is 1 second then $minute,$second have | ||
| 212 | # no effect and it's just from the fractional part of $tod to the next 1 | ||
| 213 | # second. Similarly if $self->{'decided_resolution'} is 60 seconds then | ||
| 214 | # $minute has no effect. | ||
| 215 | # | ||
| 216 | # Rumour has it $second can be 60 for some oddity like a TAI system clock | ||
| 217 | # displaying UTC. Dunno if it really happens, but cap at 59 just in case. | ||
| 218 | # | ||
| 219 | # In theory an mktime of $second+1, or $minute+1,$second=0, would be the | ||
| 220 | # $tod value to target. Not absolutely certain that would come out right | ||
| 221 | # if crossing a daylight savings boundary, though capping it modulo the | ||
| 222 | # resolution like ($newtod - $tod) % $self->{'decided_resolution'} would | ||
| 223 | # ensure a sensible range. Would an mktime be worthwhile? Taking just | ||
| 224 | # 60*$minute+$second is a little less work. | ||
| 225 | # | ||
| 226 | my $milliseconds = POSIX::ceil | ||
| 227 | (_TIMER_MARGIN_MILLISECONDS | ||
| 228 | + (1000 | ||
| 229 | * ($self->{'decided_resolution'} | ||
| 230 | - ((60*$minute + min(59,$second)) % $self->{'decided_resolution'}) | ||
| 231 | - ($tod - POSIX::floor($tod))))); # fraction part | ||
| 232 | |||
| 233 | ### timer: "$tod is $minute,$second wait $milliseconds to give ".($tod + $milliseconds / 1000.0) | ||
| 234 | Scalar::Util::weaken (my $weak_self = $self); | ||
| 235 | $self->{'timer'} = Glib::Ex::SourceIds->new | ||
| 236 | (Glib::Timeout->add ($milliseconds, | ||
| 237 | \&_timer_callback, \$weak_self, | ||
| 238 | Gtk2::GDK_PRIORITY_REDRAW() - 1)); # before redraws | ||
| 239 | |||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | #------------------------------------------------------------------------------ | ||
| 244 | |||
| 245 | # $format is an strftime() format string. Return true if it has 1 second | ||
| 246 | # resolution. | ||
| 247 | # | ||
| 248 | sub strftime_is_seconds { | ||
| 249 | my ($self, $format) = @_; | ||
| 250 | |||
| 251 | # %c is ctime() style, includes seconds | ||
| 252 | # %r is "%I:%M:%S %p" | ||
| 253 | # %s is seconds since 1970 (a GNU extension) | ||
| 254 | # %S is seconds 0 to 59 | ||
| 255 | # %T is "%H:%M:%S" | ||
| 256 | # %X is locale preferred time, probably "%H:%M:%S" | ||
| 257 | # modifiers standard E and O, plus GNU "-_0^" | ||
| 258 | # | ||
| 259 | # DateTime extras: | ||
| 260 | # %N is nanoseconds, which really can't work, so ignore | ||
| 261 | # | ||
| 262 | # DateTime methods: | ||
| 263 | # second() | ||
| 264 | # sec() | ||
| 265 | # hms(), time() | ||
| 266 | # datetime(), is8601() | ||
| 267 | # epoch() | ||
| 268 | # utc_rd_as_seconds() | ||
| 269 | # | ||
| 270 | # jd(), mjd() fractional part represents the time, but the decimals | ||
| 271 | # aren't a whole second so won't really display properly, ignore for now | ||
| 272 | # | ||
| 273 | $format =~ s/%%//g; # literal "%"s, so eg. "%%Something" is not "%S" | ||
| 274 | return ($format =~ /%[-_^0-9EO]* | ||
| 275 | ([crsSTX] | ||
| 276 | |\{(sec(ond)?|hms|(date)?time|iso8601|epoch|utc_rd_as_seconds)})/x); | ||
| 277 | } | ||
| 278 | |||
| 279 | 1; | ||
| 280 | __END__ | ||