#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# proto_datetime.py

import logging
import datetime
from DateTime.python.DateTime_pb2 import DateTime


def create_proto_datetime(datetime_value: datetime.datetime = None):
    """
    Create a protobuf message filled with the current or given timedate
    :param datetime_value: A datetime.datetime with a custom datetime
                           if not given current time will be returned
    :return: protobuf message
    """
    datetime_msg = DateTime()
    if not isinstance(datetime_value, datetime.datetime):
        datetime_value = datetime.datetime.now()
    for field in datetime_msg.DESCRIPTOR.fields_by_name.keys():
        try:  # set the date
            attr = getattr(datetime_value, field)
            if field == 'isoformat':  # callable attr
                setattr(datetime_msg, field, attr())
            else:  # it is a property
                setattr(datetime_msg, field, attr)
        except Exception as e:
            logging.exception(e)
    return datetime_msg  # -> protobuf.message object


def parse_proto_datetime(proto_msg) -> datetime.datetime:
    """
    Create a datetime object filled with the content of a protobuf message
    :param proto_msg: DateTime protobuf message
    :return: datetime.datetime
    """
    date_time = datetime.datetime(1, 1, 1)
    try:
        year, month, day = proto_msg.year, proto_msg.month, proto_msg.day
        date_time = datetime.datetime(
            year, month, day,
            proto_msg.hour, proto_msg.minute,
            proto_msg.second, proto_msg.microsecond)
    except Exception as e:
        logging.exception(e)
    return date_time


if __name__ == '__main__':
    proto_time = DateTime()
    proto_time.CopyFrom(create_proto_datetime())
    print(proto_time)
    date_time = parse_proto_datetime(proto_time)
    print(date_time)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# proto_datetime.py

import logging
import datetime
from DateTime.python.DateTime_pb2 import DateTime


def create_proto_datetime(datetime_value: datetime.datetime = None):
    """
    Create a protobuf message filled with the current or given timedate
    :param datetime_value: A datetime.datetime with a custom datetime
                           if not given current time will be returned
    :return: protobuf message
    """
    datetime_msg = DateTime()
    if not isinstance(datetime_value, datetime.datetime):
        datetime_value = datetime.datetime.now()
    for field in datetime_msg.DESCRIPTOR.fields_by_name.keys():
        try:  # set the date
            attr = getattr(datetime_value, field)
            if field == 'isoformat':  # callable attr
                setattr(datetime_msg, field, attr())
            else:  # it is a property
                setattr(datetime_msg, field, attr)
        except Exception as e:
            logging.exception(e)
    return datetime_msg  # -> protobuf.message object


def parse_proto_datetime(proto_msg) -> datetime.datetime:
    """
    Create a datetime object filled with the content of a protobuf message
    :param proto_msg: DateTime protobuf message
    :return: datetime.datetime
    """
    date_time = datetime.datetime(1, 1, 1)
    try:
        year, month, day = proto_msg.year, proto_msg.month, proto_msg.day
        date_time = datetime.datetime(
            year, month, day,
            proto_msg.hour, proto_msg.minute,
            proto_msg.second, proto_msg.microsecond)
    except Exception as e:
        logging.exception(e)
    return date_time


if __name__ == '__main__':
    proto_time = DateTime()
    proto_time.CopyFrom(create_proto_datetime())
    print(proto_time)
    date_time = parse_proto_datetime(proto_time)
    print(date_time)

#!/usr/bin/env python3 # -*- coding: utf-8 -*- # proto_datetime.py Create a protobuf message filled with the current or given timedate     :param datetime_value: A datetime.datetime with a custom datetime                            if not given current time will be returned     :return: protobuf message # set the date # callable attr # it is a property # -> protobuf.message object Create a datetime object filled with the content of a protobuf message     :param proto_msg: DateTime protobuf message     :return: datetime.datetime