Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,有没有办法对1900年之前的日期使用类似strftime的函数?_Python_Oracle_Datetime_Sql Loader_Strftime - Fatal编程技术网

在Python中,有没有办法对1900年之前的日期使用类似strftime的函数?

在Python中,有没有办法对1900年之前的日期使用类似strftime的函数?,python,oracle,datetime,sql-loader,strftime,Python,Oracle,Datetime,Sql Loader,Strftime,我没有意识到这一点,但Python的strftime函数显然不支持1900年之前的日期: >>> from datetime import datetime >>> d = datetime(1899, 1, 1) >>> d.strftime('%Y-%m-%d') Traceback (most recent call last): File "<stdin>", line 1, in <module> Val

我没有意识到这一点,但Python的
strftime
函数显然不支持1900年之前的日期:

>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900
唯一的区别是
str(d)
相当于
d.isoformat(“”)

这是ctime库(UTF)的“功能”。 此外,您可能在2038年以上遇到问题。

strftime()。无论平台是什么,1900年之前的年份都不能使用

因此,不会有使用
strftime()
的解决方案。幸运的是,“手工”完成这项工作非常简单:

可以处理任意日期。Python的
time
datetime
模块在内部使用UNIX时间戳,这就是它们的范围有限的原因

In [5]: mx.DateTime.DateTime(1899)
Out[5]: <mx.DateTime.DateTime object for '1899-01-01 00:00:00.00' at 154a960>

In [6]: DateTime.DateTime(1899).Format('%Y-%m-%d')
Out[6]: 1899-01-01
[5]中的
:mx.DateTime.DateTime(1899)
出[5]:
[6]中的DateTime.DateTime(1899).格式(“%Y-%m-%d”)
Out[6]:1899-01-01
这是来自。可以为您自己的滚动提供一个良好的起点

def strftime(self, dt, fmt):
    fmt = self.illegal_s.sub(r"\1", fmt)
    fmt = fmt.replace("%s", "s")
    if dt.year > 1900:
        return cbook.unicode_safe(dt.strftime(fmt))

    year = dt.year
    # For every non-leap year century, advance by
    # 6 years to get into the 28-year repeat cycle
    delta = 2000 - year
    off = 6*(delta // 100 + delta // 400)
    year = year + off

    # Move to around the year 2000
    year = year + ((2000 - year)//28)*28
    timetuple = dt.timetuple()
    s1 = time.strftime(fmt, (year,) + timetuple[1:])
    sites1 = self._findall(s1, str(year))

    s2 = time.strftime(fmt, (year+28,) + timetuple[1:])
    sites2 = self._findall(s2, str(year+28))

    sites = []
    for site in sites1:
        if site in sites2:
        sites.append(site)

    s = s1
    syear = "%4d" % (dt.year,)
    for site in sites:
        s = s[:site] + syear + s[site+4:]

    return cbook.unicode_safe(s)
适用于不受范围限制的
datetime
实例:

>>> import datetime
>>> x=datetime.datetime(1865, 7, 2, 9, 30, 21)
>>> x.isoformat()
'1865-07-02T09:30:21'

如果您需要不同格式的字符串,那么从
isoformat
获得的字符串片段进行切片、切块和混合并不难,这是非常一致的(
YYYY-MM-DDTHH:MM:SS.mmmmmm
,如果微秒为零,则省略点和后面的微秒)。

我明白了。所以问题是日期不能在正负32位之外?确切地说,没有直接的解决方案。我们已经发布了这么多示例代码,说明了为什么不需要strftime()。呃。。。就在我认为我能够在不使用mxODBC的情况下将mx*作为依赖项删除时:-(这基本上就是我最后所做的。啊,来源于我对matplotlib的贡献。sniff:)@dalke,太棒了。在堆栈溢出上有一个相当小的世界。
def strftime(self, dt, fmt):
    fmt = self.illegal_s.sub(r"\1", fmt)
    fmt = fmt.replace("%s", "s")
    if dt.year > 1900:
        return cbook.unicode_safe(dt.strftime(fmt))

    year = dt.year
    # For every non-leap year century, advance by
    # 6 years to get into the 28-year repeat cycle
    delta = 2000 - year
    off = 6*(delta // 100 + delta // 400)
    year = year + off

    # Move to around the year 2000
    year = year + ((2000 - year)//28)*28
    timetuple = dt.timetuple()
    s1 = time.strftime(fmt, (year,) + timetuple[1:])
    sites1 = self._findall(s1, str(year))

    s2 = time.strftime(fmt, (year+28,) + timetuple[1:])
    sites2 = self._findall(s2, str(year+28))

    sites = []
    for site in sites1:
        if site in sites2:
        sites.append(site)

    s = s1
    syear = "%4d" % (dt.year,)
    for site in sites:
        s = s[:site] + syear + s[site+4:]

    return cbook.unicode_safe(s)
>>> import datetime
>>> x=datetime.datetime(1865, 7, 2, 9, 30, 21)
>>> x.isoformat()
'1865-07-02T09:30:21'