File: TZ_Base.pm

package info (click to toggle)
libdate-manip-perl 6.98-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,928 kB
  • sloc: perl: 222,846; sh: 54; ansic: 26; makefile: 8
file content (421 lines) | stat: -rw-r--r-- 10,954 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package Date::Manip::TZ_Base;
# Copyright (c) 2010-2025 Sullivan Beck. All rights reserved.
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

########################################################################
########################################################################

require 5.010000;
use warnings;
use strict;
use IO::File;
use Carp;

our ($VERSION);
$VERSION='6.98';
END { undef $VERSION; }

########################################################################
# METHODS
########################################################################

sub _config_var {
   my($self,$var,$val) = @_;
   $var = lc($var);

   # A simple flag used to force a new configuration, but has
   # no other affect.
   return  if ($var eq 'ignore');

   my $istz     = ref($self) eq 'Date::Manip::TZ';

   if ($istz  &&  ($var eq 'tz'         ||
                   $var eq 'forcedate'  ||
                   $var eq 'setdate'    ||
                   $var eq 'configfile')) {
      if ($var eq 'tz') {
         carp "WARNING: the TZ Date::Manip config variable is deprecated\n" .
              "         and will be removed in version 7.00.  Please use\n" .
              "         the SetDate or ForceDate config variables instead.\n";
      }
      return $self->_config_var_tz($var,$val);
   } else {
      my $base  = ($istz ? $$self{'base'} : $self);
      return $base->_config_var_base($var,$val);
   }
}

# This reads a config file
#
sub _config_file {
   my($self,$file) = @_;

   return  if (! $file);

   if (! -f $file) {
      carp "ERROR: [config_file] file doesn't exist: $file";
      return;
   }
   if (! -r $file) {
      carp "ERROR: [config_file] file not readable: $file";
      return;
   }

   my $in = new IO::File;
   if (! $in->open($file)) {
      carp "ERROR: [config_file] unable to open file: $file: $!";
      return;
   }
   my @in = <$in>;
   $in->close();

   my $sect = 'conf';
   my %sect;

   chomp(@in);
   foreach my $line (@in) {
      $line =~ s/^\s+//o;
      $line =~ s/\s+$//o;
      next  if (! $line  or  $line =~ /^\043/o);

      if ($line =~ /^\*/o) {
         # New section
         $sect = $self->_config_file_section($line);
      } else {
         $sect{$sect} = 1;
         $self->_config_file_var($sect,$line);
      }
   }

   # If we did a holidays section, we need to create a regular
   # expression with all of the holiday names.

   my $istz  = ref($self) eq 'Date::Manip::TZ';
   my $base  = ($istz ? $$self{'base'} : $self);

   if (exists $sect{'holidays'}) {
      my @hol = @{ $$base{'data'}{'sections'}{'holidays'} };
      my @nam;
      while (@hol) {
         my $junk = shift(@hol);
         my $hol  = shift(@hol);
         push(@nam,$hol)  if ($hol);
      }

      if (@nam) {
         @nam    = sort _sortByLength(@nam);
         my $hol = '(?<holiday>' . join('|',map { "\Q$_\E" } @nam) . ')';
         my $yr  = '(?<y>\d\d\d\d|\d\d)';

         my $rx  = "$hol\\s*$yr|" .      # Christmas 2009
                   "$yr\\s*$hol|" .      # 2009 Christmas
                   "$hol";               # Christmas

         $$base{'data'}{'rx'}{'holidays'} = qr/^(?:$rx)$/i;
      }
   }
}

sub _config_file_section {
   my($self,$line) = @_;

   my $istz  = ref($self) eq 'Date::Manip::TZ';
   my $base  = ($istz ? $$self{'base'} : $self);

   $line    =~ s/^\*//o;
   $line    =~ s/\s*$//o;
   my $sect = lc($line);
   if (! exists $$base{'data'}{'sections'}{$sect}) {
      carp "WARNING: [config_file] unknown section created: $sect";
      $base->_section($sect);
   }
   return $sect;
}

