+Supports arguments: C<Date> and C<Time>, but you should use them only for
+some personal reasons, RFC2616 doesn't define any optional parts.
+See </Output formatters> for description of arguments.
+
+=cut
+
+sub RFC2616 {
+ my $self = shift;
+ my %args = ( Date => 1, Time => 1,
+ @_,
+ Timezone => 'utc',
+ Seconds => 1, DayOfWeek => 1,
+ );
+
+ my $res = $self->RFC2822( %args );
+ $res =~ s/\s*[+-]\d\d\d\d$/ GMT/ if $args{'Time'};
+ return $res;
+}
+
+=head4 iCal
+
+Returns the object's date and time in iCalendar format,
+
+Supports arguments: C<Date> and C<Time>.
+See </Output formatters> for description of arguments.
+
+=cut
+
+sub iCal {
+ my $self = shift;
+ my %args = (
+ Date => 1, Time => 1,
+ @_,
+ );
+ my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydaym,$isdst,$offset) =
+ $self->Localtime( 'utc' );
+
+ #the month needs incrementing, as gmtime returns 0-11
+ $mon++;
+
+ my $res;
+ if ( $args{'Date'} && !$args{'Time'} ) {
+ $res = sprintf( '%04d%02d%02d', $year, $mon, $mday );
+ }
+ elsif ( !$args{'Date'} && $args{'Time'} ) {
+ $res = sprintf( 'T%02d%02d%02dZ', $hour, $min, $sec );
+ }
+ else {
+ $res = sprintf( '%04d%02d%02dT%02d%02d%02dZ', $year, $mon, $mday, $hour, $min, $sec );
+ }
+ return $res;
+}
+
+# it's been added by mistake in 3.8.0
+sub iCalDate { return (shift)->iCal( Time => 0, @_ ) }
+
+sub _SplitOffset {
+ my ($self, $offset) = @_;
+ my $sign = $offset < 0? '-': '+';
+ $offset = int( (abs $offset) / 60 + 0.001 );
+ my $mins = $offset % 60;
+ my $hours = int( $offset/60 + 0.001 );
+ return $sign, $hours, $mins;
+}
+
+=head2 Timezones handling
+
+=head3 Localtime $context [$time]
+
+Takes one mandatory argument C<$context>, which determines whether
+we want "user local", "system" or "UTC" time. Also, takes optional
+argument unix C<$time>, default value is the current unix time.
+
+Returns object's date and time in the format provided by perl's
+builtin functions C<localtime> and C<gmtime> with two exceptions:
+
+1) "Year" is a four-digit year, rather than "years since 1900"
+
+2) The last element of the array returned is C<offset>, which
+represents timezone offset against C<UTC> in seconds.
+
+=cut
+
+sub Localtime
+{
+ my $self = shift;
+ my $tz = $self->Timezone(shift);
+
+ my $unix = shift || $self->Unix;
+ $unix = 0 unless $unix >= 0;
+
+ my @local;
+ if ($tz eq 'UTC') {
+ @local = gmtime($unix);
+ } else {
+ {
+ local $ENV{'TZ'} = $tz;
+ ## Using POSIX::tzset fixes a bug where the TZ environment variable
+ ## is cached.
+ POSIX::tzset();
+ @local = localtime($unix);
+ }
+ POSIX::tzset(); # return back previouse value
+ }
+ $local[5] += 1900; # change year to 4+ digits format
+ my $offset = Time::Local::timegm_nocheck(@local) - $unix;
+ return @local, $offset;
+}
+
+=head3 Timelocal $context @time
+
+Takes argument C<$context>, which determines whether we should
+treat C<@time> as "user local", "system" or "UTC" time.
+
+C<@time> is array returned by L<Localtime> functions. Only first
+six elements are mandatory - $sec, $min, $hour, $mday, $mon and $year.
+You may pass $wday, $yday and $isdst, these are ignored.
+
+If you pass C<$offset> as ninth argument, it's used instead of
+C<$context>. It's done such way as code
+C<$self->Timelocal('utc', $self->Localtime('server'))> doesn't
+makes much sense and most probably would produce unexpected
+result, so the method ignore 'utc' context and uses offset
+returned by L<Localtime> method.
+
+=cut
+
+sub Timelocal {
+ my $self = shift;
+ my $tz = shift;
+ if ( defined $_[9] ) {
+ return timegm(@_[0..5]) - $_[9];
+ } else {
+ $tz = $self->Timezone( $tz );
+ if ( $tz eq 'UTC' ) {
+ return Time::Local::timegm(@_[0..5]);
+ } else {
+ my $rv;
+ {
+ local $ENV{'TZ'} = $tz;
+ ## Using POSIX::tzset fixes a bug where the TZ environment variable
+ ## is cached.
+ POSIX::tzset();
+ $rv = Time::Local::timelocal(@_[0..5]);
+ };
+ POSIX::tzset(); # switch back to previouse value
+ return $rv;
+ }
+ }
+}
+
+
+=head3 Timezone $context
+
+Returns the timezone name.
+
+Takes one argument, C<$context> argument which could be C<user>, C<server> or C<utc>.
+
+=over
+
+=item user
+
+Default value is C<user> that mean it returns current user's Timezone value.
+
+=item server
+
+If context is C<server> it returns value of the C<Timezone> RT config option.
+
+=item utc
+
+If both server's and user's timezone names are undefined returns 'UTC'.
+
+=back
+
+=cut
+
+sub Timezone {
+ my $self = shift;
+ my $context = lc(shift);
+
+ $context = 'utc' unless $context =~ /^(?:utc|server|user)$/i;
+
+ my $tz;
+ if( $context eq 'user' ) {
+ $tz = $self->CurrentUser->UserObj->Timezone;
+ } elsif( $context eq 'server') {
+ $tz = RT->Config->Get('Timezone');
+ } else {
+ $tz = 'UTC';
+ }
+ $tz ||= RT->Config->Get('Timezone') || 'UTC';
+ $tz = 'UTC' if lc $tz eq 'gmt';
+ return $tz;