Changeset 2516

Show
Ignore:
Timestamp:
07/23/07 14:29:41 (1 year ago)
Author:
ant_39
Message:

CalendarPlugin:

  • A bit of a cleanup, moved a lot of generic stuff into caltools.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • calendarplugin/0.10/azcalendar/azcalendar.py

    r2515 r2516  
    6767        req.perm.assert_permission('CAL_VIEW') 
    6868 
    69         def get_week(date): 
    70             # If there is a simpler way to do this, let me know.  For now... 
    71             d_year, d_doy, d_dow = date[0], date[-2], date[-3] 
    72  
    73             doy_start = d_doy - d_dow 
    74             y_start = d_year 
    75             if doy_start < 1: 
    76                 y_start -= 1 
    77                 doy_start += 365 + int (calendar.isleap (y_start)) 
    78  
    79             doy_end = d_doy - d_dow + 7 
    80             y_end = d_year 
    81             if doy_end > 365 + int (calendar.isleap (y_end)): 
    82                 doy_end -= 365 + int (calendar.isleap (y_end)) 
    83                 y_end += 1 
    84  
    85             week_start = time.strptime (str(y_start) + str(doy_start), "%Y%j") 
    86             week_end = time.strptime (str(y_end) + str(doy_end), "%Y%j") 
    87             return week_start, week_end 
    88  
    89         def get_month_range(date): 
    90             d_year, d_month = date[0], date[1] 
    91             month_start = tuple([d_year, d_month, 1] + [0 for _ in date[3:]]) 
    92             d_month = d_month + 1 
    93             if d_month > 12: 
    94                 d_year += 1 
    95                 d_month = 1 
    96             month_end = tuple([d_year, d_month, 1] + [0 for _ in date[3:]]) 
    97             return month_start, month_end 
    98  
    99         def relative_day(week_start, which_day): 
    100             d_year, d_doy = week_start[0], week_start[-2] 
    101             doy = d_doy + which_day 
    102             if doy < 1: 
    103                 d_year -= 1 
    104                 doy += 365 + int (calendar.isleap (d_year)) 
    105             elif doy > 365 + int (calendar.isleap (d_year)): 
    106                 doy -= 365 + int (calendar.isleap (d_year)) 
    107                 d_year += 1 
    108             day = time.strptime (str(d_year) + str(doy), "%Y%j") 
    109             return day 
    110  
    11169        def stamp_dow (stamp): 
    11270            return time.localtime(stamp)[6] 
     
    13290 
    13391        cweek = [[] for _ in xrange(7)] 
    134         week_range = get_week(date) 
     92        week_range = caltools.get_week_range(date) 
    13593        bg, en = time.mktime(week_range[0]), time.mktime(week_range[1]) 
    13694        for ev in Event.events_between (self.env, bg, en,req.authname): 
     
    146104 
    147105        for d in xrange (len (day_layouts)): 
    148             today = relative_day(week_start, d) 
     106            today = caltools.relative_day(week_start, d) 
    149107            today_stamp = time.mktime (today) 
    150108            tomorrow_stamp = today_stamp + 24 * 60 * 60 
     
    183141 
    184142        display_months = [] 
    185         dm_year, dm_month = get_month_range (week_start)[0][:2] 
     143        dm_year, dm_month = caltools.get_month_range (week_start)[0][:2] 
    186144 
    187145        prev_year, prev_month, prev_day = date[:3] 
     
    200158 
    201159        for i in range(3): 
    202             month_range = get_month_range (tuple([dm_year, dm_month, 1] + [0 for i in range(6)])) 
     160            month_range = caltools.get_month_range (tuple([dm_year, dm_month, 1] + [0 for i in range(6)])) 
    203161            interesting_days = {} 
    204162 
  • calendarplugin/0.10/azcalendar/caltools.py

    r2508 r2516  
    2020    return (tm_year, tm_mon, tm_day) + struct_time[3:] 
    2121 
    22 # Each format may be either a string, or a tuple (string, callback). 
    23 # strptime result of the first matching format is returned.  If the 
    24 # format has assigned a callback, it is first called with the result 
    25 # of strptime, and result of the callback is then returned. 
     22def relative_day(week_start, which_day): 
     23    d_year, d_doy = week_start[0], week_start[-2] 
     24    doy = d_doy + which_day 
     25    if doy < 1: 
     26        d_year -= 1 
     27        doy += 365 + int (calendar.isleap (d_year)) 
     28    elif doy > 365 + int (calendar.isleap (d_year)): 
     29        doy -= 365 + int (calendar.isleap (d_year)) 
     30        d_year += 1 
     31    day = time.strptime (str(d_year) + str(doy), "%Y%j") 
     32    return day 
     33 
     34 
    2635def strptimeopt (string, *formats): 
     36    """ 
     37    Each format may be either a string, or a tuple (string, callback). 
     38    strptime result of the first matching format is returned.  If the 
     39    format has assigned a callback, it is first called with the result 
     40    of strptime, and result of the callback is then returned. 
     41    """ 
     42 
    2743    import types 
    2844    assert formats, "Non-empty list of formats required." 
     
    5975 
    6076    return begin_time, end_time, begin_stamp, end_stamp 
     77 
     78def get_week_range(date): 
     79    """ 
     80    Answer the tuple (start, end) with timestamps of start and end of 
     81    the week that contains given date. 
     82    """ 
     83 
     84    # If there is a simpler way to do this, let me know.  For now... 
     85    d_year, d_doy, d_dow = date[0], date[-2], date[-3] 
     86 
     87    doy_start = d_doy - d_dow 
     88    y_start = d_year 
     89    if doy_start < 1: 
     90        y_start -= 1 
     91        doy_start += 365 + int (calendar.isleap (y_start)) 
     92 
     93    doy_end = d_doy - d_dow + 7 
     94    y_end = d_year 
     95    if doy_end > 365 + int (calendar.isleap (y_end)): 
     96        doy_end -= 365 + int (calendar.isleap (y_end)) 
     97        y_end += 1 
     98 
     99    week_start = time.strptime (str(y_start) + str(doy_start), "%Y%j") 
     100    week_end = time.strptime (str(y_end) + str(doy_end), "%Y%j") 
     101    return week_start, week_end 
     102 
     103def get_month_range(date): 
     104    """ 
     105    Answer the tuple (start, end) with timestamps of start and end of 
     106    the month that contains given date. 
     107    """ 
     108 
     109    d_year, d_month = date[0], date[1] 
     110    month_start = tuple([d_year, d_month, 1] + [0 for _ in date[3:]]) 
     111    d_month = d_month + 1 
     112    if d_month > 12: 
     113        d_year += 1 
     114        d_month = 1 
     115    month_end = tuple([d_year, d_month, 1] + [0 for _ in date[3:]]) 
     116    return month_start, month_end