[Python] Validating and normalizing string timestamps in Python

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I want a method, written in Python with the following signature:

    def validate_and_normalize_timestamp(timestamp: str) -> str:
    ...


    that validates that the input timestamp: str is a valid RFC3339 timestamp and outputs a normalized string in the format:

    YYYY-MM-DDTHH:MM:SS.SSSZ


    This should support arbitrary precision in the fractional seconds, i.e., S, S.S, S.SS, S.SSS, S.SSSS, and so on.

    There are multitude of methods in Python, but I am looking for the most simple and robust way. Python's built-in datetime module cannot be used due to lack of support for nanoseconds. And I don't want a data structure output. I want a string.

    Building a relatively basic regular expression and validation logic can be done, but that would require more work to handle timezones and things that may hope is already handled properly.

    Continue reading...

Compartilhe esta Página