Update Date.pm in RT2

During implementing an integration between RT2 and SugarCRM I improved date module – RT::Date. I’ve added possibility to display dates according to predefined format. Currently there is a method AsString() which simple returns scalar from localtime():
scalar(localtime()); #Thu Nov 24 10:13:54 2005 for now, for example
I’ve added a new method – AsFormattedString() which get the format as parameter and returns a date according format definition:

sub AsFormattedString {
my $self = shift;
my $format = shift || 'br';
my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($self->Unix);
$year += 1900;
$mon = sprintf("%02d", $mon + 1);
$mday = sprintf("%02d", $mday);
$hour = sprintf("%02d", $hour);
$min = sprintf("%02d", $min);
$sec = sprintf("%02d", $sec);
return eval($DATE_FORMATS{$format});
}

Also, I’ve defined some formats (currently there are onlyfour but you can add others if you need):

%DATE_FORMATS = (
br => '"$year-$mon-$mday $hour:$min:$sec"',
d_br => '"$year-$mon-$mday"',
su => "$day/$mon/$year $hour:$min:$sec",
d_su => "$day/$mon/$year",
);

A full patch available here.

Leave a Reply

Your email address will not be published. Required fields are marked *