IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 29750


Ignore:
Timestamp:
Nov 10, 2010, 3:36:30 PM (15 years ago)
Author:
rhenders
Message:

Neatened up time diff code. Now much safer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/czartool/MySQLDb.pm

    r29741 r29750  
    131131###########################################################################
    132132#
     133# Formats a date/time string in a way suitable for TIMEDIFF calls
     134#
     135###########################################################################
     136sub formatTimeForDiff {
     137    my ($self, $time) = @_;
     138
     139    if (${$time} =~ m/^[0-9]{4}-[0-9]{2}-[0-9]{2}.*/) {${$time} = $self->getFullDate(${$time});}
     140    elsif(${$time} !~ m/^[0-9]+:[0-9]{2}:[0-9]{2}$/) {return 0;}
     141
     142    return 1;
     143}
     144
     145###########################################################################
     146#
    133147# Finds the difference of two times
     148#
     149# Should be able to handle most date/time formats
    134150#
    135151###########################################################################
     
    139155    if (!$time1 || !$time2) {return 0;}
    140156
     157    if (!$self->formatTimeForDiff(\$time1) || !$self->formatTimeForDiff(\$time2)) {return 0;}
     158
    141159    my $query = $self->{_db}->prepare(<<SQL);
    142160         SELECT TIMEDIFF('$time1','$time2');
     
    174192
    175193    if (!$time1 || !$time2) {return 0;}
     194    if (!$self->formatTimeForDiff(\$time1) || !$self->formatTimeForDiff(\$time2)) {return 0;}
    176195
    177196    my $query = $self->{_db}->prepare(<<SQL);
     
    207226    my ($self, $time1, $time2) = @_;
    208227
    209       my $query = $self->{_db}->prepare(<<SQL);
    210           SELECT TIME_TO_SEC(TIMEDIFF('$time1', '$time2'));
    211 SQL
    212     $query->execute;
    213 
    214 return (scalar $query->fetchrow_array() > 0) ? 0 : 1;
     228    if (!$time1 || !$time2) {return 0;}
     229    if (!$self->formatTimeForDiff(\$time1) || !$self->formatTimeForDiff(\$time2)) {return 0;}
     230
     231    my $query = $self->{_db}->prepare(<<SQL);
     232    SELECT TIME_TO_SEC(TIMEDIFF('$time1', '$time2'));
     233SQL
     234    $query->execute;
     235
     236    return (scalar $query->fetchrow_array() >= 0) ? 0 : 1;
    215237}
    216238
Note: See TracChangeset for help on using the changeset viewer.