Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime/date strftime() method and time.strftime() inconsistency #47031

Closed
mishok13 mannequin opened this issue May 7, 2008 · 3 comments
Closed

datetime/date strftime() method and time.strftime() inconsistency #47031

mishok13 mannequin opened this issue May 7, 2008 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@mishok13
Copy link
Mannequin

mishok13 mannequin commented May 7, 2008

BPO 2782
Nosy @gpshead, @tonal

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-06-02.04:11:59.806>
created_at = <Date 2008-05-07.14:45:42.699>
labels = ['type-bug', 'library']
title = 'datetime/date strftime() method and time.strftime() inconsistency'
updated_at = <Date 2008-06-02.04:11:59.759>
user = 'https://bugs.python.org/mishok13'

bugs.python.org fields:

activity = <Date 2008-06-02.04:11:59.759>
actor = 'gregory.p.smith'
assignee = 'none'
closed = True
closed_date = <Date 2008-06-02.04:11:59.806>
closer = 'gregory.p.smith'
components = ['Library (Lib)']
creation = <Date 2008-05-07.14:45:42.699>
creator = 'mishok13'
dependencies = []
files = []
hgrepos = []
issue_num = 2782
keywords = []
message_count = 3.0
messages = ['66360', '67332', '67627']
nosy_count = 4.0
nosy_names = ['gregory.p.smith', 'shura_zam', 'mishok13', 'sbj3']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2782'
versions = ['Python 2.5']

@mishok13
Copy link
Mannequin Author

mishok13 mannequin commented May 7, 2008

datetime and date strftime() method does additional check on input
format, thus being completely different from time's module
time.strftime() method behavior.
There are two ways to fix this:

  1. Add an explicit note about this behavior (e.g., "only 'str' objects
    are allowed for format strings") in docs (section 5.1.7).
  2. Allow 'unicode' objects for format strings (backport time.strftime()
    from 3.0?).

Here is a traceback for a more complete overview:

Python 2.6a2+ (trunk:62762, May  6 2008, 14:37:27)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime, date
>>> import time
>>> uformat = u'%Y-%m-%D %H-%M-%S'
>>> format = '%Y-%m-%D %H-%M-%S'
>>> datetime.today().strftime(uformat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: strftime() argument 1 must be str, not unicode
>>> datetime.today().strftime(format)
'2008-05-05/07/08 17-19-03'
>>> time.strftime(uformat)
'2008-05-05/07/08 17-19-10'
>>> time.strftime(format)
'2008-05-05/07/08 17-19-16'
>>> date.today()
datetime.date(2008, 5, 7)
>>> date.today().strftime(format)
'2008-05-05/07/08 00-00-00'
>>> date.today().strftime(uformat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: strftime() argument 1 must be str, not unicode

@mishok13 mishok13 mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 7, 2008
@gpshead
Copy link
Member

gpshead commented May 25, 2008

Yes, sounds like a bug. I'll fix it.

But should time.strftime allow a unicode format string as input in the
first place? For backwards compatibility I'd say yes. But.. Sane
output can not be guaranteed from time.strftime when given a unicode
format string if it contains multibyte characters that happen to have a
valid (bytewise) % format code in them within a multibyte character.
Anyways the output is always byte string without a specified encoding so
giving it actual unicode characters as input is not advised (at least in
2.6, i didn't check 3.0).

there's an amusing comment in Modules/datetimemodule.c:

/* I sure don't want to reproduce the strftime code from the time module,

  • so this imports the module and calls it. All the hair is due to
  • giving special meanings to the %z, %Z and %f format codes via a
  • preprocessing step on the format string.
    ...
    */
    static PyObject * wrap_strftime(

@gpshead gpshead self-assigned this May 25, 2008
@gpshead
Copy link
Member

gpshead commented Jun 2, 2008

Fixed in trunk (2.6) r63887.

@gpshead gpshead closed this as completed Jun 2, 2008
@gpshead gpshead removed their assignment Jun 2, 2008
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant