File Coverage

blib/lib/XAO/DO/Web/Date.pm
Criterion Covered Total %
statement 28 38 73.6
branch 4 16 25.0
condition 2 5 40.0
subroutine 6 6 100.0
pod 1 1 100.0
total 41 66 62.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             XAO::DO::Web::Date - XAO::Web date dysplayable object
4              
5             =head1 SYNOPSIS
6              
7             <%Date%>
8              
9             <%Date gmtime="123456789" style="short"%>
10              
11             <%Date format="%H:%M"%>
12              
13             <%Date gmtime="123456789" timezone="US/Eastern"%>
14              
15             =cut
16              
17             ###############################################################################
18             package XAO::DO::Web::Date;
19 1     1   3336 use strict;
  1         2  
  1         54  
20 1     1   4 use POSIX qw(strftime);
  1         2  
  1         7  
21 1     1   80 use XAO::Utils;
  1         2  
  1         48  
22 1     1   4 use XAO::Objects;
  1         2  
  1         30  
23 1     1   4 use base XAO::Objects->load(objname => 'Web::Page');
  1         1  
  1         5  
24              
25             our $VERSION='2.001';
26              
27             ###############################################################################
28              
29             =head1 DESCRIPTION
30              
31             Displays current or given date. Arguments are:
32              
33             =over
34              
35             =item gmtime
36              
37             Number of seconds since the Epoch (unix standard time). Optional,
38             default is to display the current time.
39              
40             =item style
41              
42             Display according to one of internal styles:
43              
44             dateonly => %m/%d/%Y => 3/27/2002
45             short => %H:%M:%S %m/%d/%Y => 12:23:34 3/27/2002
46             timeonly => %H:%M:%S => 12:23:34
47              
48             =item format
49              
50             Set custom format according to strftime C function API.
51              
52             =item timezone
53              
54             May contain a name of a time zone, which must be known to the
55             system. Default is the default system timezone.
56              
57             =back
58              
59             =cut
60              
61             sub display ($;%) {
62 600     600 1 748 my $self=shift;
63 600         1060 my $args=get_args(\@_);
64              
65             ##
66             # Setting timezone
67             #
68 600         3905 my $tz;
69 600 50       1008 if($args->{timezone}) {
70 0         0 $tz=$ENV{TZ};
71 0         0 $ENV{TZ}=$args->{timezone};
72             }
73              
74             ##
75             # It can be current time or given time
76             #
77 600   33     1024 my $time=$args->{gmtime} || time;
78              
79             ##
80             # Checking output style
81             #
82 600   50     1423 my $style=$args->{style} || '';
83 600         692 my $format='';
84 600 50       874 if(!$style) {
    0          
    0          
    0          
85 600         710 $format=$args->{format};
86             }
87             elsif($style eq 'dateonly') {
88 0         0 $format='%m/%d/%Y';
89             }
90             elsif($style eq 'short') {
91 0         0 $format='%H:%M:%S %m/%d/%Y';
92             }
93             elsif($style eq 'timeonly') {
94 0         0 $format='%H:%M:%S';
95             }
96             else {
97 0         0 eprint "Unknown date style '$style'";
98             }
99              
100             ##
101             # Displaying according to format.
102             #
103 600 50       930 if($format) {
104 600         22522 $time=strftime($format,localtime($time));
105             }
106             else {
107 0         0 $time=scalar(localtime($time));
108             }
109              
110 600         2563 $self->textout($time);
111              
112             ##
113             # Restoring timezone
114             #
115 600 50       1480 if($args->{timezone}) {
116 0 0         if(defined $tz) {
117 0           $ENV{TZ}=$tz;
118             }
119             else {
120 0           delete $ENV{TZ};
121             }
122             }
123             }
124              
125             ###############################################################################
126             1;
127             __END__