On Fri, Feb 20, 2009 at 6:21 PM, Jeff FW <jeff...@gmail.com> wrote:

>
> I just tried running the code that the "U" date-formatting parameter
> uses, and for me, it was off by about 11.5 days.  According to the
> documentation, the "U" parameter is not implemented:
> http://docs.djangoproject.com/en/dev/ref/templates/builtins/#now
>
> According to this ticket, someone might change the documentation soon,
> so you might want to get a word in beforehand:
> http://code.djangoproject.com/ticket/9850
>
> I'd do it, but you already have the lovely test case :-)
>

Note that test case doesn't work on Windows, where strftime('%s') doesn't
work the same as on Unix:

Python 2.6 (r26:66721, Oct  2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> now = datetime.now()
>>> now.strftime('%s')
''
>>> quit()

So we can't add a testcase that relies on strftime('%s') returning anything
in particular.  The Python docs note that anything outside of what is
explicitly listed (and %s is not listed) is platform-dependent and may vary
unpredictably.

Near as I can tell, also, the "U" date-formatting implementation doesn't
work.  It is:

    def U(self):
        "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
        off = self.timezone.utcoffset(self.data)
        return int(time.mktime(self.data.timetuple())) + off.seconds * 60

If you break down what that's doing in a shell, considering an easy time
like Jan 1 1970 on the dot in my tz:

>>> import datetime
>>> x = datetime.datetime(1970,1,1)
>>> x.strftime('%s')
'18000'

That makes sense, midnight Jan 1 1970 in my timezone -> 5 hours
(18000/60/60) UTC (or is it GMT?).  Whichever, 5 hours sounds right.  But
the U formatter comes up with:

>>> from django.utils.dateformat import format
>>> format(x,'U')
u'4122000'

? Not the same.  U up there is method on a DateFormat object initialized
with the datetime value:

>>> from django.utils.dateformat import DateFormat
>>> df = DateFormat(x)
>>> df.data
datetime.datetime(1970, 1, 1, 0, 0)
>>> df.timezone
EST

So this is what off winds up being:

>>> off = df.timezone.utcoffset(x)
>>> off
datetime.timedelta(-1, 68400)
>>> off.days
-1
>>> off.seconds
68400

This is negative one day, plus 68,4000 seconds, equivalent to -18,000
seconds, but the subsequent code:

return int(time.mktime(self.data.timetuple())) + off.seconds * 60

doesn't consider the negative days value and just uses the 68,4000 seconds
value, so that's a problem.

Furthermore I don't understand why this code multiplies a seconds value by
60.  You divide seconds by 60 to get minutes, and again to get hours, but
why would you multiply seconds by 60?  It may be related to the fact that
the Ptyhon docs (
http://docs.python.org/library/datetime.html#datetime.tzinfo.utcoffset) say
the timezone utcoffset is a minutes value, which is a bit confusing since in
fact later on it says a timedelta should be returned.  But surely it
wouldn't be expected that the seconds value of the returned timedelta would
contain a number of minutes?

Finally, in fact, at least on my box, doing any adjustment to the first bit
isn't going to result in a value that matches strftime('%s'), because the
first part is already what we get with strftime('%s'):

>>> int(time.mktime(x.timetuple()))
18000

So, that may be another problem with the testcase -- the strftime('%s') half
is assuming the answer is in UTC while the "U" format seems to be trying to
adjust the answer so that it is expressed in local time -- even if that was
working properly the answers wouldn't match.  But I'll admit trying to
figure out datetimes and timezones, etc. in Python rather makes my head spin
so I could be missing something here.

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to