Convert datetime to protobuf.timestamp.

    :param time: time
    :type time: ~datetime.datetime
    :return: protobuf.timestamp
    :rtype: ~google.protobuf.timestamp_pb2.Timestamp
482245
def datetime_to_timestamp(time: datetime) -> Timestamp:
    
    protime = Timestamp()
    protime.FromDatetime(time)
    return protime
493297
def datetime_to_time(date, time):
    """"""
    if (255 in date) or (255 in time):
        raise RuntimeError("specific date and time required")

    time_tuple = (
        date[0]+1900, date[1], date[2],
        time[0], time[1], time[2],
        0, 0, -1,
        )
    return _mktime(time_tuple)
1123340
def timestamp_with_timezone(dt=None):
    
    dt = dt or datetime.now()
    if timezone is None:
        return dt.strftime('%Y-%m-%d %H:%M%z')
    if not dt.tzinfo:
        tz = timezone.get_current_timezone()
        if not tz:
            tz = timezone.utc
        dt = dt.replace(tzinfo=timezone.get_current_timezone())
    return dt.strftime("%Y-%m-%d %H:%M%z")
644112
def timestamp_to_datetime(timestamp):
    """"""
    from datetime import datetime, timedelta
    obj = datetime.fromtimestamp(timestamp[0])
    return obj + timedelta(microseconds=int(timestamp[1]))
15606
def parse_time(time_input):
    
    if isinstance(time_input, datetime.date):
        return time_input.isoformat()  # datetime.date only returns date, datetime.datetime also returns time

    if len(time_input) < 8:
        raise ValueError('Invalid time string {}.\n'
                         'Please specify time in formats YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS'.format(time_input))
    time = dateutil.parser.parse(time_input)
    if len(time_input) <= 10:
        return time.date().isoformat()
    return time.isoformat()
481667
def get_timestamp(self, **kwargs):
        """"""
        timestamp = kwargs.get('timestamp')
        if not timestamp:
            now = datetime.datetime.utcnow()
            timestamp = now.strftime("%Y-%m-%dT%H:%M:%S") + ".%03d" % (now.microsecond / 1000) + "Z"

        return timestamp
573927
def timestamp_datetime(self, timestamp):
        
        format = '%Y-%m-%d %H:%M:%S'
        # timestamp为传入的值为时间戳(10位整数)，如：1332888820
        timestamp = time.localtime(timestamp)
        ## 经过localtime转换后变成
        ## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)
        # 最后再经过strftime函数转换为正常日期格式。
        return time.strftime(format, timestamp)
266050
def timestamp(self, timestamp):
        """"""
        if timestamp is None:
            self._timestamp = datetime.utcnow()
        elif isinstance(timestamp, datetime):
            self._timestamp = timestamp
        else:
            self._timestamp = datetime.strptime(timestamp, fmt)
714226
def format_datetime(cls, timestamp):
        
        if not timestamp:
            raise DateTimeFormatterException('timestamp must a valid string {}'.format(timestamp))

        return timestamp.strftime(cls.DATETIME_FORMAT)
138046
def to_timestamp(dt):
    
    if isinstance(dt, int):
        return dt
    return int(total_seconds(dt.replace(tzinfo=None) -
               datetime.datetime(1970, 1, 1)))
1161540
def timestamp_to_datetime(cls, dt, dt_format=DATETIME_FORMAT):
        """"""
        return cls.convert_datetime(cls.get_datetime(dt), dt_format=dt_format)
472989
public static String getTimestamp(Date time) {
        return getTimestampFormatter().format(time == null ? new Date() : time);
    }
538532
def set_timestamp(self,timestamp=None):
        
        if timestamp is None:
            import time
            timestamp = time.strftime('%Y-%m-%dT%H:%M:%S%Z')
        self.node.set('timestamp',timestamp)
880366
def Timestamp(year, month, day, hour, minute, second):
    """"""
    return datetime.datetime(year, month, day, hour, minute, second)
1249311
def pp_timestamp(t):
    
    if t is None:
        return ''
    h, m, s = int(t / 3600), int(t / 60 % 60), t % 60
    return "%02d:%02d:%05.2f" % (h, m, s)
661701
def _timestamp():
    """"""
    moment = time.time()
    moment_us = repr(moment).split('.')[1]
    return time.strftime("%Y-%m-%d-%H-%M-%S-{}".format(moment_us), time.gmtime(moment))
270646
def convert_datetimes_to_timestamps(data, datetime_attrs):
    
    if not data:
        return data

    new_data = {}
    for key, value in data.items():
        if key in datetime_attrs and isinstance(value, datetime):
            new_key = datetime_attrs[key]
            new_data[new_key] = timestamp_from_dt(value)
        else:
            new_data[key] = value

    return new_data