Python fromtimestamp OSError

前端 未结 5 1217
天命终不由人
天命终不由人 2020-12-03 16:36

For some reason when constructing datetimes using fromtimestamp, I get a \"OSError [Errno22] Invalid Argument\" when I use negative times less than -43200 (-12hrs). I am on

5条回答
  •  佛祖请我去吃肉
    2020-12-03 17:34

    @wim's answer is correct, but anyone else arriving here might be interested in testing it (adjust range if you wish):

    import datetime
    import platform
    print(
        "Running on Python ver.{} on {} {}\n" \
        .format(
            platform.python_version(),
            platform.system(),
            platform.release()
            )
    )
    for timestamp in range(1, 100000000):
        try:
            dt = datetime.datetime.fromtimestamp(timestamp)
        except:
            pass
        else:
            break
    print(
        "Smallest accepted Unix timestamp by {}: '{}' ({})" \
        .format(platform.system(), timestamp, dt)
    )
    

    What I got was:

    A:\src\X.utilities>test.py
    Running on Python ver.3.6.1 on Windows 7
    
    Smallest accepted Unix timestamp by Windows: '86400' (1970-01-02 02:00:00)
    

提交回复
热议问题