class DateInfo
def initialize
end
def interpretDate( input )
input = input.to_s.downcase
t = nil
currentDate = Time.local(Time.now.year, Time.now.mon, Time.now.day)
if( input =~ /\A(\d+)\s*(-|\/|\\)\s*(\d+)\s*((-|\/|\\)\s*(\d+))?/ )
month = $1.to_i
day = $3.to_i
if( $6 )
year = $6.to_i
else
year = Time.new.year
end
t = Time.local( year, month, day )
elsif( input =~ /\A(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[^\s]* (\d+)(.*)/ )
month = $1
day = $2.to_i
if ( $3 =~ /\s*(\,|)\s*(\d\d\d\d)/ )
year = $2.to_i
else
year = Time.new.year
end
t = Time.gm( year, month, day )
elsif( input =~ /\A(\d\d\d\d)(\d\d)(\d\d)\Z/ )
month = $2.to_i
day = $3.to_i
year = $1.to_i
t = Time.local( year, month, day )
elsif( input =~ /\A(\d\d\d\d)\Z/ )
year = $1.to_i
t = Time.local( year, 1, 1 )
elsif( input =~ /(this |last |next |)(sun|mon|tue|wed|thu|fri|sat)[^\s]*/ )
t = currentDate
wdays = %w(sun mon tue wed thu fri sat)
if( $1 == "last " )
t -= (86400*7)
elsif( $1 == "next " )
t += (86400*7)
end
distance = wdays.index( $2 ) - currentDate.wday
distance += 7 if ( distance < 0 )
t += (distance*86400)
elsif( input =~ /today/ )
t = Time.now
elsif( input =~ /tomorrow/ )
t = Time.now + 86400
elsif( input =~ /yesterday/ )
t = Time.now - 86400
else
return nil
end
return t
end
def startMonth( input = nil )
if( input == nil )
t = Time.local(Time.now.year, Time.now.mon, Time.now.day)
else
t = interpretDate( input )
end
return nil if( t == nil )
stringDate = t.strftime("%m/01/%y")
return stringDate
end
def endMonth( input = nil )
if( input == nil )
t = Time.local(Time.now.year, Time.now.mon, Time.now.day)
else
t = interpretDate( input )
end
return nil if( t == nil )
curMonth = t.strftime("%m").to_i
curYear = t.strftime("%y").to_i
if( curMonth == 4 || curMonth == 6 || curMonth == 9 || curMonth == 11 )
stringDate = t.strftime("%m/30/%y")
elsif( curMonth == 2 && Date.gregorian_leap?( curYear ) )
stringDate = t.strftime("%m/29/%y")
elsif( curMonth == 2 && !Date.gregorian_leap?( curYear ) )
stringDate = t.strftime("%m/28/%y")
else
stringDate = t.strftime("%m/31/%y")
end
return stringDate
end
def getFormattedDates( input= nil )
if( input == nil )
t = Time.local(Time.now.year, Time.now.mon, Time.now.day)
else
t = interpretDate( input )
end
return nil, nil if( t == nil )
intDate = t.strftime("%Y%m%d").to_i
stringDate = t.strftime("%m/%d/%y")
return intDate, stringDate
end
def getDaysBetween( startDate, endDate )
startDate = interpretDate( startDate )
endDate = interpretDate( endDate )
return nil if( startDate == nil || endDate == nil )
return (((endDate-startDate)/86400).to_i+1)
end
def dateReporter( input )
if( input == nil )
t = Time.local(Time.now.year, Time.now.mon, Time.now.day)
else
t = interpretDate( input )
end
return "That Date is no Recognized" if( t == nil )
currentDate = Time.local(Time.now.year, Time.now.mon, Time.now.day)
distance = ((t-currentDate)/86400).to_i
report = ( "You asked about "+t.strftime("%B %d, %Y")+"\n" )
report += ( "This date is a "+t.strftime("%A")+".\n" )
if( distance == 0 )
report += ( "This is today's date." )
elsif( distance == 1 )
report += ( "This is tomorrow's date." )
elsif( distance == -1 )
report += ( "This was yesterday's date." )
elsif( distance > 0 )
report += ( "This date will occur in "+distance.to_s+" days." )
elsif( distance < 0 )
report += ( "This date occured "+(distance*-1).to_s+" days ago." )
end
return report
end
end