Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 10cadce

Browse files
committed
Reimplemented datetime.now() to be useful.
1 parent 250684d commit 10cadce

4 files changed

Lines changed: 62 additions & 17 deletions

File tree

Doc/lib/libdatetime.tex

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,25 @@ \subsection{\class{datetime} Objects \label{datetime-datetime}}
525525
See also \method{now()}, \method{fromtimestamp()}.
526526
\end{methoddesc}
527527

528-
\begin{methoddesc}{now}{}
529-
Return the current local datetime. This is like \method{today()},
530-
but, if possible, supplies more precision than can be gotten from
531-
going through a \function{time.time()} timestamp (for example,
532-
this may be possible on platforms that supply the C
528+
\begin{methoddesc}{now(tz=None)}{}
529+
Return the current local date and time. If optional argument
530+
\var{tz} is \code{None} or not specified, this is like
531+
\method{today()}, but, if possible, supplies more precision than can
532+
be gotten from going through a \function{time.time()} timestamp (for
533+
example, this may be possible on platforms supplying the C
533534
\cfunction{gettimeofday()} function).
535+
536+
Else \var{tz} must be an instance of a class \class{tzinfo} subclass,
537+
and the current date and time are translated to \var{tz}'s time
538+
zone. In this case the result is equivalent to
539+
\code{\var{tz}.fromutc(datetime.utcnow().replace(tzinfo=\var{tz})}.
534540
See also \method{today()}, \method{utcnow()}.
535541
\end{methoddesc}
536542

537543
\begin{methoddesc}{utcnow}{}
538-
Return the current UTC datetime, with \member{tzinfo} \code{None}.
539-
This is like \method{now()}, but
540-
returns the current UTC date and time.
544+
Return the current UTC date and time, with \member{tzinfo} \code{None}.
545+
This is like \method{now()}, but returns the current UTC date and time,
546+
as a naive \class{datetime} object.
541547
See also \method{now()}.
542548
\end{methoddesc}
543549

Lib/test/test_datetime.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ def test_tzinfo_now(self):
22282228
# Try with and without naming the keyword.
22292229
off42 = FixedOffset(42, "42")
22302230
another = meth(off42)
2231-
again = meth(tzinfo=off42)
2231+
again = meth(tz=off42)
22322232
self.failUnless(another.tzinfo is again.tzinfo)
22332233
self.assertEqual(another.utcoffset(), timedelta(minutes=42))
22342234
# Bad argument with and w/o naming the keyword.
@@ -2239,6 +2239,24 @@ def test_tzinfo_now(self):
22392239
# Too many args.
22402240
self.assertRaises(TypeError, meth, off42, off42)
22412241

2242+
# We don't know which time zone we're in, and don't have a tzinfo
2243+
# class to represent it, so seeing whether a tz argument actually
2244+
# does a conversion is tricky.
2245+
weirdtz = FixedOffset(timedelta(hours=15, minutes=58), "weirdtz", 0)
2246+
utc = FixedOffset(0, "utc", 0)
2247+
for dummy in range(3):
2248+
now = datetime.now(weirdtz)
2249+
self.failUnless(now.tzinfo is weirdtz)
2250+
utcnow = datetime.utcnow().replace(tzinfo=utc)
2251+
now2 = utcnow.astimezone(weirdtz)
2252+
if abs(now - now2) < timedelta(seconds=30):
2253+
break
2254+
# Else the code is broken, or more than 30 seconds passed between
2255+
# calls; assuming the latter, just try again.
2256+
else:
2257+
# Three strikes and we're out.
2258+
self.fail("utcnow(), now(tz), or astimezone() may be broken")
2259+
22422260
def test_tzinfo_fromtimestamp(self):
22432261
import time
22442262
meth = self.theclass.fromtimestamp
@@ -2448,7 +2466,7 @@ def test_more_astimezone(self):
24482466
f44m = FixedOffset(44, "44")
24492467
fm5h = FixedOffset(-timedelta(hours=5), "m300")
24502468

2451-
dt = self.theclass.now(tzinfo=f44m)
2469+
dt = self.theclass.now(tz=f44m)
24522470
self.failUnless(dt.tzinfo is f44m)
24532471
# Replacing with degenerate tzinfo raises an exception.
24542472
self.assertRaises(ValueError, dt.astimezone, fnone)

Misc/NEWS

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ Extension modules
8282
creativity of political time zone fiddling appears unbounded -- fromutc()
8383
allows the highly motivated to emulate any scheme expressible in Python.
8484

85+
datetime.now(): The optional tzinfo argument was undocumented (that's
86+
repaired), and its name was changed to tz ("tzinfo" is overloaded enough
87+
already). With a tz argument, now(tz) used to return the local date
88+
and time, and attach tz to it, without any conversion of date and time
89+
members. This was less than useful. Now now(tz) returns the current
90+
date and time as local time in tz's time zone, akin to
91+
tz.fromutc(datetime.utcnow().replace(tzinfo=utc))
92+
where "utc" is an instance of a tzinfo subclass modeling UTC. Without
93+
a tz argument, now() continues to return the current local date and time,
94+
as a naive datetime object.
95+
8596
The constructors building a datetime from a timestamp could raise
8697
ValueError if the platform C localtime()/gmtime() inserted "leap
8798
seconds". Leap seconds are ignored now. On such platforms, it's

Modules/datetimemodule.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,15 +3666,25 @@ datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo)
36663666
static PyObject *
36673667
datetime_now(PyObject *cls, PyObject *args, PyObject *kw)
36683668
{
3669-
PyObject *self = NULL;
3669+
PyObject *self;
36703670
PyObject *tzinfo = Py_None;
3671-
static char *keywords[] = {"tzinfo", NULL};
3671+
static char *keywords[] = {"tz", NULL};
36723672

3673-
if (PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords,
3674-
&tzinfo)) {
3675-
if (check_tzinfo_subclass(tzinfo) < 0)
3676-
return NULL;
3677-
self = datetime_best_possible(cls, localtime, tzinfo);
3673+
if (! PyArg_ParseTupleAndKeywords(args, kw, "|O:now", keywords,
3674+
&tzinfo))
3675+
return NULL;
3676+
if (check_tzinfo_subclass(tzinfo) < 0)
3677+
return NULL;
3678+
3679+
self = datetime_best_possible(cls,
3680+
tzinfo == Py_None ? localtime : gmtime,
3681+
tzinfo);
3682+
if (self != NULL && tzinfo != Py_None) {
3683+
/* Convert UTC to tzinfo's zone. */
3684+
PyObject *temp = self;
3685+
self = PyObject_CallMethod(tzinfo, "fromutc",
3686+
"O", self);
3687+
Py_DECREF(temp);
36783688
}
36793689
return self;
36803690
}

0 commit comments

Comments
 (0)