Skip to content

Using naive object to get timezone out of a timestamp should not raise an error #11

@LefterisJP

Description

@LefterisJP

I am not sure if this is the right way to do it. but if you want to get a datetime from a timestamp and be aware of the timestamp's timezone (especially whether it's DST or not) you need to do the following (if there is a better/smarter way please let me know because this feels weird). This is in my locale time CET/CEST.

>>> formatstr = '%d/%m/%Y %H:%M:%S %Z'
>>> ts = 1675261083
>>> datetime.datetime.fromtimestamp(ts, tz=datetime.datetime.fromtimestamp(ts).astimezone().tzinfo).strftime(formatstr)
'01/02/2023 15:18:03 CET'
>>> ts = 1686065064
>>> datetime.datetime.fromtimestamp(ts, tz=datetime.datetime.fromtimestamp(ts).astimezone().tzinfo).strftime(formatstr)
'06/06/2023 17:24:24 CEST

This works fine and is correct but flake8-datetimez complains DTZ006 The use ofdatetime.datetime.fromtimestamp()withouttz argument is not allowed.

I think this is a false positive in this case and it should not complain. I am going to add noqa ignore but perhaps there is a way to code this into the logic of the tool?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions