classification
Title: format, str.format don't work well with datetime, date object
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.0, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, falsetru
Priority: normal Keywords:

Created on 2009-06-20 15:14 by falsetru, last changed 2009-06-20 15:46 by falsetru. This issue is now closed.

Messages (3)
msg89539 - (view) Author: Jeong-Min Lee (falsetru) Date: 2009-06-20 15:14
format(datetime_obj, format_string) return format_string. (when
format_string is not empty.)


>>> import datetime
>>> d = datetime.datetime.now()
>>> format(d)
'2009-06-20 23:51:54.243428'
>>> format(d, '')
'2009-06-20 23:51:54.243428'
>>> d
datetime.datetime(2009, 6, 20, 23, 51, 54, 243428)
>>> '{0}'.format(d)
'2009-06-20 23:51:54.243428'
>>> '{0:30}'.format(d) # odd
'30'
>>> format(d, '30') # odd
'30'
>>> format(str(d), '30') # workaround
'2009-06-20 23:51:54.243428    '
>>> '{0!s:30}'.format(d) # workaround
'2009-06-20 23:51:54.243428    '
msg89540 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-06-20 15:32
This is by design. Where d is a datetime, format(d, format_string)
returns d.strftime(format_string).

>>> d.strftime('30')
'30'
msg89541 - (view) Author: Jeong-Min Lee (falsetru) Date: 2009-06-20 15:46
I got it.
By the way, It would be good to document that this behaviour (at least
about datetime.__format__)
History
Date User Action Args
2009-06-20 15:46:55falsetrusetmessages: + msg89541
2009-06-20 15:32:29eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg89540
2009-06-20 15:24:23eric.smithsetassignee: eric.smith

components: + Interpreter Core, - Extension Modules, Library (Lib)
nosy: + eric.smith
2009-06-20 15:14:11falsetrucreate