# Buggy code
def tdef_to_metadata_dict(tdef):
    """
    Helper function to create a TorrentMetadata-compatible dict from TorrentDef
    """
    # We only want to determine the type of the data. XXX filtering is done by the receiving side
    tags = default_category_filter.calculateCategory(
        tdef.metainfo, tdef.get_name_as_unicode()
    )

    return {
        "infohash": tdef.get_infohash(),
        "title": tdef.get_name_as_unicode()[
            :300
        ],  # TODO: do proper size checking based on bytes
        "tags": tags[:200],  # TODO: do proper size checking based on bytes
        "size": tdef.get_length(),
        "torrent_date": datetime.fromtimestamp(tdef.get_creation_date()),
        "tracker_info": get_uniformed_tracker_url(tdef.get_tracker() or "") or "",
    }


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 114, in on_read_data
RuntimeError: [Failure instance: Traceback: <class 'ipv8.messaging.serialization.PackError'>: Could not pack item 8: ('I', -28800)
error: integer out of range for 'I' format code
lib\site-packages\pony\orm\core.py:528:new_func
Tribler\Core\Modules\restapi\mychannel_endpoint.py:286:render_PUT
lib\site-packages\twisted\internet\defer.py:321:addCallback
lib\site-packages\twisted\internet\defer.py:310:addCallbacks
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:653:_runCallbacks
Tribler\Core\Modules\restapi\mychannel_endpoint.py:246:_on_torrent_def_loaded
<auto generated wrapper of add_torrent_to_channel() function>:2:add_torrent_to_channel
lib\site-packages\pony\orm\core.py:515:new_func
Tribler\Core\Modules\MetadataStore\OrmBindings\channel_metadata.py:310:add_torrent_to_channel
Tribler\Core\Modules\MetadataStore\OrmBindings\channel_node.py:213:from_dict
Tribler\Core\Modules\MetadataStore\OrmBindings\torrent_metadata.py:68:__init__
Tribler\Core\Modules\MetadataStore\OrmBindings\channel_node.py:139:__init__
Tribler\Core\Modules\MetadataStore\serialization.py:224:__init__
Tribler\Core\Modules\MetadataStore\serialization.py:179:__init__
Tribler\Core\Modules\MetadataStore\serialization.py:108:__init__
ipv8\messaging\serialization.py:270:pack_multiple
ipv8\messaging\serialization.py:264:pack_multiple
ipv8\messaging\serialization.py:248:pack
ipv8\messaging\serialization.py:171:pack
]

# Fixed code
def tdef_to_metadata_dict(tdef):
    """
    Helper function to create a TorrentMetadata-compatible dict from TorrentDef
    """
    # We only want to determine the type of the data. XXX filtering is done by the receiving side
    tags = default_category_filter.calculateCategory(
        tdef.metainfo, tdef.get_name_as_unicode()
    )
    try:
        torrent_date = datetime.fromtimestamp(tdef.get_creation_date())
    except ValueError:
        torrent_date = EPOCH

    return {
        "infohash": tdef.get_infohash(),
        "title": tdef.get_name_as_unicode()[
            :300
        ],  # TODO: do proper size checking based on bytes
        "tags": tags[:200],  # TODO: do proper size checking based on bytes
        "size": tdef.get_length(),
        "torrent_date": torrent_date if torrent_date >= EPOCH else EPOCH,
        "tracker_info": get_uniformed_tracker_url(tdef.get_tracker() or "") or "",
    }