Source

trac-gviz / trac-dev / gviz / tracgviz / scalar.py

Diff from to

trac-dev/gviz/tracgviz/scalar.py

 __all__ = 'year month day hour minute second millisecond quarter dayOfWeek ' \
     'now dateDiff toDate upper lower'.split()
 
-from datetime import datetime, date
+from datetime import datetime, date, time
 
 def check_datatype(value, types, expected_msg=None):
-  if not isinstance(value, types):
+  if value is None:
+    pass
+  elif not isinstance(value, types):
     msg = '%s expected but received %s' % (expected_msg, value) \
         if expected_msg else 'Unexpected value %s' % (value,)
     raise ValueError(msg)
 
 def year(value):
   check_datatype(value, (datetime, date), 'Date value')
-  return value.date
+  return value.year if value is not None else None
 
 year.return_type = 'number'
 
 def month(value):
   check_datatype(value, (datetime, date), 'Date value')
-  return value.month - 1
+  return value.month - 1 if value is not None else None
 
 month.return_type = 'number'
 
 def day(value):
   check_datatype(value, (datetime, date), 'Date value')
-  return value.day
+  return value.day if value is not None else None
 
 day.return_type = 'number'
 
 def hour(value):
   check_datatype(value, (time, date), 'Time value')
-  return value.hour
+  return value.hour if value is not None else None
 
 hour.return_type = 'number'
 
 def minute(value):
   check_datatype(value, (time, date), 'Time value')
-  return value.minute
+  return value.minute if value is not None else None
 
 minute.return_type = 'number'
 
 def second(value):
   check_datatype(value, (time, date), 'Time value')
-  return value.second
+  return value.second if value is not None else None
 
 second.return_type = 'number'
 
 def millisecond(value):
   check_datatype(value, (time, date), 'Time value')
-  return value.microsecond / 1000
+  return value.microsecond / 1000 if value is not None else None
 
 millisecond.return_type = 'number'
 
 def quarter(value):
-  return 1 + month(value) / 3
+  return 1 + month(value) / 3 if value is not None else None
 
 quarter.return_type = 'number'
 
 def dayOfWeek(value):
   check_datatype(value, (datetime, date), 'Date value')
-  return value.isoweekday() % 7 + 1
+  return value.isoweekday() % 7 + 1 if value is not None else None
 
 dayOfWeek.return_type = 'number'
 
 def dateDiff(until, since):
   check_datatype(since, (datetime, date), 'Date value')
   check_datatype(until, (datetime, date), 'Date value')
-  return (until - since).days
+  return (until - since).days if until is not None and since is not None \
+      else None
 
 dateDiff.return_type = 'number'
 
-def now(value):
+def now():
   return datetime.now()
 
 now.return_type = 'datetime'
 
 def toDate(value):
   check_datatype(value, (date, datetime, int, long), 'Date value')
-  if isinstance(value, date):
+  if value is None:
+    return None
+  if isinstance(value, datetime):
+    return value.date()
+  elif isinstance(value, date):
     return value
-  elif isinstance(value, datetime):
-    return value.date()
   elif isinstance(value, (int, long)):
     return date.fromtimestamp(value / 1000)
   raise ValueError('Date value expected but received %s' % (value,))
 
 def upper(value):
   check_datatype(value, basestring, 'String value')
-  return value.upper()
+  return value.upper() if value is not None else None
 
 upper.return_type = 'string'
 
 def lower(value):
   check_datatype(value, basestring, 'String value')
-  return value.lower()
+  return value.lower() if value is not None else None
 
 lower.return_type = 'string'