[issue1158] %f format for datetime objects

2008-03-28 Thread Georg Brandl

Changes by Georg Brandl <[EMAIL PROTECTED]>:


--
resolution:  -> accepted
status: open -> closed

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2008-03-15 Thread Skip Montanaro

Skip Montanaro <[EMAIL PROTECTED]> added the comment:

Checking this in.  All tests pass.  Have for quite awhile.
rev 61402.

--
assignee:  -> skip.montanaro

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-12-09 Thread Skip Montanaro

Changes by Skip Montanaro:


Removed file: http://bugs.python.org/file8903/datetime-f.diff

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-12-09 Thread Skip Montanaro

Skip Montanaro added the comment:

Stop me before I update it again!  This update touches up
datetime_strptime a bit.  It's more uniform and a bit faster.

Added file: http://bugs.python.org/file8907/datetime-f.diff

__
Tracker <[EMAIL PROTECTED]>

__Index: Doc/library/datetime.rst
===
--- Doc/library/datetime.rst(revision 59446)
+++ Doc/library/datetime.rst(working copy)
@@ -1491,10 +1491,32 @@
 be used, as time objects have no such values.  If they're used anyway, ``1900``
 is substituted for the year, and ``0`` for the month and day.
 
-For :class:`date` objects, the format codes for hours, minutes, and seconds
-should not be used, as :class:`date` objects have no such values.  If they're
-used anyway, ``0`` is substituted for them.
+For :class:`date` objects, the format codes for hours, minutes, seconds, and
+microseconds should not be used, as :class:`date` objects have no such
+values.  If they're used anyway, ``0`` is substituted for them.
 
+:class:`time` and :class:`datetime` objects support a ``%f`` format code
+which expands to the number of microseconds in the object, zero-padded on
+the left to six places.
+
+.. versionadded:: 2.6
+
+For a naive object, the ``%z`` and ``%Z`` format codes are replaced by empty
+strings.
+
+For an aware object:
+
+``%z``
+   :meth:`utcoffset` is transformed into a 5-character string of the form 
+HHMM or
+   -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, 
and
+   MM is a 2-digit string giving the number of UTC offset minutes.  For 
example, if
+   :meth:`utcoffset` returns ``timedelta(hours=-3, minutes=-30)``, ``%z`` is
+   replaced with the string ``'-0330'``.
+
+``%Z``
+   If :meth:`tzname` returns ``None``, ``%Z`` is replaced by an empty string.
+   Otherwise ``%Z`` is replaced by the returned value, which must be a string.
+
 The full set of format codes supported varies across platforms, because Python
 calls the platform C library's :func:`strftime` function, and platform
 variations are common.  
@@ -1526,6 +1548,10 @@
 | ``%d``| Day of the month as a decimal  |   |
 |   | number [01,31].|   |
 +---++---+
+| ``%f``| Microsecond as a decimal   | \(1)  |
+|   | number [0,99], zero-padded |   |
+|   | on the left|   |
++---++---+
 | ``%H``| Hour (24-hour clock) as a  |   |
 |   | decimal number [00,23].|   |
 +---++---+
@@ -1541,13 +1567,13 @@
 | ``%M``| Minute as a decimal number |   |
 |   | [00,59].   |   |
 +---++---+
-| ``%p``| Locale's equivalent of either  | \(1)  |
+| ``%p``| Locale's equivalent of either  | \(2)  |
 |   | AM or PM.  |   |
 +---++---+
-| ``%S``| Second as a decimal number | \(2)  |
+| ``%S``| Second as a decimal number | \(3)  |
 |   | [00,61].   |   |
 +---++---+
-| ``%U``| Week number of the year| \(3)  |
+| ``%U``| Week number of the year| \(4)  |
 |   | (Sunday as the first day of|   |
 |   | the week) as a decimal number  |   |
 |   | [00,53].  All days in a new|   |
@@ -1558,7 +1584,7 @@
 | ``%w``| Weekday as a decimal number|   |
 |   | [0(Sunday),6]. |   |
 +---++---+
-| ``%W``| Week number of the year| \(3)  |
+| ``%W``| Week number of the year| \(4)  |
 |   | (Monday as the first day of|   |
 |   | the week) as a decimal number  |   |
 |   | [00,53].  All days in a new|   |
@@ -1578,7 +1604,7 @@
 | ``%Y``| Year with century as a decimal |   |
 |   | number.|   |
 +---++---+
-| ``%z``| UTC offset in the form +HHMM   | \(4)  |
+| ``%z``| UTC offset in the form +HHMM   | \(5)  |
 |   | or -HHMM (empty string if the  |   |
 |   | the object is naive).  |   |
 +---++---+
@@ -1591,17 +1617,22 @@
 Notes:
 
 (1)
+   When used with the :func:`strptime` function, the ``%f`` directive
+   accepts from one to six digits and zero pads on the right.  ``%f`` is
+   an extension to the set of format characters in the C standard.
+
+(2)
When used with the :func:`strptime` function, the ``%p`` directive only 
affects
the output hour field if th

[issue1158] %f format for datetime objects

2007-12-09 Thread Skip Montanaro

Changes by Skip Montanaro:


Removed file: http://bugs.python.org/file8901/datetime-f.diff

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-12-09 Thread Skip Montanaro

Changes by Skip Montanaro:


Removed file: http://bugs.python.org/file8439/dt-30.diff

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-12-09 Thread Skip Montanaro

Skip Montanaro added the comment:

Updated diff.  Just a tweak to datetime.rst.

Added file: http://bugs.python.org/file8903/datetime-f.diff

__
Tracker <[EMAIL PROTECTED]>

__Index: Doc/library/datetime.rst
===
--- Doc/library/datetime.rst(revision 59441)
+++ Doc/library/datetime.rst(working copy)
@@ -1491,10 +1491,32 @@
 be used, as time objects have no such values.  If they're used anyway, ``1900``
 is substituted for the year, and ``0`` for the month and day.
 
-For :class:`date` objects, the format codes for hours, minutes, and seconds
-should not be used, as :class:`date` objects have no such values.  If they're
-used anyway, ``0`` is substituted for them.
+For :class:`date` objects, the format codes for hours, minutes, seconds, and
+microseconds should not be used, as :class:`date` objects have no such
+values.  If they're used anyway, ``0`` is substituted for them.
 
+:class:`time` and :class:`datetime` objects support a ``%f`` format code
+which expands to the number of microseconds in the object, zero-padded on
+the left to six places.
+
+.. versionadded:: 2.6
+
+For a naive object, the ``%z`` and ``%Z`` format codes are replaced by empty
+strings.
+
+For an aware object:
+
+``%z``
+   :meth:`utcoffset` is transformed into a 5-character string of the form 
+HHMM or
+   -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, 
and
+   MM is a 2-digit string giving the number of UTC offset minutes.  For 
example, if
+   :meth:`utcoffset` returns ``timedelta(hours=-3, minutes=-30)``, ``%z`` is
+   replaced with the string ``'-0330'``.
+
+``%Z``
+   If :meth:`tzname` returns ``None``, ``%Z`` is replaced by an empty string.
+   Otherwise ``%Z`` is replaced by the returned value, which must be a string.
+
 The full set of format codes supported varies across platforms, because Python
 calls the platform C library's :func:`strftime` function, and platform
 variations are common.  
@@ -1526,6 +1548,10 @@
 | ``%d``| Day of the month as a decimal  |   |
 |   | number [01,31].|   |
 +---++---+
+| ``%f``| Microsecond as a decimal   | \(1)  |
+|   | number [0,99], zero-padded |   |
+|   | on the left|   |
++---++---+
 | ``%H``| Hour (24-hour clock) as a  |   |
 |   | decimal number [00,23].|   |
 +---++---+
@@ -1541,13 +1567,13 @@
 | ``%M``| Minute as a decimal number |   |
 |   | [00,59].   |   |
 +---++---+
-| ``%p``| Locale's equivalent of either  | \(1)  |
+| ``%p``| Locale's equivalent of either  | \(2)  |
 |   | AM or PM.  |   |
 +---++---+
-| ``%S``| Second as a decimal number | \(2)  |
+| ``%S``| Second as a decimal number | \(3)  |
 |   | [00,61].   |   |
 +---++---+