sub _config_file_var {
   my($self,$sect,$line) = @_;

   my $istz  = ref($self) eq 'Date::Manip::TZ';
   my $base  = ($istz ? $$self{'base'} : $self);

   my($var,$val);
   if ($line =~ /^\s*(.*?)\s*=\s*(.*?)\s*$/o) {
      ($var,$val) = ($1,$2);
   } else {
      croak "ERROR: invalid Date::Manip config file line:\n  $line\n";
   }

   if ($sect eq 'conf') {
      $var = lc($var);
      $self->_config($var,$val);
   } else {
      $base->_section($sect,$var,$val);
   }
}

# $val = $self->config(VAR);
#    Returns the value of a variable.
#
# $self->config([SECT], VAR, VAL)  sets the value of a variable
#    Sets the value of a variable.
#
sub _config {
   my($self,$var,$val) = @_;

   my $sect = 'conf';

   #
   # $self->_conf(VAR, VAL)  sets the value of a variable
   #

   $var = lc($var);
   if (defined $val) {
      return $self->_config_var($var,$val);
   }

   #
   # $self->_conf(VAR)       returns the value of a variable
   #

   if (exists $$self{'data'}{'sections'}{$sect}{$var}) {
      return $$self{'data'}{'sections'}{$sect}{$var};
   } else {
      carp "ERROR: [config] invalid config variable: $var";
      return '';
   }
}

########################################################################

sub _fix_year {
   my($self,$y) = @_;
   my $istz     = ref($self) eq 'Date::Manip::TZ';
   my $base     = ($istz ? $self->base() : $self);

   my $method   = $base->_config('yytoyyyy');

   return $y     if (length($y)==4);
   return undef  if (length($y)!=2);

   my $curr_y;
   if (ref($self) eq 'Date::Manip::TZ') {
      $curr_y  = $self->_now('y',1);
   } else {
      $curr_y  = ( localtime(time) )[5];
      $curr_y += 1900;
   }

   if ($method eq 'c') {
      return substr($curr_y,0,2) . $y;

   } elsif ($method =~ /^c(\d\d)$/) {
      return "$1$y";

   } elsif ($method =~ /^c(\d\d)(\d\d)$/) {
      return "$1$y" + ($y<$2 ? 100 : 0);

   } else {
      my $y1      = $curr_y - $method;
      my $y2      = $y1 + 99;
      $y1         =~ /^(\d\d)/;
      $y          = "$1$y";
      if ($y<$y1) {
         $y += 100;
      }
      if ($y>$y2) {
         $y -= 100;
      }
      return $y;
   }
}

###############################################################################
# Functions for setting the default date/time

# Many date operations use a default time and/or date to set some
# or all values.  This function may be used to set or examine the
# default time.
#
# _now allows you to get the current date and/or time in the
# local timezone.
#
# The function performed depends on $op and are described in the
# following table:
#
#    $op                  function
#    ------------------   ----------------------------------
#    undef                Returns the current default values
#                         (y,m,d,h,mn,s) without updating
#                         the time (it'll update if it has
#                         never been set).
#
#    'now'                Updates now and returns
#                         (y,m,d,h,mn,s)
#
#    'time'               Updates now and Returns (h,mn,s)
#
#    'y'                  Returns the default value of one
#    'm'                  of the fields (no update)
#    'd'
#    'h'
#    'mn'
#    's'
#
#    'systz'              Returns the system timezone
#
#    'isdst'              Returns the 'now' values if set,
#    'tz'                 or system time values otherwise.
#    'offset'
#    'abb'
#
sub _now {
   my($self,$op,$noupdate) = @_;
   my $istz      = ref($self) eq 'Date::Manip::TZ';
   my $base      = ($istz ? $self->base() : $self);

   # Update "NOW" if we're checking 'now', 'time', or the date
   # is not set already.

   if (! defined $noupdate) {
      if ($op =~ /(?:now|time)/) {
         $noupdate = 0;
      } else {
         $noupdate = 1;
      }
   }
   $noupdate = 0  if (! exists $$base{'data'}{'now'}{'date'});
   $self->_update_now()  unless ($noupdate);

   # Now return the value of the operation

   my @tmpnow   = @{ $$base{'data'}{'tmpnow'} };
   my @now      = (@tmpnow ? @tmpnow : @{ $$base{'data'}{'now'}{'date'} });

   if ($op eq 'tz') {
      if (exists $$base{'data'}{'now'}{'tz'}) {
         return $$base{'data'}{'now'}{'tz'};
      } else {
         return $$base{'data'}{'now'}{'systz'};
      }

   } elsif ($op eq 'systz') {
      return $$base{'data'}{'now'}{'systz'};

   } elsif ($op eq 'isdst') {
      return $$base{'data'}{'now'}{'isdst'};

   } elsif ($op eq 'offset') {
      return @{ $$base{'data'}{'now'}{'offset'} };

   } elsif ($op eq 'abb') {
      return $$base{'data'}{'now'}{'abb'};

   } elsif ($op eq 'now') {
      return @now;

   } elsif ($op eq 'y') {
      return $now[0];

   } elsif ($op eq 'time') {
      return @now[3..5];

   } elsif ($op eq 'm') {
      return $now[1];

   } elsif ($op eq 'd') {
      return $now[2];

   } elsif ($op eq 'h') {
      return $now[3];

   } elsif ($op eq 'mn') {
      return $now[4];

   } elsif ($op eq 's') {
      return $now[5];

   } else {
      carp "ERROR: [now] invalid argument list: $op";
      return ();
   }
}

