File: DateTime.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (366 lines) | stat: -rw-r--r-- 8,967 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
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

# Adapted from DateTime package to avoid requirement of DateTime package.

package MT::DateTime;

use Exporter;
@MT::DateTime::ISA = qw( Exporter );
use vars qw( @EXPORT_OK );
@EXPORT_OK = qw( ymd2rd tz_offset_as_seconds );

use MT::Util qw( epoch2ts );

sub new {
    my $class   = shift;
    my (%param) = @_;
    my $self    = \%param;
    bless $self, $class || __PACKAGE__;
}

sub week_year   { ( shift->week )[0] }
sub week_number { ( shift->week )[1] }

sub year      { shift->{year} }
sub month     { shift->{month} }
sub day       { shift->{day} }
sub hour      { shift->{hour} }
sub minute    { shift->{minute} }
sub second    { shift->{second} }
sub time_zone { shift->{time_zone} }

sub day_of_year {
    my $self = shift;
    return $self->{local_c}{day_of_year} if $self->{local_c}{day_of_year};

    my $year = $self->year;
    my $days = 0;

    require MT::Util;
    for ( my $i = 1; $i < $self->month; $i++ ) {
        $days += MT::Util::days_in( $i, $year );
    }
    $days += $self->day;
    $self->{local_c}{day_of_year} = $days;
}

sub week {
    my $self = shift;

    unless ( defined $self->{local_c}{week_year} ) {
        my $jan_one_dow_m1
            = ( ( $self->ymd2rd( $self->year, 1, 1 ) + 6 ) % 7 );

        $self->{local_c}{week_number}
            = int( ( ( $self->day_of_year ) + $jan_one_dow_m1 ) / 7 );
        $self->{local_c}{week_number}++ if $jan_one_dow_m1 < 4;

        if ( $self->{local_c}{week_number} == 0 ) {
            $self->{local_c}{week_year} = $self->year - 1;
            $self->{local_c}{week_number}
                = $self->weeks_in_year( $self->{local_c}{week_year} );
        }
        elsif ($self->{local_c}{week_number} == 53
            && $self->weeks_in_year( $self->year ) == 52 )
        {
            $self->{local_c}{week_number} = 1;
            $self->{local_c}{week_year}   = $self->year + 1;
        }
        else {
            $self->{local_c}{week_year} = $self->year;
        }
    }

    return @{ $self->{local_c} }{ 'week_year', 'week_number' };
}

sub weeks_in_year {
    my $self = shift;
    my $year = shift;

    my $jan_one_dow = ( ( $self->ymd2rd( $year, 1,  1 ) + 6 ) % 7 ) + 1;
    my $dec_31_dow  = ( ( $self->ymd2rd( $year, 12, 31 ) + 6 ) % 7 ) + 1;

    return $jan_one_dow == 4 || $dec_31_dow == 4 ? 53 : 52;
}

sub ymd2rd {
    my $self = shift;

    use integer;
    my ( $y, $m, $d );
    if (@_) {
        ( $y, $m, $d ) = @_;
    }
    elsif ( ref $self ) {
        ( $y, $m, $d ) = ( $self->{year}, $self->{month}, $self->{day} );
    }

    my $adj;

    # make month in range 3..14 (treat Jan & Feb as months 13..14 of
    # prev year)
    if ( $m <= 2 ) {
        $y -= ( $adj = ( 14 - $m ) / 12 );
        $m += 12 * $adj;
    }
    elsif ( $m > 14 ) {
        $y += ( $adj = ( $m - 3 ) / 12 );
        $m -= 12 * $adj;
    }

    # make year positive (oh, for a use integer 'sane_div'!)
    if ( $y < 0 ) {
        $d -= 146097 * ( $adj = ( 399 - $y ) / 400 );
        $y += 400 * $adj;
    }

    # add: day of month, days of previous 0-11 month period that began
    # w/March, days of previous 0-399 year period that began w/March
    # of a 400-multiple year), days of any 400-year periods before
    # that, and 306 days to adjust from Mar 1, year 0-relative to Jan
    # 1, year 1-relative (whew)

    $d
        += ( $m * 367 - 1094 ) / 12 
        + $y % 100 * 1461 / 4
        + ( $y / 100 * 36524 + $y / 400 ) - 306;
}

sub tz_offset_as_seconds {
    my $self   = shift;
    my $offset = shift;
    if ( ref $self ) {
        $offset = $self->{time_zone};
    }

    return undef unless defined $offset;

    return 0 if $offset eq '0';

    my ( $sign, $hours, $minutes, $seconds );
    if ( $offset =~ /^([\+\-])?(\d\d?):(\d\d)(?::(\d\d))?$/ ) {
        ( $sign, $hours, $minutes, $seconds ) = ( $1, $2, $3, $4 );
    }
    elsif ( $offset =~ /^([\+\-])?(\d\d)(\d\d)(\d\d)?$/ ) {
        ( $sign, $hours, $minutes, $seconds ) = ( $1, $2, $3, $4 );
    }
    else {
        return undef;
    }

    $sign = '+' unless defined $sign;
    return undef unless $hours >= 0   && $hours <= 99;
    return undef unless $minutes >= 0 && $minutes <= 59;
    return undef
        unless !defined($seconds) || ( $seconds >= 0 && $seconds <= 59 );

    my $total = $hours * 3600 + $minutes * 60;
    $total += $seconds if $seconds;
    $total *= -1 if $sign eq '-';

    return $total;
}