-| ``%U``| Week number of the year| \(3)  |
+| ``%U``| Week number of the year| \(4)  |
 |   | (Sunday as the first day of|   |
 |   | the week) as a decimal number  |   |
 |   | [00,53].  All days in a new|   |
@@ -1558,7 +1584,7 @@
 | ``%w``| Weekday as a decimal number|   |
 |   | [0(Sunday),6]. |   |
 +---++---+
-| ``%W``| Week number of the year| \(3)  |
+| ``%W``| Week number of the year| \(4)  |
 |   | (Monday as the first day of|   |
 |   | the week) as a decimal number  |   |
 |   | [00,53].  All days in a new|   |
@@ -1578,7 +1604,7 @@
 | ``%Y``| Year with century as a decimal |   |
 |   | number.|   |
 +---++---+
-| ``%z``| UTC offset in the form +HHMM   | \(4)  |
+| ``%z``| UTC offset in the form +HHMM   | \(5)  |
 |   | or -HHMM (empty string if the  |   |
 |   | the object is naive).  |   |
 +---++---+
@@ -1591,17 +1617,22 @@
 Notes:
 
 (1)
+   When used with the :func:`strptime` function, the ``%f`` directive
+   accepts from one to six digits and zero pads on the right.  ``%f`` is
+   an extension to the set of format characters in the C standard.
+
+(2)
When used with the :func:`strptime` function, the ``%p`` directive only 
affects
the output hour field if the ``%I`` directive is used to parse the hour.
 
-(2)
+(3)
The range rea

[issue1158] %f format for datetime objects

2007-12-09 Thread Skip Montanaro

Skip Montanaro added the comment:

Actually, I think I will avoid the 3.0 patch altogether and let these
changes propagate from trunk to py3k by whoever works that magic.

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-12-09 Thread Skip Montanaro

Changes by Skip Montanaro:


Removed file: http://bugs.python.org/file8438/dt-26.diff

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-12-09 Thread Skip Montanaro

Skip Montanaro added the comment:

Okay, here's my latest patch (datetime-f.diff).  2.6 only at this point.

Added file: http://bugs.python.org/file8901/datetime-f.diff

__
Tracker <[EMAIL PROTECTED]>

__Index: Doc/library/datetime.rst
===
--- Doc/library/datetime.rst(revision 59441)
+++ Doc/library/datetime.rst(working copy)
@@ -1491,10 +1491,32 @@
 be used, as time objects have no such values.  If they're used anyway, ``1900``
 is substituted for the year, and ``0`` for the month and day.
 
-For :class:`date` objects, the format codes for hours, minutes, and seconds
-should not be used, as :class:`date` objects have no such values.  If they're
-used anyway, ``0`` is substituted for them.
+For :class:`date` objects, the format codes for hours, minutes, seconds, and
+fractions of sections should not be used, as :class:`date` objects have no
+such values.  If they're used anyway, ``0`` is substituted for them.
 
+:class:`time` and :class:`datetime` objects support a ``%f`` format code
+which expands to the number of microseconds in the object, zero-padded on
+the left to six places.
+
+.. versionadded:: 2.6
+
+For a naive object, the ``%z`` and ``%Z`` format codes are replaced by empty
+strings.
+
+For an aware object:
+
+``%z``
+   :meth:`utcoffset` is transformed into a 5-character string of the form 
+HHMM or
+   -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, 
and
+   MM is a 2-digit string giving the number of UTC offset minutes.  For 
example, if
+   :meth:`utcoffset` returns ``timedelta(hours=-3, minutes=-30)``, ``%z`` is
+   replaced with the string ``'-0330'``.
+
+``%Z``
+   If :meth:`tzname` returns ``None``, ``%Z`` is replaced by an empty string.
+   Otherwise ``%Z`` is replaced by the returned value, which must be a string.
+
 The full set of format codes supported varies across platforms, because Python
 calls the platform C library's :func:`strftime` function, and platform
 variations are common.  
Index: Lib/_strptime.py
===
--- Lib/_strptime.py(revision 59441)
+++ Lib/_strptime.py(working copy)
@@ -22,7 +22,7 @@
 except:
 from dummy_thread import allocate_lock as _thread_allocate_lock
 
