Index: dfdatetime/interface.py |
diff --git a/dfdatetime/interface.py b/dfdatetime/interface.py |
index 67a3dedb0fb4baef98681ce15bae72ebcc4415f2..5817a29d906dcdbace03c65e8bf5b91d74293339 100644 |
--- a/dfdatetime/interface.py |
+++ b/dfdatetime/interface.py |
@@ -5,6 +5,7 @@ from __future__ import unicode_literals |
import abc |
import calendar |
+import decimal |
import math |
from dfdatetime import decorators |
@@ -58,6 +59,23 @@ class DateTimeValues(object): |
_UINT60_MAX = (1 << 60) - 1 |
_UINT64_MAX = (1 << 64) - 1 |
+ _DECIMAL_PLACES = { |
+ definitions.PRECISION_1_DAY: 0, |
+ definitions.PRECISION_1_HOUR: 0, |
+ definitions.PRECISION_1_NANOSECOND: 9, |
+ definitions.PRECISION_100_NANOSECONDS: 7, |
+ definitions.PRECISION_1_MICROSECOND: 6, |
+ definitions.PRECISION_1_MILLISECOND: 3, |
+ definitions.PRECISION_100_MILLISECONDS: 1, |
+ definitions.PRECISION_1_MINUTE: 0, |
+ definitions.PRECISION_1_SECOND: 0, |
+ definitions.PRECISION_2_SECONDS: 0, |
+ } |
+ |
+ _DECIMAL_GRANULARITY = dict( |
+ [(precision, decimal.Decimal(10) ** (-places)) |
+ for (precision, places) in _DECIMAL_PLACES.items()]) |
+ |
def __init__(self): |
"""Initializes date time values.""" |
super(DateTimeValues, self).__init__() |
@@ -65,6 +83,23 @@ class DateTimeValues(object): |
self.is_local_time = False |
self.precision = None |
+ def _SetNormalizedTimestamp(self, timestamp): |
+ """Sets the normalized timestamp with the correct precision. |
+ |
+ If the desired precision is listed in definitions.DECIMAL_EXPONENTS, the |
+ normalized timestamp is quantized to it. |
+ |
+ Args: |
+ timestamp (decimal.Decimal): the timestamp to be set. |
+ """ |
+ try: |
+ granularity = self._DECIMAL_GRANULARITY[self.precision] |
+ except KeyError: |
+ pass |
+ if granularity: |
+ timestamp = timestamp.quantize(granularity) |
+ self._normalized_timestamp = timestamp |
+ |
def __eq__(self, other): |
"""Determines if the date time values are equal to other. |
@@ -676,9 +711,10 @@ class DateTimeValues(object): |
"""Retrieves the normalized timestamp. |
Returns: |
- float: normalized timestamp, which contains the number of seconds since |
- January 1, 1970 00:00:00 and a fraction of second used for increased |
- precision, or None if the normalized timestamp cannot be determined. |
+ decimal.Decimal: normalized timestamp, which contains the number of |
+ seconds since January 1, 1970 00:00:00 and a fraction of second used |
+ for increased precision, or None if the normalized timestamp cannot be |
+ determined. |
""" |
def _GetNumberOfDaysInCentury(self, year): |