Changeset 197


Ignore:
Timestamp:
Feb 15, 2010, 4:29:55 PM (7 years ago)
Author:
jahn
Message:

Modified convenience function for displaying date differences to provide formatting also for dates that do not belog to the Event class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/domain/dbnp/studycapturing/Event.groovy

    r190 r197  
    2525
    2626
    27                 // time diff between end and start date
    28                 // thus, do this manually as follows
    29         def getDuration() {
    30                 def timeMillis = (endTime.getTime() - startTime.getTime()).abs()
     27        // time diff between end and start date
     28        // thus, do this manually as follows
     29        def getDuration( date1, date2 ) {
     30                def timeMillis = (date2.getTime() - date1.getTime()).abs()
    3131                def days = (timeMillis / (1000 * 60 * 60 * 24)).toInteger()
    3232                def hours = (timeMillis / (1000 * 60 * 60)).toInteger()
     
    3939
    4040
    41              // return a string that prints the duration sensibly.
    42              // the largest date unit (sec, min, h, day, week, month, or year)
    43              // is output
     41        def getDuration() {
     42                return getDuration(startTime, endTime)
     43        }
     44
     45
     46        // return a string that prints the duration sensibly.
     47        // the largest date unit (sec, min, h, day, week, month, or year)
     48        // is output
     49        def getPrettyDuration( duration ) {
     50             def handleNumerus = { number ,string ->
     51                     return number.toString() + (number==1 ? string : string + 's' )
     52             }
     53             if( duration.getYears()   > 0 ) return handleNumerus( duration.getYears(), " year" )
     54             if( duration.getMonths()  > 0 ) return handleNumerus( duration.getMonths(), " month" )
     55             if( duration.getDays()    > 0 ) return handleNumerus( duration.getDays(), " day" )
     56             if( duration.getHours()   > 0 ) return handleNumerus( duration.getHours(), " hour" )
     57             if( duration.getMinutes() > 0 ) return handleNumerus( duration.getMinutes(), " minute" )
     58                                             return handleNumerus( duration.getSeconds(), " second" )
     59        }
     60
     61
     62        // convenience method. gives formatted string output for a duration
    4463        def getPrettyDuration() {
    45              def duration = getDuration()
    46              if( duration.getYears()   > 0 ) return duration.getYears()   + " years"
    47              if( duration.getMonths()  > 0 ) return duration.getMonths()  + " months"
    48              if( duration.getWeeks()   > 0 ) return duration.getWeeks()   + " weeks"
    49              if( duration.getDays()    > 0 ) return duration.getDays()    + " days"
    50              if( duration.getHours()   > 0 ) return duration.getHours()   + " minutes"
    51                                              return duration.getSeconds() + " seconds"
     64             getPrettyDuration( getDuration() )
     65        }
     66
     67
     68        // convenience method. gives formatted string output for a duration
     69        def getPrettyDuration( date1, date2 )
     70        {
     71             return  getPrettyDuration( getDuration( date1, date2 ) )
    5272        }
    5373
     
    6282                return ( this instanceof SamplingEvent )
    6383        }
     84
    6485}
Note: See TracChangeset for help on using the changeset viewer.