Instance methods:
+ - replace(year=, month=, day=)
+ Return a date with the same value, except for those fields given
+ new values by whichever keyword arguments are specified. For
+ example, if \code{d == date(2002, 12, 31)}, then
+ \code{d.replace(day=26) == date(2000, 12, 26)}.
+
- timetuple()
Return a 9-element tuple of the form returned by
\function{time.localtime()}. The hours, minutes and seconds are
- time()
Return time object with same hour, minute, second and microsecond.
+ - replace(year=, month=, day=, hour=, minute=, second=, microsecond=)
+ Return a datetime with the same value, except for those fields given
+ new values by whichever keyword arguments are specified.
+
- timetuple()
Return a 9-element tuple of the form returned by
\function{time.localtime()}.
Instance methods:
+ - replace(hour=, minute=, second=, microsecond=)
+ Return a time with the same value, except for those fields given
+ new values by whichever keyword arguments are specified.
+
- isoformat()
Return a string representing the time in ISO 8601 format,
HH:MM:SS.mmmmmm
Instance methods:
+ - replace(hour=, minute=, second=, microsecond=, tzinfo=)
+ Return a timetz with the same value, except for those fields given
+ new values by whichever keyword arguments are specified. Note that
+ \code{tzinfo=None} can be specified to create a naive timetz from an
+ aware timetz.
+
- isoformat()
Return a string representing the time in ISO 8601 format,
HH:MM:SS.mmmmmm
Return \class{timetz} object with same hour, minute, second, microsecond,
and tzinfo.
+ - replace(year=, month=, day=, hour=, minute=, second=, microsecond=,
+ tzinfo=)
+ Return a datetimetz with the same value, except for those fields given
+ new values by whichever keyword arguments are specified. Note that
+ \code{tzinfo=None} can be specified to create a naive datetimetz from
+ an aware datetimetz.
+
- utcoffset()
If \member{tzinfo} is \code{None}, returns \code{None}, else
\code{tzinfo.utcoffset(self)}.
- - tzname():
+ - tzname()
If \member{tzinfo} is \code{None}, returns \code{None}, else
\code{tzinfo.tzname(self)}.
self.assertEqual(cls(1900, 1, 1).strftime("%Y"), "1900")
for y in 1, 49, 51, 99, 100, 1000, 1899:
self.assertRaises(ValueError, cls(y, 1, 1).strftime, "%Y")
+
+ def test_replace(self):
+ cls = self.theclass
+ args = [1, 2, 3]
+ base = cls(*args)
+ self.assertEqual(base, base.replace())
+
+ i = 0
+ for name, newval in (("year", 2),
+ ("month", 3),
+ ("day", 4)):
+ newargs = args[:]
+ newargs[i] = newval
+ expected = cls(*newargs)
+ got = base.replace(**{name: newval})
+ self.assertEqual(expected, got)
+ i += 1
+
+ # Out of bounds.
+ base = cls(2000, 2, 29)
+ self.assertRaises(ValueError, base.replace, year=2001)
+
#############################################################################
# datetime tests
self.assertRaises(TypeError, combine, d, t, 1) # too many args
self.assertRaises(TypeError, combine, "date", "time") # wrong types
+ def test_replace(self):
+ cls = self.theclass
+ args = [1, 2, 3, 4, 5, 6, 7]
+ base = cls(*args)
+ self.assertEqual(base, base.replace())
+
+ i = 0
+ for name, newval in (("year", 2),
+ ("month", 3),
+ ("day", 4),
+ ("hour", 5),
+ ("minute", 6),
+ ("second", 7),
+ ("microsecond", 8)):
+ newargs = args[:]
+ newargs[i] = newval
+ expected = cls(*newargs)
+ got = base.replace(**{name: newval})
+ self.assertEqual(expected, got)
+ i += 1
+
+ # Out of bounds.
+ base = cls(2000, 2, 29)
+ self.assertRaises(ValueError, base.replace, year=2001)
+
+
class TestTime(unittest.TestCase):
theclass = time
self.failUnless(not cls(0))
self.failUnless(not cls())
+ def test_replace(self):
+ cls = self.theclass
+ args = [1, 2, 3, 4]
+ base = cls(*args)
+ self.assertEqual(base, base.replace())
+
+ i = 0
+ for name, newval in (("hour", 5),
+ ("minute", 6),
+ ("second", 7),
+ ("microsecond", 8)):
+ newargs = args[:]
+ newargs[i] = newval
+ expected = cls(*newargs)
+ got = base.replace(**{name: newval})
+ self.assertEqual(expected, got)
+ i += 1
+
+ # Out of bounds.
+ base = cls(1)
+ self.assertRaises(ValueError, base.replace, hour=24)
+ self.assertRaises(ValueError, base.replace, minute=-1)
+ self.assertRaises(ValueError, base.replace, second=100)
+ self.assertRaises(ValueError, base.replace, microsecond=1000000)
+
# A mixin for classes with a tzinfo= argument. Subclasses must define
# theclass as a class atribute, and theclass(1, 1, 1, tzinfo=whatever)
# must be legit (which is true for timetz and datetimetz).
t = cls(0, tzinfo=FixedOffset(-24*60, ""))
self.assertRaises(ValueError, lambda: bool(t))
+ def test_replace(self):
+ cls = self.theclass
+ z100 = FixedOffset(100, "+100")
+ zm200 = FixedOffset(timedelta(minutes=-200), "-200")
+ args = [1, 2, 3, 4, z100]
+ base = cls(*args)
+ self.assertEqual(base, base.replace())
+
+ i = 0
+ for name, newval in (("hour", 5),
+ ("minute", 6),
+ ("second", 7),
+ ("microsecond", 8),
+ ("tzinfo", zm200)):
+ newargs = args[:]
+ newargs[i] = newval
+ expected = cls(*newargs)
+ got = base.replace(**{name: newval})
+ self.assertEqual(expected, got)
+ i += 1
+
+ # Ensure we can get rid of a tzinfo.
+ self.assertEqual(base.tzname(), "+100")
+ base2 = base.replace(tzinfo=None)
+ self.failUnless(base2.tzinfo is None)
+ self.failUnless(base2.tzname() is None)
+
+ # Ensure we can add one.
+ base3 = base2.replace(tzinfo=z100)
+ self.assertEqual(base, base3)
+ self.failUnless(base.tzinfo is base3.tzinfo)
+
+ # Out of bounds.
+ base = cls(1)
+ self.assertRaises(ValueError, base.replace, hour=24)
+ self.assertRaises(ValueError, base.replace, minute=-1)
+ self.assertRaises(ValueError, base.replace, second=100)
+ self.assertRaises(ValueError, base.replace, microsecond=1000000)
+
class TestDateTimeTZ(TestDateTime, TZInfoBase):
theclass = datetimetz
self.assertEqual(d.isoformat('k'), datestr + 'k' + tailstr)
self.assertEqual(str(d), datestr + ' ' + tailstr)
+ def test_replace(self):
+ cls = self.theclass
+ z100 = FixedOffset(100, "+100")
+ zm200 = FixedOffset(timedelta(minutes=-200), "-200")
+ args = [1, 2, 3, 4, 5, 6, 7, z100]
+ base = cls(*args)
+ self.assertEqual(base, base.replace())
+
+ i = 0
+ for name, newval in (("year", 2),
+ ("month", 3),
+ ("day", 4),
+ ("hour", 5),
+ ("minute", 6),
+ ("second", 7),
+ ("microsecond", 8),
+ ("tzinfo", zm200)):
+ newargs = args[:]
+ newargs[i] = newval
+ expected = cls(*newargs)
+ got = base.replace(**{name: newval})
+ self.assertEqual(expected, got)
+ i += 1
+
+ # Ensure we can get rid of a tzinfo.
+ self.assertEqual(base.tzname(), "+100")
+ base2 = base.replace(tzinfo=None)
+ self.failUnless(base2.tzinfo is None)
+ self.failUnless(base2.tzname() is None)
+
+ # Ensure we can add one.
+ base3 = base2.replace(tzinfo=z100)
+ self.assertEqual(base, base3)
+ self.failUnless(base.tzinfo is base3.tzinfo)
+
+ # Out of bounds.
+ base = cls(2000, 2, 29)
+ self.assertRaises(ValueError, base.replace, year=2001)
def test_suite():
allsuites = [unittest.makeSuite(klass, 'test')
/* Constructors. */
+static char *date_kws[] = {"year", "month", "day", NULL};
+
static PyObject *
date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
int month;
int day;
- static char *keywords[] = {
- "year", "month", "day", NULL
- };
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "iii", keywords,
+ if (PyArg_ParseTupleAndKeywords(args, kw, "iii", date_kws,
&year, &month, &day)) {
if (check_date_args(year, month, day) < 0)
return NULL;
0, 0, 0, -1);
}
+static PyObject *
+date_replace(PyDateTime_Date *self, PyObject *args, PyObject *kw)
+{
+ PyObject *clone;
+ PyObject *tuple;
+ int year = GET_YEAR(self);
+ int month = GET_MONTH(self);
+ int day = GET_DAY(self);
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|iii:replace", date_kws,
+ &year, &month, &day))
+ return NULL;
+ tuple = Py_BuildValue("iii", year, month, day);
+ if (tuple == NULL)
+ return NULL;
+ clone = date_new(self->ob_type, tuple, NULL);
+ Py_DECREF(tuple);
+ return clone;
+}
+
static PyObject *date_getstate(PyDateTime_Date *self);
static long
PyDoc_STR("Return the day of the week represented by the date.\n"
"Monday == 0 ... Sunday == 6")},
+ {"replace", (PyCFunction)date_replace, METH_KEYWORDS,
+ PyDoc_STR("Return date with new specified fields.")},
+
{"__setstate__", (PyCFunction)date_setstate, METH_O,
PyDoc_STR("__setstate__(state)")},
/* Constructors. */
+
+static char *datetime_kws[] = {"year", "month", "day",
+ "hour", "minute", "second", "microsecond",
+ NULL};
+
static PyObject *
datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
int second = 0;
int usecond = 0;
- static char *keywords[] = {
- "year", "month", "day", "hour", "minute", "second",
- "microsecond", NULL
- };
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiii", keywords,
+ if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiii", datetime_kws,
&year, &month, &day, &hour, &minute,
&second, &usecond)) {
if (check_date_args(year, month, day) < 0)
return self->hashcode;
}
+static PyObject *
+datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
+{
+ PyObject *clone;
+ PyObject *tuple;
+ int y = GET_YEAR(self);
+ int m = GET_MONTH(self);
+ int d = GET_DAY(self);
+ int hh = DATE_GET_HOUR(self);
+ int mm = DATE_GET_MINUTE(self);
+ int ss = DATE_GET_SECOND(self);
+ int us = DATE_GET_MICROSECOND(self);
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiii:replace",
+ datetime_kws,
+ &y, &m, &d, &hh, &mm, &ss, &us))
+ return NULL;
+ tuple = Py_BuildValue("iiiiiii", y, m, d, hh, mm, ss, us);
+ if (tuple == NULL)
+ return NULL;
+ clone = datetime_new(self->ob_type, tuple, NULL);
+ Py_DECREF(tuple);
+ return clone;
+}
+
static PyObject *
datetime_timetuple(PyDateTime_DateTime *self)
{
"defaults\n"
"to 'T'.")},
+ {"replace", (PyCFunction)datetime_replace, METH_KEYWORDS,
+ PyDoc_STR("Return datetime with new specified fields.")},
+
{"__setstate__", (PyCFunction)datetime_setstate, METH_O,
PyDoc_STR("__setstate__(state)")},
/* Constructors. */
+static char *time_kws[] = {"hour", "minute", "second", "microsecond", NULL};
+
static PyObject *
time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
int second = 0;
int usecond = 0;
- static char *keywords[] = {
- "hour", "minute", "second", "microsecond", NULL
- };
- if (PyArg_ParseTupleAndKeywords(args, kw, "|iiii", keywords,
+ if (PyArg_ParseTupleAndKeywords(args, kw, "|iiii", time_kws,
&hour, &minute, &second, &usecond)) {
if (check_time_args(hour, minute, second, usecond) < 0)
return NULL;
return self->hashcode;
}
+static PyObject *
+time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
+{
+ PyObject *clone;
+ PyObject *tuple;
+ int hh = TIME_GET_HOUR(self);
+ int mm = TIME_GET_MINUTE(self);
+ int ss = TIME_GET_SECOND(self);
+ int us = TIME_GET_MICROSECOND(self);
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiii:replace",
+ time_kws,
+ &hh, &mm, &ss, &us))
+ return NULL;
+ tuple = Py_BuildValue("iiii", hh, mm, ss, us);
+ if (tuple == NULL)
+ return NULL;
+ clone = time_new(self->ob_type, tuple, NULL);
+ Py_DECREF(tuple);
+ return clone;
+}
+
static int
time_nonzero(PyDateTime_Time *self)
{
{"strftime", (PyCFunction)time_strftime, METH_KEYWORDS,
PyDoc_STR("format -> strftime() style string.")},
+ {"replace", (PyCFunction)time_replace, METH_KEYWORDS,
+ PyDoc_STR("Return datetime with new specified fields.")},
+
{"__setstate__", (PyCFunction)time_setstate, METH_O,
PyDoc_STR("__setstate__(state)")},
* Constructors.
*/
+static char *timetz_kws[] = {"hour", "minute", "second", "microsecond",
+ "tzinfo", NULL};
+
static PyObject *
timetz_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
int usecond = 0;
PyObject *tzinfo = Py_None;
- static char *keywords[] = {
- "hour", "minute", "second", "microsecond", "tzinfo", NULL
- };
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", keywords,
+ if (PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO", timetz_kws,
&hour, &minute, &second, &usecond,
&tzinfo)) {
if (check_time_args(hour, minute, second, usecond) < 0)
/* Note: tp_richcompare and tp_hash are inherited from time. */
+static PyObject *
+timetz_replace(PyDateTime_TimeTZ *self, PyObject *args, PyObject *kw)
+{
+ PyObject *clone;
+ PyObject *tuple;
+ int hh = TIME_GET_HOUR(self);
+ int mm = TIME_GET_MINUTE(self);
+ int ss = TIME_GET_SECOND(self);
+ int us = TIME_GET_MICROSECOND(self);
+ PyObject *tzinfo = self->tzinfo;
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiO:replace",
+ timetz_kws,
+ &hh, &mm, &ss, &us, &tzinfo))
+ return NULL;
+ tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
+ if (tuple == NULL)
+ return NULL;
+ clone = timetz_new(self->ob_type, tuple, NULL);
+ Py_DECREF(tuple);
+ return clone;
+}
+
static int
timetz_nonzero(PyDateTime_TimeTZ *self)
{
{"dst", (PyCFunction)timetz_dst, METH_NOARGS,
PyDoc_STR("Return self.tzinfo.dst(self).")},
+ {"replace", (PyCFunction)timetz_replace, METH_KEYWORDS,
+ PyDoc_STR("Return timetz with new specified fields.")},
+
{"__setstate__", (PyCFunction)timetz_setstate, METH_O,
PyDoc_STR("__setstate__(state)")},
((PyDateTime_DateTimeTZ *)self)->tzinfo = newtzinfo;
}
+static char *datetimetz_kws[] = {
+ "year", "month", "day", "hour", "minute", "second",
+ "microsecond", "tzinfo", NULL
+};
+
static PyObject *
datetimetz_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{
int usecond = 0;
PyObject *tzinfo = Py_None;
- static char *keywords[] = {
- "year", "month", "day", "hour", "minute", "second",
- "microsecond", "tzinfo", NULL
- };
-
- if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", keywords,
+ if (PyArg_ParseTupleAndKeywords(args, kw, "iii|iiiiO", datetimetz_kws,
&year, &month, &day, &hour, &minute,
&second, &usecond, &tzinfo)) {
if (check_date_args(year, month, day) < 0)
/* Note: tp_richcompare and tp_hash are inherited from datetime. */
+static PyObject *
+datetimetz_replace(PyDateTime_DateTimeTZ *self, PyObject *args, PyObject *kw)
+{
+ PyObject *clone;
+ PyObject *tuple;
+ int y = GET_YEAR(self);
+ int m = GET_MONTH(self);
+ int d = GET_DAY(self);
+ int hh = DATE_GET_HOUR(self);
+ int mm = DATE_GET_MINUTE(self);
+ int ss = DATE_GET_SECOND(self);
+ int us = DATE_GET_MICROSECOND(self);
+ PyObject *tzinfo = self->tzinfo;
+
+ if (! PyArg_ParseTupleAndKeywords(args, kw, "|iiiiiiiO:replace",
+ datetimetz_kws,
+ &y, &m, &d, &hh, &mm, &ss, &us,
+ &tzinfo))
+ return NULL;
+ tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
+ if (tuple == NULL)
+ return NULL;
+ clone = datetimetz_new(self->ob_type, tuple, NULL);
+ Py_DECREF(tuple);
+ return clone;
+}
+
static PyObject *
datetimetz_timetuple(PyDateTime_DateTimeTZ *self)
{
{"dst", (PyCFunction)datetimetz_dst, METH_NOARGS,
PyDoc_STR("Return self.tzinfo.dst(self).")},
+ {"replace", (PyCFunction)datetimetz_replace, METH_KEYWORDS,
+ PyDoc_STR("Return datetimetz with new specified fields.")},
+
{"__setstate__", (PyCFunction)datetimetz_setstate, METH_O,
PyDoc_STR("__setstate__(state)")},