What is the oldest time that can be represented in Python?

前端 未结 5 933
耶瑟儿~
耶瑟儿~ 2021-02-06 21:40

I have written a function comp(time1, time2) which will return True when time1 is less than time2. I have a scenario where

相关标签:
5条回答
  • 2021-02-06 21:48

    If using the datetime module, date, time, and datetime objects all have a min and max attribute.

    >>> from datetime import date, time, datetime
    >>> date.min
    datetime.date(1, 1, 1)
    >>> date.max
    datetime.date(9999, 12, 31)
    >>> time.min
    datetime.time(0, 0)
    >>> time.max
    datetime.time(23, 59, 59, 999999)
    >>> datetime.min
    datetime.datetime(1, 1, 1, 0, 0)
    >>> datetime.max
    datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)
    
    0 讨论(0)
  • 2021-02-06 21:52

    Certain functions in the datetime module obey datetime.MINYEAR and datetime.MAXYEAR and will raise a ValueException for dates outside that range. These are assigned to 1 and 9999, respectively.

    The calender module relies heavily on the datetime module, but in general, observes the “proleptic Gregorian”, which extends indefinately in both directions.

    the time module similarly places no particular restrictions on year elements in time tuple values, and calculates times and dates using only seconds since the epoch.


    That being said, you cannot reliably process dates before about February 12, 1582, when the Gregorian calender was adopted. Before that day, dates were computed using a variety of location dependent calenders, for which there is no support in standard python.

    0 讨论(0)
  • 2021-02-06 21:57

    In python, the datetime object exports the following constants

    datetime.MINYEAR
    The smallest year number allowed in a date or datetime object. MINYEAR is 1.
    
    datetime.MAXYEAR
    The largest year number allowed in a date or datetime object. MAXYEAR is 9999.
    

    http://docs.python.org/library/datetime.html

    0 讨论(0)
  • 2021-02-06 22:03

    If you're using standard issue unix timestamp values then the earliest representable moment of time is back in 1970:

    >>> import time
    >>> time.gmtime(0)
    (1970, 1, 1, 0, 0, 0, 3, 1, 0)
    
    0 讨论(0)
  • 2021-02-06 22:07

    If you're using the time module, you have no guarantee, because it defers to C library functions on the platform that can handle implementation-defined minimum and maximum times. https://docs.python.org/3/library/time.html states:

    Most of the functions defined in this module call platform C library functions with the same name. It may sometimes be helpful to consult the platform documentation, because the semantics of these functions varies among platforms.

    and https://docs.python.org/3/library/time.html#time.mktime states:

    The earliest date for which it can generate a time is platform-dependent.

    That's because these functions take or return time_t values, and per the C11 standard:

    The range and precision of times representable in clock_t and time_t are implementation-defined.

    Unlike the datetime module, the time module does not expose any constants indicating the minimum and maximum values it supports, so if you truly need to find the min and max for your platform then you'd need to write some code to find them experimentally at runtime, e.g. using an exponential search.

    0 讨论(0)
提交回复
热议问题