I have written a function comp(time1, time2)
which will return True
when time1
is less than time2
. I have a scenario where time1
should always be less than time2
. I need time1
to have the least possible value (i.e. represent the earliest possible moment). How can I get this time?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
If you're using standard issue unix timestamp values then the earliest representable moment of time is back in 1970:
Certain functions in the
datetime
module obeydatetime.MINYEAR
anddatetime.MAXYEAR
and will raise aValueException
for dates outside that range. These are assigned to 1 and 9999, respectively.The
calender
module relies heavily on thedatetime
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.
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:and https://docs.python.org/3/library/time.html#time.mktime states:
That's because these functions take or return
time_t
values, and per the C11 standard:Unlike the
datetime
module, thetime
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.If using the datetime module, date, time, and datetime objects all have a
min
andmax
attribute.In python, the datetime object exports the following constants
http://docs.python.org/library/datetime.html