## ------------------------------------------------------------------------
## iSiLK - A python-based graphical user interface for SiLK
## ------------------------------------------------------------------------
## Copyright (c) 2007-2008 Carnegie Mellon University. All Rights Reserved.
## ------------------------------------------------------------------------
## Authors: Andrew Kompanek (ajk@cert.org)
## ------------------------------------------------------------------------
## GNU General Public License (GPL) Rights pursuant to Version 2, June 1991
## Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013
## ------------------------------------------------------------------------
## See the LICENSE file included in this distribution
## ------------------------------------------------------------------------

import time
import datetime as dt

# Convert a time tuple (assumed to be GM time) to seconds since Epoch.
# Horrible horrible hack so I can do all my math in UTC
# regardless of the TZ setting.
def mkgmtime(t):
   t = time.mktime(t) # - time.timezone
   return t

def _calculate_date_ranges():
   tm_now        = dt.datetime.utcnow()
   tm_thishour   = dt.datetime(tm_now.year, tm_now.month, tm_now.day, tm_now.hour)
   tm_now = tm_thishour
   tm_thisday    = dt.datetime(tm_now.year, tm_now.month, tm_now.day, 0)
   tm_yesterday  = tm_thisday  - dt.timedelta(days=1) 
   tm_lasthour   = tm_thishour - dt.timedelta(hours=1)
   tm_lastday    = tm_thishour - dt.timedelta(hours=24)
   tm_lastweek   = tm_thisday  - dt.timedelta(days=7)
   tm_last2weeks = tm_thisday  - dt.timedelta(days=14)
   tm_lastmonth  = tm_thisday  - dt.timedelta(days=30)
   ranges = {}
   ranges[DateRange.CURRENT_HOUR]    = (tm_thishour,   tm_now)
   ranges[DateRange.LAST_HOUR]       = (tm_lasthour,   tm_lasthour)
   ranges[DateRange.LAST_24_HOURS]   = (tm_lasthour - dt.timedelta(hours=23), tm_lasthour)
   ranges[DateRange.TODAY]           = (tm_thisday,    tm_now)
   ranges[DateRange.YESTERDAY]       = (tm_yesterday,  tm_yesterday + dt.timedelta(hours=23))
   ranges[DateRange.LAST_7_DAYS]     = (tm_lastweek,   tm_lasthour)
   ranges[DateRange.LAST_14_DAYS]    = (tm_last2weeks, tm_lasthour)
   ranges[DateRange.LAST_30_DAYS]    = (tm_lastmonth,  tm_lasthour)
   # MUSTFIXME: get rid of these
   #ranges[DateRange.TEST_DATE_RANGE_1] = (dt.datetime(2004, 12, 28,0), dt.datetime(2004,12,28,2))
   #ranges[DateRange.TEST_DATE_RANGE_2] = (dt.datetime(2004, 12, 14,0), dt.datetime(2004,12,15,23))
   return ranges

class DateRange:
    (CURRENT_HOUR,
      TODAY,
      YESTERDAY,
      LAST_HOUR,
      LAST_24_HOURS,
      LAST_7_DAYS,
      LAST_14_DAYS,
      LAST_30_DAYS
      #TEST_DATE_RANGE_1,
      #TEST_DATE_RANGE_2
      ) = range(0,8)
      
    names = [
        "Current Hour",
        "Previous Hour",
        "Previous 24 Hours", 
        "Today", 
        "Yesterday",     
        "Previous 7 Days",   
        "Previous 14 Days",  
        "Previous 30 Days",
#        "Test range"   
    ]

    def __init__(self, opt):
         if opt < 0 or opt > 8:
             raise Exception("Illegal DateRange value")
         self.opt = opt

    def __int__(self):
         return self.opt

    def __str__(self):
        (start, end) = self.GetRangeTuple()
        start_str = time.asctime(start)
        end_str = time.asctime(end)
        name = DateRange.names[self.opt]
        return "%s: %s to %s" % (name, start_str, end_str)

    def GetRangeTuple(self):
        ranges = _calculate_date_ranges()
        (s, e) = ranges[self.opt]
        return (s.utctimetuple(), e.utctimetuple())
