python modulus

0 views
Skip to first unread message

redsna...@gmail.com

unread,
Dec 8, 2009, 8:57:46 AM12/8/09
to Python Ireland
I'm trying to use Python's Modulus operator and getting weird
results.

I'm trying to calculate the week that a date falls on.

My first week, week1 is:

week1 = time.mktime(time.strptime("6 Jan 10", "%d %b %y"))

My test date is:
now = time.mktime(time.strptime("24 Jan 10", "%d %b %y"))

(now - week1) % (3600*24*7) should result in c. 2.5 (week 3).

Instead I'm getting a result of:
In [45]: (now - week1) % (3600*24*7)
Out[45]: 345600.0

I'm using Python 2.6.2 (r262:71600, Aug 21 2009, 12:23:57) (default
Fedora 12 RPM)

Is this like the from __future__ import division issue?

TIA,
John

Nick Johnson

unread,
Dec 8, 2009, 9:04:10 AM12/8/09
to python...@googlegroups.com
Hi John,

On Tue, Dec 8, 2009 at 1:57 PM, jo...@nuatech.net <redsna...@gmail.com> wrote:
I'm trying to use Python's Modulus operator and getting weird
results.

I'm trying to calculate the week that a date falls on.

My first week, week1 is:

week1 = time.mktime(time.strptime("6 Jan 10", "%d %b %y"))

My test date is:
now = time.mktime(time.strptime("24 Jan 10", "%d %b %y"))

(now - week1) %  (3600*24*7) should result in c. 2.5 (week 3).

The modulo operator here will return the second of the week, not the week number itself. You want (now - week1) / (3600*24*7).

-Nick
 

Instead I'm getting a result of:
In [45]: (now - week1) %  (3600*24*7)
Out[45]: 345600.0

I'm using Python 2.6.2 (r262:71600, Aug 21 2009, 12:23:57) (default
Fedora 12 RPM)

Is this like the from __future__ import division issue?

TIA,
John

--

You received this message because you are subscribed to the Google Groups "Python Ireland" group.
To post to this group, send email to python...@googlegroups.com.
To unsubscribe from this group, send email to pythonirelan...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pythonireland?hl=en.



Padraig Kitterick

unread,
Dec 8, 2009, 9:04:15 AM12/8/09
to python...@googlegroups.com
> --
>
> You received this message because you are subscribed to the Google Groups "Python Ireland" group.
> To post to this group, send email to python...@googlegroups.com.
> To unsubscribe from this group, send email to pythonirelan...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pythonireland?hl=en.
>
>
>
It's doing exactly what you asked it to do:

>>> now-week1
1555200.0
>>> (3600*24*7)
604800
>>> (now-week1) / d
2.5714285714285716
>>> (now-week1) % d
345600.0

Padraig

redsna...@gmail.com

unread,
Dec 8, 2009, 9:15:03 AM12/8/09
to Python Ireland
Thanks Nick and Padraig,

Stupid mistake on my part.

J

On Dec 8, 2:04 pm, Nick Johnson <arach...@notdot.net> wrote:
> Hi John,
>
> > pythonirelan...@googlegroups.com<pythonireland%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages