是否有比任何有效的datetime.datetime(…)更小的比较对象?

2024-10-01 13:46:10 发布

您现在位置:Python中文网/ 问答频道 /正文

对于datetime,是否有一个MIN常数

我需要它来表示资源的过期日期,并且我希望有一个表示“总是过期”的datetime作为默认值(而不是None,这样无论发生什么我都可以使用相同的过期比较)

所以现在我用datetime.datetime(datetime.MINYEAR,1,1,0,0,tzusc())作为我的MIN datetime,但我想知道是否有其他方法来表示“最低可能时间”(即使它不是datetime


Tags: 方法nonedatetime时间常数资源minminyear
3条回答

您可以使用time.gmtime(0),它是epoch

Time Documentation

The epoch is the point where the time starts. On January 1st of that year, at 0 hours, the “time since the epoch” is zero. For Unix, the epoch is 1970. To find out what the epoch is, look at gmtime(0).

你可以试试-^{}。根据documentation-

datetime.min

The earliest representable datetime, datetime(MINYEAR, 1, 1, tzinfo=None).

datetime.datetime.fromtimestamp(0)它会给你:

datetime.datetime(1970, 1, 1, 1, 0)

Return the local date corresponding to the POSIX timestamp, such as is returned by time.time(). This may raise ValueError, if the timestamp is out of the range of values supported by the platform C localtime() function. It’s common for this to be restricted to years from 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by fromtimestamp().

相关问题 更多 >