Пример #1
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')
Пример #2
0
 def do_convert(cls, value):
   if value in ("", None):
     return None
   elif isinstance(value, datetime.datetime):
     return moment.date_to_ts(value.date())
   elif isinstance(value, datetime.date):
     return moment.date_to_ts(value)
   elif isinstance(value, (float, six.integer_types)):
     return float(value)
   elif isinstance(value, six.string_types):
     # We also accept a date in ISO format (YYYY-MM-DD), the time portion is optional and ignored
     return moment.parse_iso_date(value)
   else:
     raise objtypes.ConversionError('Date')
Пример #3
0
def encode_object(value):
    """
  Produces a Grist-encoded version of the value, e.g. turning a Date into ['d', timestamp].
  Returns ['U', repr(value)] if it fails to encode otherwise.
  """
    try:
        if isinstance(value, (six.text_type, float, bool)) or value is None:
            return value
        elif isinstance(value, six.binary_type):
            return value.decode('utf8')
        elif isinstance(value, six.integer_types):
            if not is_int_short(value):
                raise UnmarshallableError("Integer too large")
            return value
        elif isinstance(value, AltText):
            return six.text_type(value)
        elif isinstance(value, records.Record):
            return ['R', value._table.table_id, value._row_id]
        elif isinstance(value, RecordStub):
            return ['R', value.table_id, value.row_id]
        elif isinstance(value, datetime):
            return [
                'D',
                moment.dt_to_ts(value),
                value.tzinfo.zone.name if value.tzinfo else 'UTC'
            ]
        elif isinstance(value, date):
            return ['d', moment.date_to_ts(value)]
        elif isinstance(value, RaisedException):
            return ['E'] + value.encode_args()
        elif isinstance(value, (list, tuple)):
            return ['L'] + [encode_object(item) for item in value]
        elif isinstance(value, records.RecordSet):
            return ['r', value._table.table_id, value._get_encodable_row_ids()]
        elif isinstance(value, RecordSetStub):
            return ['r', value.table_id, value.row_ids]
        elif isinstance(value, dict):
            if not all(isinstance(key, six.string_types) for key in value):
                raise UnmarshallableError("Dict with non-string keys")
            return [
                'O',
                {key: encode_object(val)
                 for key, val in six.iteritems(value)}
            ]
        elif value == _pending_sentinel:
            return ['P']
        elif value == _censored_sentinel:
            return ['C']
        elif isinstance(value, UnmarshallableValue):
            return ['U', value.value_repr]
    except Exception as e:
        pass
    # We either don't know how to convert the value, or failed during the conversion. Instead we
    # return an "UnmarshallableValue" object, with repr() of the value to show to the user.
    return ['U', safe_repr(value)]
Пример #4
0
 def do_convert(self, value):
     if value in ("", None):
         return None
     elif isinstance(value, datetime.datetime):
         return moment.dt_to_ts(value, self.timezone)
     elif isinstance(value, datetime.date):
         return moment.date_to_ts(value, self.timezone)
     elif isinstance(value, (float, int, long)):
         return float(value)
     elif isinstance(value, basestring):
         # We also accept a datetime in ISO format (YYYY-MM-DD[T]HH:mm:ss)
         return moment.parse_iso(value, self.timezone)
     else:
         raise objtypes.ConversionError('DateTime')
Пример #5
0
def encode_object(value):
    """
  Produces a Grist-encoded version of the value, e.g. turning a Date into ['d', timestamp].
  Returns ['U', repr(value)] if it fails to encode otherwise.
  """
    try:
        if isinstance(value, (str, unicode, float, bool)) or value is None:
            return value
        elif isinstance(value, (long, int)):
            if not is_int_short(value):
                raise UnmarshallableError("Integer too large")
            return value
        elif isinstance(value, AltText):
            return str(value)
        elif isinstance(value, records.Record):
            return ['R', value._table.table_id, value._row_id]
        elif isinstance(value, datetime):
            return [
                'D',
                moment.dt_to_ts(value),
                value.tzinfo.zone.name if value.tzinfo else 'UTC'
            ]
        elif isinstance(value, date):
            return ['d', moment.date_to_ts(value)]
        elif isinstance(value, RaisedException):
            return ['E'] + value.encode_args()
        elif isinstance(value, (list, tuple, RecordList, records.ColumnView)):
            return ['L'] + [encode_object(item) for item in value]
        elif isinstance(value, records.RecordSet):
            # Represent RecordSet (e.g. result of lookupRecords) in the same way as a RecordList.
            return ['L'] + [encode_object(int(item)) for item in value]
        elif isinstance(value, dict):
            if not all(isinstance(key, basestring) for key in value):
                raise UnmarshallableError("Dict with non-string keys")
            return [
                'O',
                {key: encode_object(val)
                 for key, val in value.iteritems()}
            ]
        elif value == _pending_sentinel:
            return ['P']
        elif isinstance(value, UnmarshallableValue):
            return ['U', value.value_repr]
    except Exception as e:
        pass
    # We either don't know how to convert the value, or failed during the conversion. Instead we
    # return an "UnmarshallableValue" object, with repr() of the value to show to the user.
    return ['U', safe_repr(value)]