from typing import Any
from datetime import datetime


def datetime_encoder(data: Any) -> Any:
    """
    A datetime encoder for `msgpack`

    Usage:

    ```
    from datetime import datetime
    from scalpel import datetime_encoder
    import msgpack

    data = {'fruit': 'apple', 'date': datetime.utcnow()}
    packed_data = msgpack.packb(data, default=datetime_encoder)
    ```
    """
    if isinstance(data, datetime):
        return {'__datetime__': True, 'as_str': data.strftime('%Y%m%dT%H:%M:%S.%f')}
    return data