sub _update_now {
   my($self) = @_;
   my $istz     = ref($self) eq 'Date::Manip::TZ';
   my $base     = ($istz ? $self->base() : $self);

   # If we've called ForceDate, don't change it.
   return  if ($$base{'data'}{'now'}{'force'});

   # If we've called SetDate (which will only happen if a
   # Date::Manip:TZ object is available), figure out what 'now' is
   # based on the number of seconds that have elapsed since it was
   # set.  This will ONLY happen if TZ has been loaded.

   if ($$base{'data'}{'now'}{'set'}) {
      my $date = $$base{'data'}{'now'}{'setdate'};
      my $secs = time - $$base{'data'}{'now'}{'setsecs'};

      $date      = $base->calc_date_time($date,[0,0,$secs]);  # 'now' in GMT
      my $zone   = $self->_now('tz',1);
      my ($err,$date2,$offset,$isdst,$abbrev) = $self->convert_from_gmt($date,$zone);

      $$base{'data'}{'now'}{'date'}   = $date2;
      $$base{'data'}{'now'}{'isdst'}  = $isdst;
      $$base{'data'}{'now'}{'offset'} = $offset;
      $$base{'data'}{'now'}{'abb'}    = $abbrev;
      return;
   }

   # Otherwise, we'll use the system time.

   my $time = time;
   my($s,$mn,$h,$d,$m,$y,$wday,$yday,$isdst) = localtime($time);
   my($s0,$mn0,$h0,$d0,$m0,$y0)              = gmtime($time);

   $y += 1900;
   $m++;

   $y0 += 1900;
   $m0++;

   my $off = $base->calc_date_date([$y,$m,$d,$h,$mn,$s],[$y0,$m0,$d0,$h0,$mn0,$s0],1);

   $$base{'data'}{'now'}{'date'}  = [$y,$m,$d,$h,$mn,$s];
   $$base{'data'}{'now'}{'isdst'} = $isdst;
   $$base{'data'}{'now'}{'offset'}= $off;

   my $abb = '???';
   if (ref($self) eq 'Date::Manip::TZ') {
      my $zone   = $self->_now('tz',1);
      my $per    = $self->date_period([$y,$m,$d,$h,$mn,$s],$zone,1,$isdst);
      $abb = $$per[4];
   }

   $$base{'data'}{'now'}{'abb'}   = $abb;

   return;
}

###############################################################################
# This sorts from longest to shortest element
#
no strict 'vars';
sub _sortByLength {
   return (length $b <=> length $a);
}
use strict 'vars';

1;
# Local Variables:
# mode: cperl
# indent-tabs-mode: nil
# cperl-indent-level: 3
# cperl-continued-statement-offset: 2
# cperl-continued-brace-offset: 0
# cperl-brace-offset: 0
# cperl-brace-imaginary-offset: 0
# cperl-label-offset: 0
# End: