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

Issue using datetime with format() #52037

Closed
JordanS mannequin opened this issue Jan 26, 2010 · 8 comments
Closed

Issue using datetime with format() #52037

JordanS mannequin opened this issue Jan 26, 2010 · 8 comments
Assignees
Labels
docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@JordanS
Copy link
Mannequin

JordanS mannequin commented Jan 26, 2010

BPO 7789
Nosy @birkenfeld, @abalkin, @ericvsmith, @bitdancer
Superseder
  • bpo-8913: Document that datetime.format is datetime.strftime
  • 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 = 'https://github.com/birkenfeld'
    closed_at = <Date 2010-10-04.14:15:09.680>
    created_at = <Date 2010-01-26.19:21:56.128>
    labels = ['type-bug', 'docs']
    title = 'Issue using datetime with format()'
    updated_at = <Date 2010-10-04.14:15:09.679>
    user = 'https://bugs.python.org/JordanS'

    bugs.python.org fields:

    activity = <Date 2010-10-04.14:15:09.679>
    actor = 'belopolsky'
    assignee = 'georg.brandl'
    closed = True
    closed_date = <Date 2010-10-04.14:15:09.680>
    closer = 'belopolsky'
    components = ['Documentation']
    creation = <Date 2010-01-26.19:21:56.128>
    creator = 'JordanS'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 7789
    keywords = []
    message_count = 8.0
    messages = ['98358', '98362', '98363', '98365', '98367', '98369', '98370', '117949']
    nosy_count = 5.0
    nosy_names = ['georg.brandl', 'belopolsky', 'eric.smith', 'r.david.murray', 'JordanS']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '8913'
    type = 'behavior'
    url = 'https://bugs.python.org/issue7789'
    versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2']

    @JordanS
    Copy link
    Mannequin Author

    JordanS mannequin commented Jan 26, 2010

    format() cannot handle datetime.DateTime objects, returns the format_spec instead, without applying formatting to it, perhaps default behaviour in case of unknown type. Different modifications, ie: using str.format() syntax produce same behaviour.

    Sample code:

    import datetime
    
    #data
    row = {0: 1, 'BeamId': 218, 2: 0.0, 3: 0.0, 4: datetime.datetime(2006, 7, 18, 0, 12), 5: datetime.datetime(2007, 2, 23, 18, 26, 55, 450000), 6: 32637.774406455803, 1: 218, 'DateAndTime': datetime.datetime(2007, 2, 23, 18, 26, 55, 450000), 'AvgFlexuralStrengthDown': 32637.774406455803, 'WarmUpTime': datetime.datetime(2006, 7, 18, 0, 12), 'AvgFlexuralStrengthUp': 15916.5463146028, 'IceSheetId': 1, 'Y': 0.0, 'X': 0.0, 7: 15916.5463146028}
    
    titles = ['BeamId', 'DateAndTime', 'AvgFlexuralStrengthDown', 'WarmUpTime', 'AvgFlexuralStrengthUp', 'IceSheetId', 'Y', 'X']
    
    #attempt to print datetime: ignores formatting, doesn't print datetime value, prints format_spec instead
    for key in titles:
        asLine = "{0:*<30}".format(row[key])
        print(asLine),
    print '\n'
    
    #prints a repr of datetime
    for key in titles:
        asLine = "{0!r:*<30}".format(row[key])
        print(asLine),
    print '\n'
    
    #prints datetime as string
    for key in titles:
        asLine = "{0!s:*<30}".format(row[key])
        print(asLine),
    print '\n'

    @JordanS JordanS mannequin added topic-2to3 type-bug An unexpected behavior, bug, or error labels Jan 26, 2010
    @briancurtin briancurtin added stdlib Python modules in the Lib dir and removed topic-2to3 labels Jan 26, 2010
    @JordanS
    Copy link
    Mannequin Author

    JordanS mannequin commented Jan 26, 2010

    sorry, first time posting anything like this:
    versions:

    Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32

    @ericvsmith
    Copy link
    Member

    datetime.datetime passes its format string to strftime:

    >>> import datetime
    >>> x = datetime.datetime(2001, 1, 2, 3, 4)
    >>> x.strftime('%Y-%m-%d')
    '2001-01-02'
    >>> '{0:%Y-%m-%d}'.format(x)
    '2001-01-02'

    I'll check to make sure this is documented.

    @ericvsmith ericvsmith added docs Documentation in the Doc dir and removed stdlib Python modules in the Lib dir labels Jan 26, 2010
    @ericvsmith ericvsmith self-assigned this Jan 26, 2010
    @bitdancer
    Copy link
    Member

    If it is, it isn't any place obvious. I thought I remembered something about using strftime strings in format, but when I looked in the docs for datetime and the section on the format mini language I couldn't find it, so I ended up doing '{} ...'.format(x.strftime("...") in my code...

    @ericvsmith
    Copy link
    Member

    I don't think this is documented (that I can find, at least), so I'll assign it to Georg.

    I think the correct thing to do is something like this, in the datetime, date, and time object descriptions:

    date.__format__(fmt)
        For a date d, format(d, fmt) is equivalent to d.strftime(fmt).

    Ditto for date.__format__. But maybe there's a better, more obvious place to document this.

    @ericvsmith
    Copy link
    Member

    The documentation for this belongs in the mini-language specification, since that just address built-in types. Each type can define its own format specification language.

    So I think adding documentation of __format__ to each non-builtin type that implements __format__ is probably the best way to go.

    @ericvsmith
    Copy link
    Member

    Eric Smith wrote:

    The documentation for this belongs in the mini-language specification, ...

    Oops. "does NOT belong in the mini-language specification".

    @ericvsmith ericvsmith assigned birkenfeld and unassigned ericvsmith Jan 27, 2010
    @abalkin
    Copy link
    Member

    abalkin commented Oct 4, 2010

    The original bug report is invalid and the documentation issue is a duplicate of bpo-8913.

    @abalkin abalkin closed this as completed Oct 4, 2010
    @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
    docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants