Esempio n. 1
0
    def test_ts_to_dt(self):
        # Verify that ts_to_dt works as expected.
        value_sec = 1426291200  # 2015-03-14 00:00:00 in UTC

        value_dt_utc = moment.ts_to_dt(value_sec, moment.get_zone('UTC'))
        value_dt_aware = moment.ts_to_dt(value_sec,
                                         moment.get_zone('America/New_York'))
        self.assertEqual(value_dt_utc.strftime("%Y-%m-%d %H:%M:%S %Z"),
                         '2015-03-14 00:00:00 UTC')
        self.assertEqual(value_dt_aware.strftime("%Y-%m-%d %H:%M:%S %Z"),
                         '2015-03-13 20:00:00 EDT')
Esempio n. 2
0
    def test_date_to_ts(self):
        d = date(2015, 3, 14)
        tzla = moment.get_zone('America/Los_Angeles')

        def format_utc(ts):
            return moment.ts_to_dt(ts, moment.get_zone('UTC')).strftime(fmt)

        self.assertEqual(format_utc(moment.date_to_ts(d)),
                         '2015-03-14 00:00:00 UTC')
        self.assertEqual(format_utc(moment.date_to_ts(d, tzla)),
                         '2015-03-14 07:00:00 UTC')
        self.assertEqual(
            moment.ts_to_dt(moment.date_to_ts(d, tzla), tzla).strftime(fmt),
            '2015-03-14 00:00:00 PDT')
Esempio n. 3
0
    def test_dt_to_ds(self):
        # Verify that dt_to_ts works for both naive and aware datetime objects.
        value_dt = datetime(2015, 3, 14, 0, 0)  # In UTC
        value_sec = 1426291200
        tzla = moment.get_zone('America/Los_Angeles')

        def format_utc(ts):
            return moment.ts_to_dt(ts, moment.get_zone('UTC')).strftime(fmt)

        # Check that a naive datetime is interpreted in UTC.
        self.assertEqual(value_dt.strftime("%Y-%m-%d %H:%M:%S %Z"),
                         '2015-03-14 00:00:00 ')
        self.assertEqual(moment.dt_to_ts(value_dt),
                         value_sec)  # Interpreted in UTC

        # Get an explicit UTC version and make sure that also works.
        value_dt_utc = value_dt.replace(tzinfo=moment.TZ_UTC)
        self.assertEqual(value_dt_utc.strftime(fmt), '2015-03-14 00:00:00 UTC')
        self.assertEqual(moment.dt_to_ts(value_dt_utc), value_sec)

        # Get an aware datetime, and make sure that works too.
        value_dt_aware = moment.ts_to_dt(value_sec,
                                         moment.get_zone('America/New_York'))
        self.assertEqual(value_dt_aware.strftime(fmt),
                         '2015-03-13 20:00:00 EDT')
        self.assertEqual(moment.dt_to_ts(value_dt_aware), value_sec)

        # Check that dt_to_ts pays attention to the timezone.
        # If we interpret midnight in LA time, it's a later timestamp.
        self.assertEqual(format_utc(moment.dt_to_ts(value_dt, tzla)),
                         '2015-03-14 07:00:00 UTC')
        # The second argument is ignored if the datetime is aware.
        self.assertEqual(format_utc(moment.dt_to_ts(value_dt_utc, tzla)),
                         '2015-03-14 00:00:00 UTC')
        self.assertEqual(format_utc(moment.dt_to_ts(value_dt_aware, tzla)),
                         '2015-03-14 00:00:00 UTC')

        # If we modify an aware datetime, we may get a new timezone abbreviation.
        value_dt_aware -= timedelta(days=28)
        self.assertEqual(value_dt_aware.strftime(fmt),
                         '2015-02-13 20:00:00 EST')
Esempio n. 4
0
def decode_object(value):
    """
  Given a Grist-encoded value, returns an object represented by it.
  If typename is unknown, or construction fails for any reason, returns (not raises!)
  RaisedException with the original exception in its .error property.
  """
    try:
        if not isinstance(value, (list, tuple)):
            if isinstance(value, unicode):
                # TODO For now, the sandbox uses binary strings throughout; see TODO in main.py for more
                # on this. Strings that come from JS become Python binary strings, and we will not see
                # unicode here. But we may see it if unmarshalling data that comes from DB, since
                # DocStorage encodes/decodes values by marshaling JS strings as unicode. For consistency,
                # convert those unicode strings to binary strings too.
                return value.encode('utf8')
            return value
        code = value[0]
        args = value[1:]
        if code == 'R':
            return RecordStub(args[0], args[1])
        elif code == 'D':
            return moment.ts_to_dt(args[0], moment.Zone(args[1]))
        elif code == 'd':
            return moment.ts_to_date(args[0])
        elif code == 'E':
            return RaisedException.decode_args(*args)
        elif code == 'L':
            return [decode_object(item) for item in args]
        elif code == 'O':
            return {
                decode_object(key): decode_object(val)
                for key, val in args[0].iteritems()
            }
        elif code == 'P':
            return _pending_sentinel
        elif code == 'U':
            return UnmarshallableValue(args[0])
        raise KeyError("Unknown object type code %r" % code)
    except Exception as e:
        return RaisedException(e)
Esempio n. 5
0
def decode_object(value):
    """
  Given a Grist-encoded value, returns an object represented by it.
  If typename is unknown, or construction fails for any reason, returns (not raises!)
  RaisedException with the original exception in its .error property.
  """
    try:
        if not isinstance(value, (list, tuple)):
            return value
        code = value[0]
        args = value[1:]
        if code == 'R':
            return RecordStub(args[0], args[1])
        elif code == 'r':
            return RecordSetStub(args[0], args[1])
        elif code == 'D':
            return moment.ts_to_dt(args[0], moment.Zone(args[1]))
        elif code == 'd':
            return moment.ts_to_date(args[0])
        elif code == 'E':
            return RaisedException.decode_args(*args)
        elif code == 'L':
            return [decode_object(item) for item in args]
        elif code == 'l':
            return ReferenceLookup(*args)
        elif code == 'O':
            return {
                decode_object(key): decode_object(val)
                for key, val in six.iteritems(args[0])
            }
        elif code == 'P':
            return _pending_sentinel
        elif code == 'C':
            return _censored_sentinel
        elif code == 'U':
            return UnmarshallableValue(args[0])
        raise KeyError("Unknown object type code %r" % code)
    except Exception as e:
        return RaisedException(e)
Esempio n. 6
0
 def format_utc(ts):
     return moment.ts_to_dt(ts, moment.get_zone('UTC')).strftime(fmt)
Esempio n. 7
0
 def ts_to_dt_nyc(dt):
     return moment.ts_to_dt(dt, moment.get_zone('America/New_York'))
Esempio n. 8
0
 def ts_to_dt_utc(dt):
     return moment.ts_to_dt(dt, moment.get_zone('UTC'))
Esempio n. 9
0
 def _make_rich_value(self, typed_value):
     return typed_value and moment.ts_to_dt(typed_value, self._timezone)
Esempio n. 10
0

class NumericColumn(BaseColumn):
    def set(self, row_id, value):
        # Make sure any integers are treated as floats to avoid truncation.
        # Uses `type(value) == int` rather than `isintance(value, int)` to specifically target
        # ints and not bools (which are singleton instances the class int in python).  But
        # perhaps something should be done about bools also?
        # pylint: disable=unidiomatic-typecheck
        super(NumericColumn,
              self).set(row_id,
                        float(value) if type(value) == int else value)


_sample_date = moment.ts_to_date(0)
_sample_datetime = moment.ts_to_dt(0, None, moment.TZ_UTC)


class DateColumn(NumericColumn):
    """
  DateColumn contains numerical timestamps represented as seconds since epoch, in type float,
  to midnight of specific UTC dates. Accessing them yields date objects.
  """
    def _make_rich_value(self, typed_value):
        return typed_value and moment.ts_to_date(typed_value)

    def sample_value(self):
        return _sample_date


class DateTimeColumn(NumericColumn):