Use datetime.strftime() on years before 1900? (“require year >= 1900”)

前端 未结 4 1886
别跟我提以往
别跟我提以往 2020-12-14 00:31

I used : utctime = datetime.datetime(1601,1,1) + datetime.timedelta(microseconds = tup[5]) last_visit_time = \"Last visit time:\"+ utctime.strftime(\'%Y-%m-%d %H:%M:%

相关标签:
4条回答
  • 2020-12-14 00:52

    You can do the following:

    >>> utctime.isoformat()
    '1601-01-01T00:00:00.000050'
    

    Now if you want to have exactly the same format as above:

    iso = utctime.isoformat()
    tokens = iso.strip().split("T")
    last_visit_time = "Last visit time: %s %s" % (tokens[0], tokens[1].strip().split(".")[0])
    

    Not that there seems to be a patch for strftime to fix this behavior here (not tested)

    0 讨论(0)
  • 2020-12-14 00:59

    the isoformat method accepts a parameter specifing the character(s) dividing the date part from the time part of a datetime obj in its representation. Therefore:

    >>> utctime.isoformat(" ")
    '1601-01-01 00:00:00.000050'
    

    should do it. Furthermore, if you want to remove the microseconds you can operate a split.

    >>> utctime.isoformat(" ").split(".")[0]
    '1601-01-01 00:00:00'
    
    0 讨论(0)
  • 2020-12-14 01:14

    There are various ways to alter strftime so that it handles pre-1900 dates:

    • There's a recipe at ASPN that gives you a separate strftime method that you can call with a date object: aspn_recipe.strftime(dt, fmt) if you set it up in a module yourself
    • As @stephen-rumbalski said, the external mxDateTime package supports this; but that's using a totally different date object system
    • As of version 1.5, the virtualtime package will patch both time.strftime and datetime.datetime.strftime to behave as in Python 3.3+. You can take advantage of this without enabling the other virtual time functions. (Disclaimer: I work on this package)

    Note that Python 2.7, 3.0 and 3.1 have errors before the year 1900, Python 3.2 has errors before the year 1000. Additionally, pre-3.2 versions interpret years between 0 and 99 as between 1969 and 2068. Python versions from 3.3 onward support all positive years in datetime (and negative years in time.strftime), and time.strftime doesn't do any mapping of years between 0 and 99.

    The original Python bug explains that they decided that this was a feature in Python 2.7 (presumably since it avoided lack of system strftime support for these dates), and then gradually worked on it in the Python 3 series, by reimplementing functionality.

    0 讨论(0)
  • 2020-12-14 01:16

    I recommend using arrow (which is an abstraction package on datetime and dateutil), it's really easy to handle every kind of datetime objects, even in Python 2.6/7.x and with dates prior to 1900.

    For example:

    >>> import arrow
    
    >>> in_date_str = "1853-10-30T13:36:41.942623+00:00"
    >>> in_date_obj = arrow.get(crea)
    >>> print(in_date_obj)
    arrow[1853-10-30T13:36:41.942623+00:00]>
    
    # basic formatting
    >>> in_date_obj.format()
    u'1853-10-30 13:36:41-00:00'
    
    # advanced formatting
    >>> in_date_obj.format("ffffdd D MMMM YYYY", "fr_FR")
    u'Dimanche 30 Octobre 1853'
    
    # humanized delta
    >>> in_date_obj.humanize()
    u'162 years ago'
    # easy localization handling
    >>> in_date_obj.humanize(locale="fr_FR")
    u'il y a 162 ans'
    
    0 讨论(0)
提交回复
热议问题