-__all__ = ['strptime']
+__all__ = []
 
 def _getlang():
 # Figure out what the current language is set to.
@@ -190,6 +190,7 @@
 base.__init__({
 # The " \d" part of the regex is to make %c from ANSI C work
 'd': r"(?P3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
+'f': r"(?P[0-9]{1,6})",
 'H': r"(?P2[0-3]|[0-1]\d|\d)",
 'I': r"(?P1[0-2]|0[1-9]|[1-9])",
 'j': 
r"(?P36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
@@ -291,7 +292,7 @@
 return 1 + days_to_week + day_of_week
 
 
-def strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
+def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
 """Return a time struct based on the input string and the format string."""
 global _TimeRE_cache, _regex_cache
 with _cache_lock:
@@ -327,7 +328,7 @@
   data_string[found.end():])
 year = 1900
 month = day = 1
-hour = minute = second = 0
+hour = minute = second = fraction = 0
 tz = -1
 # Default to -1 to signify that values not known; not critical to have,
 # though
@@ -384,6 +385,11 @@
 minute = int(found_dict['M'])
 elif group_key == 'S':
 second = int(found_dict['S'])
+elif group_key == 'f':
+s = found_dict['f']
+# Pad to always return microseconds.
+s += "0" * (6 - len(s))
+fraction = int(s)
 elif group_key == 'A':
 weekday = locale_time.f_weekday.index(found_dict['A'].lower())
 elif group_key == 'a':
@@ -440,6 +446,9 @@
 day = datetime_result.day
 if weekday == -1:
 weekday = datetime_date(year, month, day).weekday()
-return time.struct_time((year, month, day,
- hour, minute, second,
- weekday, julian, tz))
+return (time.struct_time((year, month, day,
+  hour, minute, second,
+  weekday, julian, tz)), fraction)
+
+def _strptime_time(data_string, format="%a %b %d %H:%M:%S %Y"):
+return _strptime(data_string, format)[0]
Index: Lib/test/test_strptime.py
===
--- Lib/test/test_strptime.py   (revision 59441)
+++ Lib/test/test_strptime.py   (working copy)
@@ -208,11 +208,11 @@
 
 def test_ValueError(self):
 # Make sure ValueError is raised when match fails or format is bad
-self.assertRaises(ValueError, _strptime.strptime, data_string="%d",
+sel

[issue1158] %f format for datetime objects

2007-09-17 Thread Skip Montanaro

Skip Montanaro added the comment:

Brett> In terms of strptime, I would just change _strptime.strptime() to
Brett> _strptime._strptime() and have it return everything along with
Brett> the microseconds measurement.  Then have public functions that
Brett> call that function and either strip off the microseconds or not.

Sounds good.  I'll work that into my patch(es).

Skip

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-17 Thread Brett Cannon

Brett Cannon added the comment:

In terms of strptime, I would just change _strptime.strptime() to
_strptime._strptime() and have it return everything along with the
microseconds measurement.  Then have public functions that call that
function and either strip off the microseconds or not.

-Brett

On 9/17/07, Skip Montanaro <[EMAIL PROTECTED]> wrote:
>
> Skip Montanaro added the comment:
>
> Brett,
>
> Continuing the discussion of strptime... It returns a time.struct_time.
> Would it cause too much breakage (only do this in 3.0) to add a tm_usec
> field to the end of that object?  It seems a lot of bits of code might
> still expect a length 9 tuple-ish thing and do this:
>
>   yy, mm, dd, hh, mm, ss, wk, j, tz = time.strptime(...)
>
> If we could make that change for 3.0 that might allow strptime to parse
> %f format codes.
>
> S
>
> __
> Tracker <[EMAIL PROTECTED]>
> 
> __
> ___
> Python-bugs-list mailing list
> Unsubscribe: 
> http://mail.python.org/mailman/options/python-bugs-list/brett%40python.org
>
>

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-17 Thread Skip Montanaro

Skip Montanaro added the comment:

Brett,

Continuing the discussion of strptime... It returns a time.struct_time.
Would it cause too much breakage (only do this in 3.0) to add a tm_usec
field to the end of that object?  It seems a lot of bits of code might
still expect a length 9 tuple-ish thing and do this:

  yy, mm, dd, hh, mm, ss, wk, j, tz = time.strptime(...)

If we could make that change for 3.0 that might allow strptime to parse
%f format codes.

S

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-16 Thread Skip Montanaro

Changes by Skip Montanaro:


__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-16 Thread Skip Montanaro

Skip Montanaro added the comment:

Here are new patches for 2.6 and 3.0 including changes
to doc and test cases.

__
Tracker <[EMAIL PROTECTED]>

__Index: Lib/test/test_datetime.py
===
--- Lib/test/test_datetime.py	(revision 58174)
+++ Lib/test/test_datetime.py	(working copy)
@@ -1488,9 +1488,9 @@
 
 def test_more_strftime(self):
 # This tests fields beyond those tested by the TestDate.test_strftime.
-t = self.theclass(2004, 12, 31, 6, 22, 33)
-self.assertEqual(t.strftime("%m %d %y %S %M %H %j"),
-"12 31 04 33 22 06 366")
+t = self.theclass(2004, 12, 31, 6, 22, 33, 47)
+self.assertEqual(t.strftime("%m %d %y %f %S %M %H %j"),
+"12 31 04 47 33 22 06 366")
 
 def test_extract(self):
 dt = self.theclass(2002, 3, 4, 18, 45, 3, 1234)
@@ -1763,7 +1763,7 @@
 
 def test_strftime(self):
 t = self.theclass(1, 2, 3, 4)
-self.assertEqual(t.strftime('%H %M %S'), "01 02 03")
+self.assertEqual(t.strftime('%H %M %S %f'), "01 02 03 04")
 # A naive object replaces %z and %Z with empty strings.
 self.assertEqual(t.strftime("'%z' '%Z'"), "'' ''")
 
Index: Modules/datetimemodule.c
===
--- Modules/datetimemodule.c	(revision 58174)
+++ Modules/datetimemodule.c	(working copy)
@@ -1130,10 +1130,24 @@
 	return 0;
 }
 
+static PyObject *
+make_freplacement(PyObject *object)
+{
+	char freplacement[7];
+	if (PyTime_Check(object))
+	sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object));
+	else if (PyDateTime_Check(object))
+	sprintf(freplacement, "%06d", DATE_GET_MICROSECOND(object));
+	else
+	sprintf(freplacement, "%06d", 0);
+
+	return PyString_FromStringAndSize(freplacement, strlen(freplacement));
+}
+
 /* 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 and %Z format codes via a preprocessing
- * step on the format string.
+ * giving special meanings to the %z, %Z and %f format codes via a
+ * preprocessing step on the format string.
  * tzinfoarg is the argument to pass to the object's tzinfo method, if
  * needed.
  */
@@ -1145,6 +1159,7 @@
 
 	PyObject *zreplacement = NULL;	/* py string, replacement for %z */
 	PyObject *Zreplacement = NULL;	/* py string, replacement for %Z */
+	PyObject *freplacement = NULL;	/* py string, replacement for %f */
 
 	char *pin;	/* pointer to next char in input format */
 	char ch;	/* next char in input format */
@@ -1186,11 +1201,11 @@
 		}
 	}
 
-	/* Scan the input format, looking for %z and %Z escapes, building
+	/* Scan the input format, looking for %z/%Z/%f escapes, building
 	 * a new format.  Since computing the replacements for those codes
 	 * is expensive, don't unless they're actually used.
 	 */
-	totalnew = PyString_Size(format) + 1;	/* realistic if no %z/%Z */
+	totalnew = PyString_Size(format) + 1;	/* realistic if no %z/%Z/%f */
 	newfmt = PyString_FromStringAndSize(NULL, totalnew);
 	if (newfmt == NULL) goto Done;
 	pnew = PyString_AsString(newfmt);
@@ -1272,6 +1287,18 @@
 			ptoappend = PyString_AS_STRING(Zreplacement);
 			ntoappend = PyString_GET_SIZE(Zreplacement);
 		}
+		else if (ch == 'f') {
+			/* format microseconds */
+			if (freplacement == NULL) {
+freplacement = make_freplacement(object);
+if (freplacement == NULL)
+	goto Done;
+			}
+			assert(freplacement != NULL);
+			assert(PyString_Check(freplacement));
+			ptoappend = PyString_AS_STRING(freplacement);
+			ntoappend = PyString_GET_SIZE(freplacement);
+		}
 		else {
 			/* percent followed by neither z nor Z */
 			ptoappend = pin - 2;
@@ -1313,6 +1340,7 @@
 		Py_DECREF(time);
 	}
  Done:
+	Py_XDECREF(freplacement);
 	Py_XDECREF(zreplacement);
 	Py_XDECREF(Zreplacement);
 	Py_XDECREF(newfmt);
Index: Doc/library/datetime.rst
===
--- Doc/library/datetime.rst	(revision 58174)
+++ Doc/library/datetime.rst	(working copy)
@@ -1297,10 +1297,16 @@
 be used, as time objects have no such values.  If they're used anyway, ``1900``
 is substituted for the year, and ``0`` for the month and day.
 
-For :class:`date` objects, the format codes for hours, minutes, and seconds
-should not be used, as :class:`date` objects have no such values.  If they're
-used anyway, ``0`` is substituted for them.
+For :class:`date` objects, the format codes for hours, minutes, seconds, and
+fractions of sections should not be used, as :class:`date` objects have no
+such values.  If they're used anyway, ``0`` is substituted for them.
 
+:class:`time` and :class:`datetime` objects support a ``%f`` format code
+which expands to the

[issue1158] %f format for datetime objects

2007-09-16 Thread Skip Montanaro

Changes by Skip Montanaro:


__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-13 Thread Brett Cannon

Brett Cannon added the comment:

On 9/13/07, Skip Montanaro <[EMAIL PROTECTED]> wrote:
>
> Skip Montanaro added the comment:
>
> Brett> Are you going to add support to strptime as well?
>
> I looked at strptime for about two seconds then moved on.  I presume you
> would know how to add it easily though. ;-)
>

Adding support is not issue.  It's exposing the microseconds to
datetime.  It would probably require something along the lines of
returning a tuple with microseconds included and have time use a
function that creates another tuple with that info removed and
datetime using the more informational tuple.

But adding the %f support is very straightforward.

> Brett> As for the 'time' module, I don't think it would be useful as it 
> serves
> Brett> no purpose since the time tuple can't resolve to that
> Brett> resolution.
>
> Resolution isn't the issue.  You just make sure you add enough zeroes to
> make it be microseconds.  The bigger problem is that time.strftime() takes a
> tuple of ints which basically represents a struct tm.  That struct has an
> int seconds field and no sub-second field.  You'd either have to start
> allowing floating point tm_sec fields then either truncating or rounding
> (but which?) to get ints when you need to actually generate an actual struct
> tm.

Right.  I am in favour of this being just a datetime thing as we
should get people moved off of time for stuff like this anyway.

-Brett

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-13 Thread Skip Montanaro

Skip Montanaro added the comment:

Eric> It's a nit, but there are a few other comments that should be
Eric> changed to mention %f in addition to %z/%Z.

Yes, thanks.

Skip

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-13 Thread Skip Montanaro

Skip Montanaro added the comment:

Brett> Are you going to add support to strptime as well?

I looked at strptime for about two seconds then moved on.  I presume you
would know how to add it easily though. ;-)

Brett> As for the 'time' module, I don't think it would be useful as it 
serves
Brett> no purpose since the time tuple can't resolve to that
Brett> resolution. 

Resolution isn't the issue.  You just make sure you add enough zeroes to
make it be microseconds.  The bigger problem is that time.strftime() takes a
tuple of ints which basically represents a struct tm.  That struct has an
int seconds field and no sub-second field.  You'd either have to start
allowing floating point tm_sec fields then either truncating or rounding
(but which?) to get ints when you need to actually generate an actual struct
tm.

Skip

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-13 Thread Eric V. Smith

Eric V. Smith added the comment:

It's a nit, but there are a few other comments that should be changed to
mention %f in addition to %z/%Z.

 * giving special meanings to the %z and %Z format codes via a preprocessing

/* Scan the input format, looking for %z and %Z escapes, building

/* percent followed by neither z nor Z */

--
nosy: +eric.smith

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-12 Thread Brett Cannon

Brett Cannon added the comment:

Are you going to add support to strptime as well?

As for the 'time' module, I don't think it would be useful as it serves
no purpose since the time tuple can't resolve to that resolution.  If
you do add support, though, I guess you just default to 0.

--
nosy: +brett.cannon

__
Tracker <[EMAIL PROTECTED]>

__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1158] %f format for datetime objects

2007-09-12 Thread Skip Montanaro

New submission from Skip Montanaro:

Attached is a patch for py3k which adds a %f format code to its strftime
method.  When included in a format string it expands to the number of
microseconds in the object.  date, time and datetime objects all support
the format (though I'm not sure what, if anything, it means for date
objects).

I don't know how practical this is for time.strftime() level because the
inputs are all so excited about being ints.  Still, if you wanted to
allow the seconds field to be a float and were willing to cast it to an
int where necessary and shadow struct tm with a pseudo-float-friendly
version of the same, you could probably smash it in there as well.

--
components: Extension Modules
files: percent-f.diff
keywords: patch, py3k
messages: 55876
nosy: skip.montanaro
priority: normal
severity: normal
status: open
title: %f format for datetime objects
type: rfe
versions: Python 3.0

__
Tracker <[EMAIL PROTECTED]>

__Index: Modules/datetimemodule.c
===
--- Modules/datetimemodule.c	(revision 58132)
+++ Modules/datetimemodule.c	(working copy)
@@ -1177,6 +1177,15 @@
 	return NULL;
 }
 
+static PyObject *
+make_freplacement(PyObject *object)
+{
+	char freplacement[7];
+	sprintf(freplacement, "%06d", TIME_GET_MICROSECOND(object));
+
+	return PyBytes_FromStringAndSize(freplacement, strlen(freplacement));
+}
+
 /* 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 and %Z format codes via a preprocessing
@@ -1192,6 +1201,7 @@
 
 	PyObject *zreplacement = NULL;	/* py string, replacement for %z */
 	PyObject *Zreplacement = NULL;	/* py string, replacement for %Z */
+	PyObject *freplacement = NULL;	/* py string, replacement for %f */
 
 	const char *pin;/* pointer to next char in input format */
 Py_ssize_t flen;/* length of input format */
@@ -1243,7 +1253,7 @@
 	 * a new format.  Since computing the replacements for those codes
 	 * is expensive, don't unless they're actually used.
 	 */
-	totalnew = flen + 1;	/* realistic if no %z/%Z */
+	totalnew = flen + 1;	/* realistic if no %z/%Z/%f */
 	newfmt = PyBytes_FromStringAndSize(NULL, totalnew);
 	if (newfmt == NULL) goto Done;
 	pnew = PyBytes_AsString(newfmt);
@@ -1301,6 +1311,18 @@
 			ptoappend = PyBytes_AS_STRING(Zreplacement);
 			ntoappend = PyBytes_GET_SIZE(Zreplacement);
 		}
+		else if (ch == 'f') {
+			/* format microseconds */
+			if (freplacement == NULL) {
+freplacement = make_freplacement(object);
+if (freplacement == NULL)
+	goto Done;
+			}
+			assert(freplacement != NULL);
+			assert(PyBytes_Check(freplacement));
+			ptoappend = PyBytes_AS_STRING(freplacement);
+			ntoappend = PyBytes_GET_SIZE(freplacement);
+		}
 		else {
 			/* percent followed by neither z nor Z */
 			ptoappend = pin - 2;
@@ -1347,6 +1369,7 @@
 		Py_DECREF(time);
 	}
  Done:
+	Py_XDECREF(freplacement);
 	Py_XDECREF(zreplacement);
 	Py_XDECREF(Zreplacement);
 	Py_XDECREF(newfmt);
@@ -3159,7 +3182,7 @@
 {
 	char buf[100];
 	PyObject *result;
-	int us = TIME_GET_MICROSECOND(self);;
+	int us = TIME_GET_MICROSECOND(self);
 
 	if (us)
 		result = PyUnicode_FromFormat("%02d:%02d:%02d.%06d",
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com