1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
51 RT::Date - a simple Object Oriented date.
59 RT Date is a simple Date Object designed to be speedy and easy for RT to use.
61 The fact that it assumes that a time of 0 means "never" is probably a bug.
75 use base qw/RT::Base/;
81 use vars qw($MINUTE $HOUR $DAY $WEEK $MONTH $YEAR);
87 $MONTH = 30.4375 * $DAY;
88 $YEAR = 365.25 * $DAY;
105 our @DAYS_OF_WEEK = (
116 'DefaultFormat', # loc
122 'LocalizedDateTime', # loc
127 Object constructor takes one argument C<RT::CurrentUser> object.
133 my $class = ref($proto) || $proto;
135 bless ($self, $class);
136 $self->CurrentUser(@_);
143 Takes a param hash with the fields C<Format>, C<Value> and C<Timezone>.
145 If $args->{'Format'} is 'unix', takes the number of seconds since the epoch.
147 If $args->{'Format'} is ISO, tries to parse an ISO date.
149 If $args->{'Format'} is 'unknown', require Time::ParseDate and make it figure
150 things out. This is a heavyweight operation that should never be called from
151 within RT's core. But it's really useful for something like the textbox date
152 entry where we let the user do whatever they want.
154 If $args->{'Value'} is 0, assumes you mean never.
167 return $self->Unix(0) unless $args{'Value'} && $args{'Value'} =~ /\S/;
169 my $format = lc $args{'Format'};
171 if ( $format eq 'unix' ) {
172 return $self->Unix( $args{'Value'} );
175 ($format eq 'sql' || $format eq 'iso')
176 && $args{'Value'} =~ /^(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/
179 my $u = eval { Time::Local::timegm($6, $5, $4, $3, $2-1, $1) } || 0;
180 $RT::Logger->warning("Invalid date $args{'Value'}: $@") if $@ && !$u;
181 return $self->Unix( $u > 0 ? $u : 0 );
183 elsif ( $format =~ /^(sql|datemanip|iso)$/ ) {
184 $args{'Value'} =~ s!/!-!g;
186 if ( ( $args{'Value'} =~ /^(\d{4})?(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ )
187 || ( $args{'Value'} =~ /^(\d{4})?(\d\d)(\d\d)(\d\d):(\d\d):(\d\d)$/ )
188 || ( $args{'Value'} =~ /^(?:(\d{4})-)?(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/ )
189 || ( $args{'Value'} =~ /^(?:(\d{4})-)?(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)\+00$/ )
192 my ($year, $mon, $mday, $hours, $min, $sec) = ($1, $2, $3, $4, $5, $6);
194 # use current year if string has no value
195 $year ||= (localtime time)[5] + 1900;
197 #timegm expects month as 0->11
200 #now that we've parsed it, deal with the case where everything was 0
201 return $self->Unix(0) if $mon < 0 || $mon > 11;
203 my $tz = lc $args{'Format'} eq 'datemanip'? 'user': 'utc';
204 $self->Unix( $self->Timelocal( $tz, $sec, $min, $hours, $mday, $mon, $year ) );
206 $self->Unix(0) unless $self->Unix > 0;
209 $RT::Logger->warning(
210 "Couldn't parse date '$args{'Value'}' as a $args{'Format'} format"
212 return $self->Unix(0);
215 elsif ( $format eq 'unknown' ) {
216 require Time::ParseDate;
217 # the module supports only legacy timezones like PDT or EST...
218 # so we parse date as GMT and later apply offset, this only
219 # should be applied to absolute times, so compensate shift in NOW
221 $now += ($self->Localtime( $args{Timezone}, $now ))[9];
222 my ($date, $error) = Time::ParseDate::parsedate(
226 UK => RT->Config->Get('DateDayBeforeMonth'),
227 PREFER_PAST => RT->Config->Get('AmbiguousDayInPast'),
228 PREFER_FUTURE => RT->Config->Get('AmbiguousDayInFuture'),
230 unless ( defined $date ) {
231 $RT::Logger->warning(
232 "Couldn't parse date '$args{'Value'}' by Time::ParseDate"
234 return $self->Unix(0);
237 # apply timezone offset
238 $date -= ($self->Localtime( $args{Timezone}, $date ))[9];
241 "RT::Date used Time::ParseDate to make '$args{'Value'}' $date\n"
244 return $self->Unix($date || 0);
248 "Unknown Date format: $args{'Format'}\n"
250 return $self->Unix(0);
258 Set the object's time to the current time. Takes no arguments
259 and returns unix time.
264 return $_[0]->Unix(time);
267 =head2 SetToMidnight [Timezone => 'utc']
269 Sets the date to midnight (at the beginning of the day).
270 Returns the unixtime at midnight.
278 Timezone context C<user>, C<server> or C<UTC>. See also L</Timezone>.
286 my %args = ( Timezone => '', @_ );
287 my $new = $self->Timelocal(
289 0,0,0,($self->Localtime( $args{'Timezone'} ))[3..9]
291 return $self->Unix( $new );
294 =head2 SetToStart PERIOD[, Timezone => 'utc' ]
296 Set to the beginning of the current PERIOD, which can be
297 "year", "month", "day", "hour", or "minute".
305 my $tz = $args{'Timezone'} || '';
306 my @localtime = $self->Localtime($tz);
307 #remove 'offset' so that DST is figured based on the resulting time.
310 # This is the cleanest way to implement it, I swear.
313 last if ($p eq 'MINUTE');
315 last if ($p eq 'HOUR');
317 last if ($p eq 'DAY');
319 last if ($p eq 'MONTH');
321 last if ($p eq 'YEAR');
322 $RT::Logger->warning("Couldn't find start date of '$p'.");
325 my $new = $self->Timelocal($tz, @localtime);
326 return $self->Unix($new);
331 Takes either an C<RT::Date> object or the date in unixtime format as a string,
332 if nothing is specified uses the current time.
334 Returns the differnce between the time in the current object and that time
335 as a number of seconds. Returns C<undef> if any of two compared values is
336 incorrect or not set.
343 $other = time unless defined $other;
344 if ( UNIVERSAL::isa( $other, 'RT::Date' ) ) {
345 $other = $other->Unix;
347 return undef unless $other=~ /^\d+$/ && $other > 0;
349 my $unix = $self->Unix;
350 return undef unless $unix > 0;
352 return $unix - $other;
357 Takes either an C<RT::Date> object or the date in unixtime format as a string,
358 if nothing is specified uses the current time.
360 Returns the differnce between C<$self> and that time as a number of seconds as
361 a localized string fit for human consumption. Returns empty string if any of
362 two compared values is incorrect or not set.
368 my $diff = $self->Diff( @_ );
369 return '' unless defined $diff;
371 return $self->DurationAsString( $diff );
374 =head2 DurationAsString
376 Takes a number of seconds. Returns a localized string describing
379 Takes optional named arguments:
385 How many elements to show, how precise it should be. Default is 1,
390 Turn on short notation with one character units, for example
397 sub DurationAsString {
399 my $duration = int shift;
400 my %args = ( Show => 1, Short => 0, @_ );
402 unless ( $duration ) {
403 return $args{Short}? $self->loc("0s") : $self->loc("0 seconds");
407 $negative = 1 if $duration < 0;
408 $duration = abs $duration;
414 while ( $duration > 0 && ++$i <= $args{'Show'} ) {
417 if ( $duration < $MINUTE ) {
418 $locstr = $args{Short}
420 : '[quant,_1,second,seconds]'; # loc
423 elsif ( $duration < ( $coef * $HOUR ) ) {
424 $locstr = $args{Short}
426 : '[quant,_1,minute,minutes]'; # loc
429 elsif ( $duration < ( $coef * $DAY ) ) {
430 $locstr = $args{Short}
432 : '[quant,_1,hour,hours]'; # loc
435 elsif ( $duration < ( $coef * $WEEK ) ) {
436 $locstr = $args{Short}
438 : '[quant,_1,day,days]'; # loc
441 elsif ( $duration < ( $coef * $MONTH ) ) {
442 $locstr = $args{Short}
444 : '[quant,_1,week,weeks]'; # loc
447 elsif ( $duration < $YEAR ) {
448 $locstr = $args{Short}
450 : '[quant,_1,month,months]'; # loc
454 $locstr = $args{Short}
456 : '[quant,_1,year,years]'; # loc
459 my $value = int( $duration / $unit + ($i < $args{'Show'}? 0 : 0.5) );
460 $duration -= int( $value * $unit );
462 push @res, $self->loc($locstr, $value);
468 return $self->loc( "[_1] ago", join ' ', @res );
471 return join ' ', @res;
477 Takes nothing. Returns a string that's the difference between the
478 time in the object and now.
482 sub AgeAsString { return $_[0]->DiffAsString }
488 Returns the object's time as a localized string with curent user's preferred
491 If the current user didn't choose preferred format then system wide setting is
492 used or L</DefaultFormat> if the latter is not specified. See config option
501 return $self->loc("Not set") unless $self->IsSet;
503 my $format = RT->Config->Get( 'DateTimeFormat', $self->CurrentUser ) || 'DefaultFormat';
504 $format = { Format => $format } unless ref $format;
505 %args = (%$format, %args);
507 return $self->Get( Timezone => 'user', %args );
510 =head2 GetWeekday DAY
512 Takes an integer day of week and returns a localized string for
513 that day of week. Valid values are from range 0-6, Note that B<0
522 return $self->loc($DAYS_OF_WEEK[$dow])
523 if $DAYS_OF_WEEK[$dow];
527 =head2 GetMonth MONTH
529 Takes an integer month and returns a localized string for that month.
530 Valid values are from from range 0-11.
538 return $self->loc($MONTHS[$mon])
543 =head2 AddSeconds SECONDS
545 Takes a number of seconds and returns the new unix time.
547 Negative value can be used to substract seconds.
553 my $delta = shift or return $self->Unix;
555 $self->Set(Format => 'unix', Value => ($self->Unix + $delta));
557 return ($self->Unix);
560 =head2 AddDays [DAYS]
562 Adds C<24 hours * DAYS> to the current time. Adds one day when
563 no argument is specified. Negative value can be used to substract
566 Returns new unix time.
573 $days = 1 unless defined $days;
574 return $self->AddSeconds( $days * $DAY );
579 Adds 24 hours to the current time. Returns new unix time.
583 sub AddDay { return $_[0]->AddSeconds($DAY) }
587 Adds one month to the current time. Returns new
595 my @localtime = $self->Localtime($args{'Timezone'});
596 # remove offset, as with SetToStart
599 $localtime[4]++; #month
600 if ( $localtime[4] == 12 ) {
602 $localtime[5]++; #year
605 my $new = $self->Timelocal($args{'Timezone'}, @localtime);
606 return $self->Unix($new);
609 =head2 Unix [unixtime]
611 Optionally takes a date in unix seconds since the epoch format.
612 Returns the number of seconds since the epoch
620 my $time = int(shift || 0);
622 RT->Logger->notice("Passed a unix time less than 0, forcing to 0: [$time]");
625 $self->{'time'} = int $time;
627 return $self->{'time'};
632 Alias for L</Get> method. Arguments C<Date> and C<Time>
633 are fixed to true values, other arguments could be used
634 as described in L</Get>.
640 unless (defined $self) {
641 use Carp; Carp::confess("undefined $self");
643 return $self->Get( @_, Date => 1, Time => 1 );
648 Takes Format argument which allows you choose date formatter.
649 Pass throught other arguments to the formatter method.
651 Returns the object's formatted date. Default formatter is ISO.
657 return $self->Get( @_, Date => 1, Time => 0 );
667 return $self->Get( @_, Date => 0, Time => 1 );
672 Returns a formatted and localized string that represents the time of
681 my %args = (Format => 'ISO', @_);
682 my $formatter = $args{'Format'};
683 unless ( $self->ValidFormatter($formatter) ) {
684 RT->Logger->warning("Invalid date formatter '$formatter', falling back to ISO");
687 $formatter = 'ISO' unless $self->can($formatter);
688 return $self->$formatter( %args );
691 =head2 Output formatters
693 Fomatter is a method that returns date and time in different configurable
696 Each method takes several arguments:
704 =item Timezone - Timezone context C<server>, C<user> or C<UTC>
708 Formatters may also add own arguments to the list, for example
709 in RFC2822 format day of time in output is optional so it
710 understands boolean argument C<DayOfTime>.
714 Returns an array of available formatters.
725 =head3 ValidFormatter FORMAT
727 Returns a true value if C<FORMAT> is a known formatter. Otherwise returns
735 return (grep { $_ eq $format } $self->Formatters and $self->can($format))
746 my %args = ( Date => 1,
753 # 0 1 2 3 4 5 6 7 8 9
754 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
755 $self->Localtime($args{'Timezone'});
756 $wday = $self->GetWeekday($wday);
757 $mon = $self->GetMonth($mon);
758 $_ = sprintf "%02d", $_ foreach $mday, $hour, $min, $sec;
760 if( $args{'Date'} && !$args{'Time'} ) {
761 return $self->loc('[_1] [_2] [_3] [_4]',
762 $wday,$mon,$mday,$year);
763 } elsif( !$args{'Date'} && $args{'Time'} ) {
764 if( $args{'Seconds'} ) {
765 return $self->loc('[_1]:[_2]:[_3]',
768 return $self->loc('[_1]:[_2]',
772 if( $args{'Seconds'} ) {
773 return $self->loc('[_1] [_2] [_3] [_4]:[_5]:[_6] [_7]',
774 $wday,$mon,$mday,$hour,$min,$sec,$year);
776 return $self->loc('[_1] [_2] [_3] [_4]:[_5] [_6]',
777 $wday,$mon,$mday,$hour,$min,$year);
784 Returns the L<DateTime::Locale> object representing the current user's locale.
791 my $lang = $self->CurrentUser->UserObj->Lang;
793 require I18N::LangTags::Detect;
794 $lang = ( I18N::LangTags::Detect::detect(), 'en' )[0];
797 return DateTime::Locale->load($lang);
800 =head3 LocalizedDateTime
802 Returns date and time as string, with user localization.
804 Supports arguments: C<DateFormat> and C<TimeFormat> which may contains date and
805 time format as specified in L<DateTime::Locale> (default to C<date_format_full> and
806 C<time_format_medium>), C<AbbrDay> and C<AbbrMonth> which may be set to 0 if
807 you want full Day/Month names instead of abbreviated ones.
811 sub LocalizedDateTime
814 my %args = ( Date => 1,
824 # Require valid names for the format methods
825 my $date_format = $args{DateFormat} =~ /^\w+$/
826 ? $args{DateFormat} : 'date_format_full';
828 my $time_format = $args{TimeFormat} =~ /^\w+$/
829 ? $args{TimeFormat} : 'time_format_medium';
831 my $formatter = $self->LocaleObj;
832 $date_format = $formatter->$date_format;
833 $time_format = $formatter->$time_format;
834 $date_format =~ s/EEEE/EEE/g if ( $args{'AbbrDay'} );
835 $date_format =~ s/MMMM/MMM/g if ( $args{'AbbrMonth'} );
837 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
838 $self->Localtime($args{'Timezone'});
840 my $tz = $self->Timezone($args{'Timezone'});
842 # FIXME : another way to call this module without conflict with local
844 my $dt = DateTime::->new( locale => $formatter,
855 if ( $args{'Date'} && !$args{'Time'} ) {
856 return $dt->format_cldr($date_format);
857 } elsif ( !$args{'Date'} && $args{'Time'} ) {
858 return $dt->format_cldr($time_format);
860 return $dt->format_cldr($date_format) . " " . $dt->format_cldr($time_format);
866 Returns the object's date in ISO format C<YYYY-MM-DD mm:hh:ss>.
867 ISO format is locale-independent, but adding timezone offset info
868 is not implemented yet.
870 Supports arguments: C<Timezone>, C<Date>, C<Time> and C<Seconds>.
871 See L</Output formatters> for description of arguments.
877 my %args = ( Date => 1,
883 # 0 1 2 3 4 5 6 7 8 9
884 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
885 $self->Localtime($args{'Timezone'});
887 #the month needs incrementing, as gmtime returns 0-11
891 $res .= sprintf("%04d-%02d-%02d", $year, $mon, $mday) if $args{'Date'};
892 $res .= sprintf(' %02d:%02d', $hour, $min) if $args{'Time'};
893 $res .= sprintf(':%02d', $sec) if $args{'Time'} && $args{'Seconds'};
901 Returns the object's date and time in W3C date time format
902 (L<http://www.w3.org/TR/NOTE-datetime>).
904 Format is locale-independent and is close enough to ISO, but
905 note that date part is B<not optional> and output string
906 has timezone offset mark in C<[+-]hh:mm> format.
908 Supports arguments: C<Timezone>, C<Time> and C<Seconds>.
909 See L</Output formatters> for description of arguments.
922 # 0 1 2 3 4 5 6 7 8 9
923 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
924 $self->Localtime( $args{'Timezone'} );
926 #the month needs incrementing, as gmtime returns 0-11
930 $res .= sprintf("%04d-%02d-%02d", $year, $mon, $mday);
931 if ( $args{'Time'} ) {
932 $res .= sprintf('T%02d:%02d', $hour, $min);
933 $res .= sprintf(':%02d', $sec) if $args{'Seconds'};
935 $res .= sprintf "%s%02d:%02d", $self->_SplitOffset( $offset );
945 =head3 RFC2822 (MIME)
947 Returns the object's date and time in RFC2822 format,
948 for example C<Sun, 06 Nov 1994 08:49:37 +0000>.
949 Format is locale-independent as required by RFC. Time
950 part always has timezone offset in digits with sign prefix.
952 Supports arguments: C<Timezone>, C<Date>, C<Time>, C<DayOfWeek>
953 and C<Seconds>. See L</Output formatters> for description of
960 my %args = ( Date => 1,
968 # 0 1 2 3 4 5 6 7 8 9
969 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
970 $self->Localtime($args{'Timezone'});
972 my ($date, $time) = ('','');
973 $date .= "$DAYS_OF_WEEK[$wday], " if $args{'DayOfWeek'} && $args{'Date'};
974 $date .= sprintf("%02d %s %04d", $mday, $MONTHS[$mon], $year) if $args{'Date'};
976 if ( $args{'Time'} ) {
977 $time .= sprintf("%02d:%02d", $hour, $min);
978 $time .= sprintf(":%02d", $sec) if $args{'Seconds'};
979 $time .= sprintf " %s%02d%02d", $self->_SplitOffset( $offset );
982 return join ' ', grep $_, ($date, $time);
985 =head3 RFC2616 (HTTP)
987 Returns the object's date and time in RFC2616 (HTTP/1.1) format,
988 for example C<Sun, 06 Nov 1994 08:49:37 GMT>. While the RFC describes
989 version 1.1 of HTTP, but the same form date can be used in version 1.0.
991 Format is fixed-length, locale-independent and always represented in GMT
992 which makes it quite useless for users, but any date in HTTP transfers
993 must be presented using this format.
995 HTTP-date = rfc1123 | ...
996 rfc1123 = wkday "," SP date SP time SP "GMT"
997 date = 2DIGIT SP month SP 4DIGIT
998 ; day month year (e.g., 02 Jun 1982)
999 time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
1000 ; 00:00:00 - 23:59:59
1001 wkday = "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun"
1002 month = "Jan" | "Feb" | "Mar" | "Apr" | "May" | "Jun"
1003 | "Jul" | "Aug" | "Sep" | "Oct" | "Nov" | "Dec"
1005 Supports arguments: C<Date> and C<Time>, but you should use them only for
1006 some personal reasons, RFC2616 doesn't define any optional parts.
1007 See L</Output formatters> for description of arguments.
1013 my %args = ( Date => 1, Time => 1,
1016 Seconds => 1, DayOfWeek => 1,
1019 my $res = $self->RFC2822( %args );
1020 $res =~ s/\s*[+-]\d\d\d\d$/ GMT/ if $args{'Time'};
1026 Returns the object's date and time in iCalendar format.
1027 If only date requested then user's timezone is used, otherwise
1030 Supports arguments: C<Date> and C<Time>.
1031 See L</Output formatters> for description of arguments.
1038 Date => 1, Time => 1,
1043 if ( $args{'Date'} && !$args{'Time'} ) {
1044 my (undef, undef, undef, $mday, $mon, $year) =
1045 $self->Localtime( 'user' );
1046 $res = sprintf( '%04d%02d%02d', $year, $mon+1, $mday );
1047 } elsif ( !$args{'Date'} && $args{'Time'} ) {
1048 my ($sec, $min, $hour) =
1049 $self->Localtime( 'utc' );
1050 $res = sprintf( 'T%02d%02d%02dZ', $hour, $min, $sec );
1052 my ($sec, $min, $hour, $mday, $mon, $year) =
1053 $self->Localtime( 'utc' );
1054 $res = sprintf( '%04d%02d%02dT%02d%02d%02dZ', $year, $mon+1, $mday, $hour, $min, $sec );
1059 # it's been added by mistake in 3.8.0
1060 sub iCalDate { return (shift)->iCal( Time => 0, @_ ) }
1063 my ($self, $offset) = @_;
1064 my $sign = $offset < 0? '-': '+';
1065 $offset = int( (abs $offset) / 60 + 0.001 );
1066 my $mins = $offset % 60;
1067 my $hours = int( $offset/60 + 0.001 );
1068 return $sign, $hours, $mins;
1071 =head2 Timezones handling
1073 =head3 Localtime $context [$time]
1075 Takes one mandatory argument C<$context>, which determines whether
1076 we want "user local", "system" or "UTC" time. Also, takes optional
1077 argument unix C<$time>, default value is the current unix time.
1079 Returns object's date and time in the format provided by perl's
1080 builtin functions C<localtime> and C<gmtime> with two exceptions:
1086 "Year" is a four-digit year, rather than "years since 1900"
1090 The last element of the array returned is C<offset>, which
1091 represents timezone offset against C<UTC> in seconds.
1100 my $tz = $self->Timezone(shift);
1102 my $unix = shift || $self->Unix;
1103 $unix = 0 unless $unix >= 0;
1107 @local = gmtime($unix);
1110 local $ENV{'TZ'} = $tz;
1111 ## Using POSIX::tzset fixes a bug where the TZ environment variable
1114 @local = localtime($unix);
1116 POSIX::tzset(); # return back previous value
1118 $local[5] += 1900; # change year to 4+ digits format
1119 my $offset = Time::Local::timegm_nocheck(@local) - $unix;
1120 return @local, $offset;
1123 =head3 Timelocal $context @time
1125 Takes argument C<$context>, which determines whether we should
1126 treat C<@time> as "user local", "system" or "UTC" time.
1128 C<@time> is array returned by L</Localtime> functions. Only first
1129 six elements are mandatory - $sec, $min, $hour, $mday, $mon and $year.
1130 You may pass $wday, $yday and $isdst, these are ignored.
1132 If you pass C<$offset> as ninth argument, it's used instead of
1133 C<$context>. It's done such way as code
1134 C<< $self->Timelocal('utc', $self->Localtime('server')) >> doesn't
1135 make much sense and most probably would produce unexpected
1136 results, so the method ignores 'utc' context and uses the offset
1137 returned by the L</Localtime> method.
1144 if ( defined $_[9] ) {
1145 return timegm(@_[0..5]) - $_[9];
1147 $tz = $self->Timezone( $tz );
1148 if ( $tz eq 'UTC' ) {
1149 return Time::Local::timegm(@_[0..5]);
1153 local $ENV{'TZ'} = $tz;
1154 ## Using POSIX::tzset fixes a bug where the TZ environment variable
1157 $rv = Time::Local::timelocal(@_[0..5]);
1159 POSIX::tzset(); # switch back to previouse value
1166 =head3 Timezone $context
1168 Returns the timezone name for the specified context. C<$context>
1169 should be one of these values:
1175 The current user's Timezone value will be returned.
1179 The value of the C<Timezone> RT config option will be returned.
1183 For any other value of C<$context>, or if the specified context has no
1184 defined timezone, C<UTC> is returned.
1192 Carp::carp 'RT::Date->Timezone requires a context argument';
1196 my $context = lc(shift);
1199 if( $context eq 'user' ) {
1200 $tz = $self->CurrentUser->UserObj->Timezone;
1201 } elsif( $context eq 'server') {
1202 $tz = RT->Config->Get('Timezone');
1206 $tz ||= RT->Config->Get('Timezone') || 'UTC';
1207 $tz = 'UTC' if lc $tz eq 'gmt';
1213 Returns true if this Date is set in the database, otherwise returns a false value.
1215 This avoids needing to compare to 1970-01-01 in any of your code.
1221 return $self->Unix ? 1 : 0;
1226 RT::Base->_ImportOverlays();