[webmail logged me out, so sorry if you get ++ copies]
> Flavio S. Glock <[EMAIL PROTECTED]> wrote:
> So this is the current list of proposed
DT::Incomplete methods that
> are
> "waiting for votes":
>
> * epoch
> $epoch = $dti->epoch if $dti->can_be_datetime;
Sounds good
sub epoch {
my $self = shift;
return undef unless $self->can_be_datetime;
return $self->clone->become_datetime->epoch;
}
> * has_date / has_time
Sounds good, see below and:
sub has_time { $_[0]->has{'hour', 'minute'} }
sub has_date { $_[0]->has{'year', 'month', 'day'} }
> * keys or defined_fields
> returns the list of names of "defined" fields
sub has {
# called with parameters you get true or false
# called with no params and you get a list of fields
my $self = shift;
if (@_) {
foreach (@_) {
return 0 unless exists $self->{has}{$_}
}
return 1
}
return keys %{$self->{has}}
}
Which gives:
print "Got hour" if $dti->has('hour');
print "Got time" if $dti->has('hour', 'minute')
and
print "Object has " . join(', ', @{$dti->has} );
> * join( $dti )
> join the "defined" keys of two DT::Incomplete
objects
'join' feels like 'append'. 'union' feels better, but relates
to sets. Maybe 'merge'? Also consider 'overlay'.
dti1 2003-xx-21Txx:18:xx.12345
dti2 2003-09-22T15:17:xx.xxxxx
merge: 2003-09-21T15:18:xx.12345
overlay: 2003-09-22T15:17:xx.12345
Note that $dti1->overlay($dti2) is the same as
$dti2->merge($dti1);
sub merge { # not 'submerge'!!!
my $self = shift;
my $dti = shift;
die "Argument to merge must be a DateTime or
DateTime::Incomplete" unless
ref($dti)=~/^DateTime(::Incomplete)?$/;
# There must be a better way to do this:
$self->{has}{year} ||= $dti->year;
$self->{has}{month} ||= $dti->month;
$self->{has}{day} ||= $dti->day;
$self->{has}{hour} ||= $dti->hour;
$self->{has}{minute} ||= $dti->minute;
$self->{has}{second} ||= $dti->second;
$self->{has}{nanosecond} ||= $dti->nanosecond;
$self->{has}{time_zone} ||= $dti->time_zone;
$self->{has}{locale} ||= $dti->locale;
return $self
}
sub overlay { $_[0] = $_[1]->clone->merge( $_[0] ) }
### PROBABLY A VERY UN-KOSHER WAY TO DO
IT! ###
> * is_incomplete / is_complete
sub is_complete { $_[0]->has('year', 'month', 'day',
'hour', 'minute', 'second', 'nanosecond') }
sub is_incomplete { ($_[0]->is_complete) ? 0 : 1 }
> * can_be_datetime / become_datetime
sub can_be_datetime {
my $self = shift;
# YYYY-MM-DD HH:MM:SS
# YYYY-MM-DD HH:MM:SS.NNNNNNNNN
return 1 if $self->has('year', 'month', 'day', 'hour',
'minute', 'second');
# YYYY-MM-DD HH:MM
# YYYY-MM-DD HH:MM::SS, but is already caught
return 1 if $self->has('year', 'month', 'day', 'hour',
'minute') and not $self->has('nanosecond');
# YYYY-MM-DD
return 1 if $self->has('year', 'month', 'day')
and not ($self->has('hour')
or $self->has('minute')
or $self->has('second')
or $self->has('nanosecond')
);
return 0;
}
sub become_datetime {
my $self = shift;
return undef unless $self->can_be_datetime;
my $dt = DateTime->new(
year => $self->year,
month => $self->month,
day => $self->day,
);
$dt->set_time_zone( $self->time_zone )
if $self->has('time_zone');
$dt->set( locale => $self->locale )
if $self->has('locale');
$dt->set( hour => $self->hour, minute => $minute )
if $self->has('hour', 'minute');
$dt->set( second => $self->second )
if $self->has('second');
$dt->set( nanosecond => $self->nanosecond )
if $self->has('nanosecond');
$self = $dt; #### DOES THAT DO THE TRICK, OR
IS IT MORE COMPLEX? ####
return $self;
}
Above code passes all tests on the perl installed in
my head. Havn't tried any of it with the much fussier
software version.
perl -v
This is perl, v5.8.0 built for ricks-brain-1.0