IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 26, 2010, 10:33:29 AM (16 years ago)
Author:
eugene
Message:

merge updates from trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20101103/tools/czartool/MySQLDb.pm

    r29436 r29834  
    6464    return $self->{_dbHost};                                                 
    6565}                                                                               
    66 ###########################################################################
    67 #
    68 # Adds the provided interval to the provided time
     66
     67###########################################################################
     68#
     69# Returns a date formatted wit hthe current formatting
    6970#
    7071###########################################################################
     
    8283###########################################################################
    8384#
     85# Returns a full MySQL formatted date
     86#
     87###########################################################################
     88sub getFullDate {
     89    my ($self, $time) = @_;
     90
     91    my $query = $self->{_db}->prepare(<<SQL);
     92    SELECT DATE_FORMAT('$time','%Y-%m-%d %H:%i:%s');
     93SQL
     94    $query->execute;
     95
     96    return scalar $query->fetchrow_array();
     97}
     98
     99###########################################################################
     100#
    84101# Adds the provided interval to the provided time
    85102#
     
    98115###########################################################################
    99116#
     117# Subtracts the provided interval from the provided time
     118#
     119###########################################################################
     120sub subtractInterval {
     121    my ($self, $time, $interval) = @_;
     122
     123      my $query = $self->{_db}->prepare(<<SQL);
     124          SELECT '$time' - INTERVAL $interval;
     125SQL
     126    $query->execute;
     127
     128return scalar $query->fetchrow_array();
     129}
     130
     131###########################################################################
     132#
     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#
    100147# Finds the difference of two times
     148#
     149# Should be able to handle most date/time formats
    101150#
    102151###########################################################################
     
    104153    my ($self, $time1, $time2) = @_;
    105154
    106       my $query = $self->{_db}->prepare(<<SQL);
     155    if (!$time1 || !$time2) {return 0;}
     156
     157    if (!$self->formatTimeForDiff(\$time1) || !$self->formatTimeForDiff(\$time2)) {return 0;}
     158
     159    my $query = $self->{_db}->prepare(<<SQL);
    107160         SELECT TIMEDIFF('$time1','$time2');
     161SQL
     162
     163    $query->execute;
     164    return scalar $query->fetchrow_array();
     165}
     166
     167###########################################################################
     168#
     169# Returns the provided time in seconds
     170#
     171###########################################################################
     172sub getTimeInSecs {
     173    my ($self, $time) = @_;
     174
     175    if (!$time) {return 0;}
     176
     177      my $query = $self->{_db}->prepare(<<SQL);
     178         SELECT TIME_TO_SEC('$time');
    108179SQL
    109180
     
    120191    my ($self, $time1, $time2) = @_;
    121192
    122       my $query = $self->{_db}->prepare(<<SQL);
     193    if (!$time1 || !$time2) {return 0;}
     194    if (!$self->formatTimeForDiff(\$time1) || !$self->formatTimeForDiff(\$time2)) {return 0;}
     195
     196    my $query = $self->{_db}->prepare(<<SQL);
    123197         SELECT TIME_TO_SEC(TIMEDIFF('$time1','$time2'));
    124198SQL
     
    146220###########################################################################
    147221#
    148 # Subtracts the provided interval from the provided time
    149 #
    150 ###########################################################################
    151 sub subtractInterval {
    152     my ($self, $time, $interval) = @_;
    153 
    154       my $query = $self->{_db}->prepare(<<SQL);
    155           SELECT '$time' - INTERVAL $interval;
    156 SQL
    157     $query->execute;
    158 
    159 return scalar $query->fetchrow_array();
    160 }
    161 
    162 ###########################################################################
    163 #
    164222# Returns whether time1 is before time2
    165223#
     
    168226    my ($self, $time1, $time2) = @_;
    169227
    170       my $query = $self->{_db}->prepare(<<SQL);
    171           SELECT '$time1' < '$time2';
    172 SQL
    173     $query->execute;
    174 
    175 return scalar $query->fetchrow_array();
     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;
    176237}
    177238
Note: See TracChangeset for help on using the changeset viewer.