| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- encoding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | ## |
|---|
| 5 | ## datetimeutils.py |
|---|
| 6 | ## Created on Wed Feb 8 22:21:13 2006 |
|---|
| 7 | ## by Antti Kaihola <akaihola@ambitone.com> |
|---|
| 8 | ## |
|---|
| 9 | ## Copyright (C) 2006 Antti Kaihola |
|---|
| 10 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 11 | ## it under the terms of the GNU General Public License as published by |
|---|
| 12 | ## the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | ## (at your option) any later version. |
|---|
| 14 | ## |
|---|
| 15 | ## This program is distributed in the hope that it will be useful, |
|---|
| 16 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | ## GNU General Public License for more details. |
|---|
| 19 | ## |
|---|
| 20 | ## You should have received a copy of the GNU General Public License |
|---|
| 21 | ## along with this program; if not, write to the Free Software |
|---|
| 22 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | ## |
|---|
| 24 | |
|---|
| 25 | from datetime import datetime, timedelta |
|---|
| 26 | from django.http import HttpResponseNotFound |
|---|
| 27 | import re |
|---|
| 28 | |
|---|
| 29 | re_datespan = ''.join(x for x in |
|---|
| 30 | r'''(?P<startyear>\d{4}) |
|---|
| 31 | ((?P<startmonth>\d\d) |
|---|
| 32 | ((?P<startday>\d\d) |
|---|
| 33 | (- |
|---|
| 34 | ((?P<endyear>\d{4})? |
|---|
| 35 | (?P<endmonth>\d\d) |
|---|
| 36 | )? |
|---|
| 37 | (?P<endday>\d\d) |
|---|
| 38 | )? |
|---|
| 39 | )? |
|---|
| 40 | )?''' |
|---|
| 41 | if x not in ' \n') |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | def normalize_datespan(startyear=None, startmonth=None, startday=None, |
|---|
| 45 | endyear =None, endmonth=None, endday=None): |
|---|
| 46 | startdate = datetime(int(startyear or datetime.now().year), |
|---|
| 47 | int(startmonth or 1 ), |
|---|
| 48 | int(startday or 1 ), |
|---|
| 49 | 4) |
|---|
| 50 | enddate = datetime(int(endyear or (startmonth and startdate.year |
|---|
| 51 | or startdate.year+1+(startdate.month==12))), |
|---|
| 52 | int(endmonth or ((startmonth is not None and startday is None) |
|---|
| 53 | and startdate.month%12+1 |
|---|
| 54 | or startdate.month)), |
|---|
| 55 | int(endday or startdate.day ), |
|---|
| 56 | 4) |
|---|
| 57 | if startday is not None: |
|---|
| 58 | enddate += timedelta(days=1) |
|---|
| 59 | return startdate, enddate |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | def datespan(view): |
|---|
| 63 | def datespan_wrapper(*args, **kw): |
|---|
| 64 | kp = {} |
|---|
| 65 | for key in ('startyear', 'startmonth', 'startday', |
|---|
| 66 | 'endyear' , 'endmonth' , 'endday'): |
|---|
| 67 | kp[key] = kw.pop(key, None) |
|---|
| 68 | |
|---|
| 69 | try: |
|---|
| 70 | kw['startdate'], kw['enddate'] = normalize_datespan(**kp) |
|---|
| 71 | except ValueError: |
|---|
| 72 | start = (kp['startyear' ] or '') + \ |
|---|
| 73 | (kp['startmonth'] or '') + \ |
|---|
| 74 | (kp['startday' ] or '') |
|---|
| 75 | end = (kp['endyear' ] or '') + \ |
|---|
| 76 | (kp['endmonth' ] or '') + \ |
|---|
| 77 | (kp['endday' ] or '') |
|---|
| 78 | daterange = '-'.join(filter(None, (start, end))) |
|---|
| 79 | |
|---|
| 80 | return HttpResponseNotFound('''\ |
|---|
| 81 | <h1>Unrecognized date "%s"</h1> |
|---|
| 82 | <p> |
|---|
| 83 | Please make sure that you specify the date range in the YYYYMMDD format, |
|---|
| 84 | e.g. June 20, 2006 should be expressed as 20060620. |
|---|
| 85 | </p> |
|---|
| 86 | <p> |
|---|
| 87 | Here are some examples of accepted dates and date ranges: |
|---|
| 88 | </p> |
|---|
| 89 | <ul> |
|---|
| 90 | <li>20060714 (July 14, 2006)</li> |
|---|
| 91 | <li>20060714-15 (July 14-15, 2006)</li> |
|---|
| 92 | <li>20060714-0801 (July 14 - Aug 1, 2006)</li> |
|---|
| 93 | <li>20060714-20070713 (July 14, 2006 - July 13, 2007) |
|---|
| 94 | </ul>''' % daterange) |
|---|
| 95 | return view(*args, **kw) |
|---|
| 96 | return datespan_wrapper |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | def datespanstr_from_GET(getdict): |
|---|
| 100 | return getdict.get('dates', '') |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | def datespan_from_GET(getdict): |
|---|
| 104 | m = re.match(re_datespan, datespanstr_from_GET(getdict)) |
|---|
| 105 | return normalize_datespan(**m.groupdict()) |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | def datespan_from_GET_decorator(view): |
|---|
| 109 | def wrapper(*args, **kw): |
|---|
| 110 | request = kw.get('request', None) |
|---|
| 111 | if request: |
|---|
| 112 | kw['startdate'], kw['enddate'] = datespan_from_GET(request.GET) |
|---|
| 113 | view(*args, **kw) |
|---|
| 114 | return wrapper |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | if __name__ == "__main__": |
|---|
| 118 | import doctest |
|---|
| 119 | doctest.testmod() |
|---|