Skip Menu |

This queue is for tickets about the RT-Client-REST CPAN distribution.

Report information
The Basics
Id: 73920
Status: resolved
Priority: 0/
Queue: RT-Client-REST

People
Owner: Nobody in particular
Requestors: bitcard [...] davel.me.uk
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: 0.43



Subject: Date handling patch
I have a patch, attached, which adds additional accessors for tickets returning a timestamp, converting the string field to and from a DateTime object. Kind regards, Dave Lambley
Subject: datetime.patch
diff --git a/rt-client-rest/Makefile.PL b/rt-client-rest/Makefile.PL index eadb97c..c939cd2 100644 --- a/rt-client-rest/Makefile.PL +++ b/rt-client-rest/Makefile.PL @@ -12,6 +12,8 @@ requires 'HTTP::Cookies' => 0; requires 'HTTP::Request::Common' => 0; requires 'LWP' => 0; requires 'Params::Validate' => 0; +requires 'DateTime' => 0, +requires 'DateTime::Format::DateParse' => 0, test_requires 'Test::More'; test_requires 'Test::Exception' => 0; diff --git a/rt-client-rest/lib/RT/Client/REST/Object.pm b/rt-client-rest/lib/RT/Client/REST/Object.pm index f1a18c9..79e6d80 100644 --- a/rt-client-rest/lib/RT/Client/REST/Object.pm +++ b/rt-client-rest/lib/RT/Client/REST/Object.pm @@ -144,6 +144,8 @@ use Error qw(:try); use Params::Validate; use RT::Client::REST::Object::Exception 0.04; use RT::Client::REST::SearchResult 0.02; +use DateTime; +use DateTime::Format::DateParse; =item new @@ -261,6 +263,32 @@ sub _generate_methods { } }; + if ($settings->{is_datetime}) { + *{$class. '::' . $method . "_datetime"} = sub { + # All dates are in UTC + # http://requesttracker.wikia.com/wiki/REST#Data_format + + my ($self) = shift; + my $real_method = $class.'::'.$method; + if (@_) { + unless ($_[0]->isa('DateTime')) { + RT::Client::REST::Object::InvalidValueException + ->throw( + "'@_[0]' is not a valid value for attribute '${method}_datetime'" + ); + + } + my $z = $_[0]->clone; + $z->set_time_zone("UTC"); + $self->$method($_[0]->strftime("%a %b %d %T %Y")); + return $z; + } + + return DateTime::Format::DateParse->parse_datetime($self->$method, 'UTC'); + + }; + } + if ($settings->{list}) { # Generate convenience methods for list manipulation. my $add_method = $class . '::add_' . $method; diff --git a/rt-client-rest/lib/RT/Client/REST/Ticket.pm b/rt-client-rest/lib/RT/Client/REST/Ticket.pm index c7cbb96..47c3cbf 100644 --- a/rt-client-rest/lib/RT/Client/REST/Ticket.pm +++ b/rt-client-rest/lib/RT/Client/REST/Ticket.pm @@ -162,36 +162,42 @@ sub _attributes {{ validation => { type => SCALAR, }, + is_datetime => 1, }, starts => { validation => { type => SCALAR|UNDEF, }, + is_datetime => 1, }, started => { validation => { type => SCALAR|UNDEF, }, + is_datetime => 1, }, due => { validation => { type => SCALAR|UNDEF, }, + is_datetime => 1, }, resolved => { validation => { type => SCALAR|UNDEF, }, + is_datetime => 1, }, told => { validation => { type => SCALAR|UNDEF, }, + is_datetime => 1, }, time_estimated => { @@ -220,6 +226,7 @@ sub _attributes {{ type => SCALAR, }, rest_name => 'LastUpdated', + is_datetime => 1, }, }} @@ -313,6 +320,13 @@ and therefore the value cannot be changed.. =back +=head2 Attributes storing a time + +The attributes which store a time stamp have an additional accessor with the +suffix C<_datetime> (eg., C<resolved_datetime>). This allows you can get and +set the stored value as a DateTime object. Internally, it is converted into +the date-time string which RT uses, which is assumed to be in UTC. + =head1 DB METHODS For full explanation of these, please see B<"DB METHODS"> in diff --git a/rt-client-rest/t/22-ticket.t b/rt-client-rest/t/22-ticket.t index 8382da5..24e91bc 100644 --- a/rt-client-rest/t/22-ticket.t +++ b/rt-client-rest/t/22-ticket.t @@ -1,14 +1,14 @@ use strict; use warnings; -use Test::More tests => 97; +use Test::More tests => 113; use Test::Exception; use constant METHODS => ( 'new', 'to_form', 'from_form', 'rt_type', 'comment', 'correspond', 'attachments', 'transactions', 'take', 'untake', 'steal', - # attrubutes: + # attributes: 'id', 'queue', 'owner', 'creator', 'subject', 'status', 'priority', 'initial_priority', 'final_priority', 'requestors', 'cc', 'admin_cc', 'created', 'starts', 'started', 'due', 'resolved', 'told', @@ -180,4 +180,41 @@ ok(4 == $ticket->requestors, 'There are now 4 requestors'); ok('ticket' eq $ticket->rt_type); +# Test time parsing +$ticket->due('Thu Jan 12 11:14:31 2012'); +my $dt = $ticket->due_datetime(); +is($dt->year, 2012); +is($dt->month, 1); +is($dt->day, 12); +is($dt->hour, 11); +is($dt->minute, 14); +is($dt->second, 31); +is($dt->time_zone->name, 'UTC'); + +$dt = DateTime->new( + year => 1983, + month => 9, + day => 1, + hour => 1, + minute => 2, + second => 3, + time_zone => 'EST' +); + +$dt=$ticket->due_datetime($dt); + +is($dt->year, 1983); +is($dt->month, 9); +is($dt->day, 1); +is($dt->hour, 6); +is($dt->minute, 2); +is($dt->second, 3); +is($dt->time_zone->name, 'UTC'); + +is($ticket->due, 'Thu Sep 01 01:02:03 1983'); + +throws_ok { + $ticket->due_datetime(bless {}, 'foo'); +} 'RT::Client::REST::Object::InvalidValueException'; + # vim:ft=perl:
Applied as r54, will be in the next release. Thanks for the patch!