| 1 | # $Id: DateTimeField.pm,v 1.5 2005/10/05 20:39:34 mjb47 Exp $ | ||
| 2 | |||
| 3 | package DBIx::XMLServer::DateTimeField; | ||
| 4 | use XML::LibXML; | ||
| 5 | use Date::Manip qw( Date_Init UnixDate ); | ||
| 6 | our @ISA = ('DBIx::XMLServer::Field'); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | DBIx::XMLServer::DateTimeField - date and time field type | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This class implements the built-in date and time field type of | ||
| 15 | DBIx::XMLServer. The B<where> and B<value> methods are overridden | ||
| 16 | from the base class. | ||
| 17 | |||
| 18 | =head2 B<where> method | ||
| 19 | |||
| 20 | $sql_expression = $date_time_field->where($condition); | ||
| 21 | |||
| 22 | The condition may consist of one of the numeric comparison operators | ||
| 23 | '=', '>', '<', '>=' or '<=', followed by a date and time. The date | ||
| 24 | and time may be in any format understood by the C<Date::Manip> | ||
| 25 | package, such as '2003-11-03 21:29:10' or 'yesterday at midnight'. | ||
| 26 | |||
| 27 | Alternatively, the condition may be empty, in which case the SQL expression | ||
| 28 | is | ||
| 29 | |||
| 30 | <field> IS NOT NULL . | ||
| 31 | |||
| 32 | If the condition is the character '!', then the SQL expression is | ||
| 33 | |||
| 34 | <field> IS NULL . | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub BEGIN { Date_Init("DateFormat = EU"); } | ||
| 39 | |||
| 40 | sub where { | ||
| 41 | my $self = shift; | ||
| 42 | my $cond = shift; | ||
| 43 | return "$column IS NOT NULL" if $cond eq ''; | ||
| 44 | return "$column IS NULL" if $cond eq '!'; | ||
| 45 | my $column = $self->select; | ||
| 46 | my ($comp, $date) = ($cond =~ /([=<>]+)(.*)/); | ||
| 47 | defined $comp or die "Unrecognised date condition: $cond\n"; | ||
| 48 | $comp =~ /^(=|[<>]=?)$/ or die "Unrecognised date/time comparison: $comp\n"; | ||
| 49 | my $date1 = UnixDate($date, '%q') or die "Unrecognised date/time: $date\n"; | ||
| 50 | return "$column $comp " . $date1; | ||
| 51 | } | ||
| 52 | |||
| 53 | =head2 B<value> method | ||
| 54 | |||
| 55 | The date and time is returned in the format 'YYYY-mm-ddThh:mm:ss', as | ||
| 56 | required by the B<xsi:datetime> type in XML Schema. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub value { | ||
| 61 | shift; | ||
| 62 | return UnixDate(shift @{shift()}, '%Y-%m-%dT%T'); | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | |||
| 67 | __END__ |