sub _param2ts {
    my ( $param, $blog ) = @_;

    my ( $type, $value );
    if ( 'HASH' eq ref($param) ) {
        $type  = $param->{type};
        $value = $param->{value};
    }
    else {
        $type  = 'ts';
        $value = $param;
    }
    if ( 'CODE' eq ref($value) ) {
        $value = $value->();
    }

    my $ts;
    if ( 'epoch' eq $type ) {
        $ts = epoch2ts( $blog, $value );
    }
    elsif ( 'datetime' eq $type ) {
        $ts = sprintf "%04d%02d%02d%02d%02d%02d",
            $value->year,
            $value->month,
            $value->day,
            $value->hour,
            $value->minute,
            $value->second;
    }
    else {
        $ts = $value;
    }
    $ts;
}

sub compare {
    my $self  = shift;
    my %param = @_;

    # a => $ts | CODE | { value => CODE|$v, type => ts|epoch|datetime }
    # b => $ts | CODE | { value => CODE|$v, type => ts|epoch|datetime }
    # blog => ref|N|undef
    # comparer => CODE|undef

    my $blog = $param{blog};
    if ( defined($blog) && !ref($blog) ) {
        $blog = MT->model('blog')->load($blog);
        $blog = undef unless ref($blog);
    }

    if ( !exists( $param{a} ) && ref($self) ) {
        $param{a} = { value => $self, type => 'datetime' };
    }
    my $ts_a = _param2ts( $param{a}, $blog );

    if ( !exists( $param{b} ) && ref($self) ) {
        $param{b} = { value => $self, type => 'datetime' };
    }
    my $ts_b = _param2ts( $param{b}, $blog );

    my $comparer = $param{code};
    if ( 'CODE' eq ref($comparer) ) {
        return $comparer->( $ts_a, $ts_b );
    }
    else {
        return $ts_a - $ts_b;
    }
}

1;
__END__

=head1 NAME

MT::DateTime - A utility package for handling date/time values for Movable
Type.

=head1 METHODS

=head2 MT::DateTime->new(%attr)

Constructs a new C<MT::DateTime> object using the values in C<%attr>.
C<%attr> may contain:

=over 4

=item * year

A 4-digit year.

=item * month

Month number, where January is 0.

=item * day

Day number, from 1 to 31.

=item * hour

Hour in 24 hour notation (0-23).

=item * minute

Minutes (0-59).

=item * second

Seconds (0-59).

=item * time_zone

Timezone, in '+HH:MM', '-HH:MM', '+HH', or '-HH' notation.

=back

=head2 compare( a => $a, b => $b, blog => $blog )

Compares two timestamp strings and returns negative valye, 0, or
positive value depending on whether the "a" argument is less than,
equal to, or greater than the "b" argument.

You can specify scalar value to "a" and "b".  They are treated as
timestamp values (e.g. 20080405123456).  You can also specify either
epoch string (e.g the string returned from time method), or C<MT::DateTime>
object.  In those cases, you must specify both value and type in a hash,
for example:

    MT::DateTime->compare( blog => $blog,
        a => '20041231123456', b => { value => time(), type => 'epoch' } );

=head2 $datetime->week_year()

Returns the year for the start of the week for the object.

=head2 $datetime->week_number()

Returns the week number calculated for the object.

=head2 $datetime->year()

Returns the year component of the object.

=head2 $datetime->month()

Returns the month component of the object.

=head2 $datetime->day()

Returns the day component of the object.

=head2 $datetime->hour()

Returns the hours component of the object.

=head2 $datetime->minute()

Returns the minutes component of the object.

=head2 $datetime->second()

Returns the seconds component of the object.

=head2 $datetime->time_zone()

Returns the time zone component of the object.

=head2 $datetime->day_of_year()

Returns the day number of the year of the object.

=head2 $datetime->week()

Returns a list containing the year of the week (C<week_year>) and
the week number (C<week_number>).

=head2 MT::DateTime->weeks_in_year($year)

Returns the number of weeks that are in the specified C<$year>. Returns
either 52 or 53, depending on the year.

=head2 $datetime->ymd2rd( [ $year, $month, $day ])

Converts the given C<$year>, C<$month>, C<$day> (or, if unspecified,
uses the year, month, day elements from C<$datetime>) into a 'Rata Die'
days value.

=head2 $datetime->tz_offset_as_seconds( [$offset] )

Converts the given C<$offset> (or, if absent, uses the time_zone
component of C<$datetime>) into an expression of seconds. I.e.,
an C<$offset> of '-1:30' would yield -5400.

=head1 AUTHOR & COPYRIGHT

Please see L<MT/AUTHOR & COPYRIGHT>.

=cut