4
- allow strings in getDateTime routine;
6
"""History (most recent first):
7
11-feb-2007 [als] added INVALID_VALUE
8
10-feb-2007 [als] allow date strings padded with spaces instead of zeroes
9
20-dec-2005 [yc] handle long objects in getDate/getDateTime
10
16-dec-2005 [yc] created from ``strutil`` module.
13
__version__ = "$Revision: 1.4 $"[11:-2]
14
__date__ = "$Date: 2007/02/11 08:57:17 $"[7:-2]
21
"""Return a string without ASCII NULs.
23
This function searchers for the first NUL (ASCII 0) occurance
24
and truncates string till that position.
28
return str[:str.index('\0')]
33
def getDate(date=None):
34
"""Return `datetime.date` instance.
36
Type of the ``date`` argument could be one of the following:
38
use current date value;
40
this value will be returned;
42
the result of the date.date() will be returned;
44
assuming "%Y%m%d" or "%y%m%dd" format;
46
assuming it's a timestamp (returned for example
47
by the time.time() call;
49
assuming (year, month, day, ...) sequence;
51
Additionaly, if ``date`` has callable ``ticks`` attribute,
52
it will be used and result of the called would be treated
58
return datetime.date.today()
59
if isinstance(date, datetime.date):
61
if isinstance(date, datetime.datetime):
63
if isinstance(date, (int, long, float)):
65
return datetime.date.fromtimestamp(date)
66
if isinstance(date, basestring):
67
date = date.replace(" ", "0")
70
return datetime.date(*time.strptime(date, "%y%m%d")[:3])
72
return datetime.date(*time.strptime(date, "%Y%m%d")[:3])
73
if hasattr(date, "__getitem__"):
74
# a sequence (assuming date/time tuple)
75
return datetime.date(*date[:3])
76
return datetime.date.fromtimestamp(date.ticks())
79
def getDateTime(value=None):
80
"""Return `datetime.datetime` instance.
82
Type of the ``value`` argument could be one of the following:
84
use current date value;
86
result will be converted to the `datetime.datetime` instance
89
``value`` will be returned as is;
91
*** CURRENTLY NOT SUPPORTED ***;
93
assuming it's a timestamp (returned for example
94
by the time.time() call;
96
assuming (year, month, day, ...) sequence;
98
Additionaly, if ``value`` has callable ``ticks`` attribute,
99
it will be used and result of the called would be treated
100
as a timestamp value.
105
return datetime.datetime.today()
106
if isinstance(value, datetime.datetime):
108
if isinstance(value, datetime.date):
109
return datetime.datetime.fromordinal(value.toordinal())
110
if isinstance(value, (int, long, float)):
111
# value is a timestamp
112
return datetime.datetime.fromtimestamp(value)
113
if isinstance(value, basestring):
114
raise NotImplementedError("Strings aren't currently implemented")
115
if hasattr(value, "__getitem__"):
116
# a sequence (assuming date/time tuple)
117
return datetime.datetime(*tuple(value)[:6])
118
return datetime.datetime.fromtimestamp(value.ticks())
121
class classproperty(property):
122
"""Works in the same way as a ``property``, but for the classes."""
124
def __get__(self, obj, cls):
125
return self.fget(cls)
128
class _InvalidValue(object):
130
"""Value returned from DBF records when field validation fails
132
The value is not equal to anything except for itself
133
and equal to all empty values: None, 0, empty string etc.
134
In other words, invalid value is equal to None and not equal
135
to None at the same time.
137
This value yields zero upon explicit conversion to a number type,
138
empty string for string types, and False for boolean.
142
def __eq__(self, other):
145
def __ne__(self, other):
146
return not (other is self)
148
def __nonzero__(self):
161
def __unicode__(self):
167
# invalid value is a constant singleton
168
INVALID_VALUE = _InvalidValue()
170
# vim: set et sts=4 sw=4 :