• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# (c) 2005 Clark C. Evans and contributors
2# This module is part of the Python Paste Project and is released under
3# the MIT License: http://www.opensource.org/licenses/mit-license.php
4# Some of this code was funded by: http://prometheusresearch.com
5from time import localtime
6from datetime import date
7from paste.util.datetimeutil import *
8
9def test_timedelta():
10    assert('' == normalize_timedelta(""))
11    assert('0.10' == normalize_timedelta("6m"))
12    assert('0.50' == normalize_timedelta("30m"))
13    assert('0.75' == normalize_timedelta("45m"))
14    assert('1.00' == normalize_timedelta("60 min"))
15    assert('1.50' == normalize_timedelta("90min"))
16    assert('1.50' == normalize_timedelta("1.50"))
17    assert('4.50' == normalize_timedelta("4 : 30"))
18    assert('1.50' == normalize_timedelta("1h 30m"))
19    assert('1.00' == normalize_timedelta("1"))
20    assert('1.00' == normalize_timedelta("1 hour"))
21    assert('8.00' == normalize_timedelta("480 mins"))
22    assert('8.00' == normalize_timedelta("8h"))
23    assert('0.50' == normalize_timedelta("0.5"))
24    assert('0.10' == normalize_timedelta(".1"))
25    assert('0.50' == normalize_timedelta(".50"))
26    assert('0.75' == normalize_timedelta("0.75"))
27
28def test_time():
29    assert('03:00 PM' == normalize_time("3p", ampm=True))
30    assert('03:00 AM' == normalize_time("300", ampm=True))
31    assert('03:22 AM' == normalize_time("322", ampm=True))
32    assert('01:22 PM' == normalize_time("1322", ampm=True))
33    assert('01:00 PM' == normalize_time("13", ampm=True))
34    assert('12:00 PM' == normalize_time("noon", ampm=True))
35    assert("06:00 PM" == normalize_time("6", ampm=True))
36    assert("01:00 PM" == normalize_time("1", ampm=True))
37    assert("07:00 AM" == normalize_time("7", ampm=True))
38    assert("01:00 PM" == normalize_time("1 pm", ampm=True))
39    assert("03:30 PM" == normalize_time("3:30 pm", ampm=True))
40    assert("03:30 PM" == normalize_time("3 30 pm", ampm=True))
41    assert("03:30 PM" == normalize_time("3 30 P.M.", ampm=True))
42    assert("12:00 PM" == normalize_time("0", ampm=True))
43    assert("12:00 AM" == normalize_time("1200 AM", ampm=True))
44
45def test_date():
46    tm = localtime()
47    yr = tm[0]
48    mo = tm[1]
49    assert(date(yr,4,11)  == parse_date("411"))
50    assert(date(yr,4,11)  == parse_date("APR11"))
51    assert(date(yr,4,11)  == parse_date("11APR"))
52    assert(date(yr,4,11)  == parse_date("4 11"))
53    assert(date(yr,4,11)  == parse_date("11 APR"))
54    assert(date(yr,4,11)  == parse_date("APR 11"))
55    assert(date(yr,mo,11) == parse_date("11"))
56    assert(date(yr,4,1)   == parse_date("APR"))
57    assert(date(yr,4,11)  == parse_date("4/11"))
58    assert(date.today()   == parse_date("today"))
59    assert(date.today()   == parse_date("now"))
60    assert(None           == parse_date(""))
61    assert(''             == normalize_date(None))
62
63    assert('2001-02-03' == normalize_date("20010203"))
64    assert('1999-04-11' == normalize_date("1999 4 11"))
65    assert('1999-04-11' == normalize_date("1999 APR 11"))
66    assert('1999-04-11' == normalize_date("APR 11 1999"))
67    assert('1999-04-11' == normalize_date("11 APR 1999"))
68    assert('1999-04-11' == normalize_date("4 11 1999"))
69    assert('1999-04-01' == normalize_date("1999 APR"))
70    assert('1999-04-01' == normalize_date("1999 4"))
71    assert('1999-04-01' == normalize_date("4 1999"))
72    assert('1999-04-01' == normalize_date("APR 1999"))
73    assert('1999-01-01' == normalize_date("1999"))
74
75    assert('1999-04-01' == normalize_date("1APR1999"))
76    assert('2001-04-01' == normalize_date("1APR2001"))
77
78    assert('1999-04-18' == normalize_date("1999-04-11+7"))
79    assert('1999-04-18' == normalize_date("1999-04-11 7"))
80    assert('1999-04-01' == normalize_date("1 apr 1999"))
81    assert('1999-04-11' == normalize_date("11 apr 1999"))
82    assert('1999-04-11' == normalize_date("11 Apr 1999"))
83    assert('1999-04-11' == normalize_date("11-apr-1999"))
84    assert('1999-04-11' == normalize_date("11 April 1999"))
85    assert('1999-04-11' == normalize_date("11 APRIL 1999"))
86    assert('1999-04-11' == normalize_date("11 april 1999"))
87    assert('1999-04-11' == normalize_date("11 aprick 1999"))
88    assert('1999-04-11' == normalize_date("APR 11, 1999"))
89    assert('1999-04-11' == normalize_date("4/11/1999"))
90    assert('1999-04-11' == normalize_date("4-11-1999"))
91    assert('1999-04-11' == normalize_date("1999-4-11"))
92    assert('1999-04-11' == normalize_date("19990411"))
93
94    assert('1999-01-01' == normalize_date("1 Jan 1999"))
95    assert('1999-02-01' == normalize_date("1 Feb 1999"))
96    assert('1999-03-01' == normalize_date("1 Mar 1999"))
97    assert('1999-04-01' == normalize_date("1 Apr 1999"))
98    assert('1999-05-01' == normalize_date("1 May 1999"))
99    assert('1999-06-01' == normalize_date("1 Jun 1999"))
100    assert('1999-07-01' == normalize_date("1 Jul 1999"))
101    assert('1999-08-01' == normalize_date("1 Aug 1999"))
102    assert('1999-09-01' == normalize_date("1 Sep 1999"))
103    assert('1999-10-01' == normalize_date("1 Oct 1999"))
104    assert('1999-11-01' == normalize_date("1 Nov 1999"))
105    assert('1999-12-01' == normalize_date("1 Dec 1999"))
106
107    assert('1999-04-30' == normalize_date("1999-4-30"))
108    assert('2000-02-29' == normalize_date("29 FEB 2000"))
109    assert('2001-02-28' == normalize_date("28 FEB 2001"))
110    assert('2004-02-29' == normalize_date("29 FEB 2004"))
111    assert('2100-02-28' == normalize_date("28 FEB 2100"))
112    assert('1900-02-28' == normalize_date("28 FEB 1900"))
113
114    def assertError(val):
115        try:
116            normalize_date(val)
117        except (TypeError,ValueError):
118            return
119        raise ValueError("type error expected", val)
120
121    assertError("2000-13-11")
122    assertError("APR 99")
123    assertError("29 FEB 1900")
124    assertError("29 FEB 2100")
125    assertError("29 FEB 2001")
126    assertError("1999-4-31")
127    assertError("APR 99")
128    assertError("20301")
129    assertError("020301")
130    assertError("1APR99")
131    assertError("1APR01")
132    assertError("1 APR 99")
133    assertError("1 APR 01")
134    assertError("11/5/01")
135
136