Esempio n. 1
0
    def value_to_message(self, value):
        value = super(DateTimeField, self).value_to_message(value)
        # First, determine the delta from the epoch, so we can fill in
        # DateTimeMessage's milliseconds field.
        if value.tzinfo is None:
            time_zone_offset = 0
            local_epoch = datetime.datetime.utcfromtimestamp(0)
        else:
            time_zone_offset = util.total_seconds(
                value.tzinfo.utcoffset(value))
            # Determine Jan 1, 1970 local time.
            local_epoch = datetime.datetime.fromtimestamp(-time_zone_offset,
                                                          tz=value.tzinfo)
        delta = value - local_epoch

        # Create and fill in the DateTimeMessage, including time zone if
        # one was specified.
        message = DateTimeMessage()
        message.milliseconds = int(util.total_seconds(delta) * 1000)
        if value.tzinfo is not None:
            utc_offset = value.tzinfo.utcoffset(value)
            if utc_offset is not None:
                message.time_zone_offset = int(
                    util.total_seconds(value.tzinfo.utcoffset(value)) / 60)

        return message
Esempio n. 2
0
    def value_to_message(self, value):
        value = super(DateTimeField, self).value_to_message(value)
        # First, determine the delta from the epoch, so we can fill in
        # DateTimeMessage's milliseconds field.
        if value.tzinfo is None:
            time_zone_offset = 0
            local_epoch = datetime.datetime.utcfromtimestamp(0)
        else:
            time_zone_offset = util.total_seconds(
                value.tzinfo.utcoffset(value))
            # Determine Jan 1, 1970 local time.
            local_epoch = datetime.datetime.fromtimestamp(-time_zone_offset,
                                                          tz=value.tzinfo)
        delta = value - local_epoch

        # Create and fill in the DateTimeMessage, including time zone if
        # one was specified.
        message = DateTimeMessage()
        message.milliseconds = int(util.total_seconds(delta) * 1000)
        if value.tzinfo is not None:
            utc_offset = value.tzinfo.utcoffset(value)
            if utc_offset is not None:
                message.time_zone_offset = int(
                    util.total_seconds(value.tzinfo.utcoffset(value)) / 60)

        return message
Esempio n. 3
0
 def testTimeZoneOffsetDelta(self):
     """Test that delta works with TimeZoneOffset."""
     time_zone = util.TimeZoneOffset(datetime.timedelta(minutes=3))
     epoch = time_zone.utcoffset(datetime.datetime.utcfromtimestamp(0))
     self.assertEqual(180, util.total_seconds(epoch))
Esempio n. 4
0
 def testTimeZoneOffsetDelta(self):
     """Test that delta works with TimeZoneOffset."""
     time_zone = util.TimeZoneOffset(datetime.timedelta(minutes=3))
     epoch = time_zone.utcoffset(datetime.datetime.utcfromtimestamp(0))
     self.assertEqual(180, util.total_seconds(epoch))