incrementing a time tuple by one day

D

David Stockwell

I'm sure this has been asked before, but I wasn't able to find it.

First off I know u can't change a tuple but if I wanted to increment a time
tuple by one day what is the standard method to do that?

I've tried the obvious things and haven't gotten very far.

I have a time tuple that was created like this:
aDate = '19920228'
x = time.strptime(aDate,"%Y%m%d")
print x
(1992, 2, 28, 0, 0, 0, 4, 59, -1)

y = time.mktime(x) + time.mktime((0,0,1,0,0,0,0,0,0))
print y
1643277600.0
print time.ctime(y)
'Thu Jan 27 05:00:00 2022'

It appears to have decremented by a day and a month instead of increment.

What am I doing wrong?

Thanks




David
-------
Surf a wave to the future with a free tracfone
http://cellphone.duneram.com/index.html

_________________________________________________________________
Check out Election 2004 for up-to-date election news, plus voter tools and
more! http://special.msn.com/msn/election2004.armx
 
P

Peter Hansen

David said:
I'm sure this has been asked before, but I wasn't able to find it.

First off I know u can't change a tuple but if I wanted to increment a
time tuple by one day what is the standard method to do that?

I've tried the obvious things and haven't gotten very far.

I have a time tuple that was created like this:
aDate = '19920228'
x = time.strptime(aDate,"%Y%m%d")
print x
(1992, 2, 28, 0, 0, 0, 4, 59, -1)

y = time.mktime(x) + time.mktime((0,0,1,0,0,0,0,0,0))
print y
1643277600.0
print time.ctime(y)
'Thu Jan 27 05:00:00 2022'

It appears to have decremented by a day and a month instead of increment.

What am I doing wrong?

What you're doing wrong is: not using the datetime module...
'Sat Feb 29 00:00:00 1992'


-Peter
 
B

Bengt Richter

I'm sure this has been asked before, but I wasn't able to find it.

First off I know u can't change a tuple but if I wanted to increment a time
tuple by one day what is the standard method to do that?

I've tried the obvious things and haven't gotten very far.

I have a time tuple that was created like this:
aDate = '19920228'
x = time.strptime(aDate,"%Y%m%d")
print x
(1992, 2, 28, 0, 0, 0, 4, 59, -1)

y = time.mktime(x) + time.mktime((0,0,1,0,0,0,0,0,0))
print y
1643277600.0
print time.ctime(y)
'Thu Jan 27 05:00:00 2022'
^^^^
the trouble is that you are adding a time delta in seconds since some epoch
instead of adding 24*60*60 seconds (one day).

Note your supposed 1-day delta value in seconds: 944035200.0

Or in days: 10926.333333333334

Fri Feb 28 00:00:00 1992

To get a one-day delta, you could calculate it (in general you'd have to
watch out for leap stuff, but this case seems to work)
'Sat Feb 29 00:00:00 1992'
It appears to have decremented by a day and a month instead of increment.
You didn't read the entire date ;-)
What am I doing wrong?
Misinterpreting time.mktime((0,0,0,0,0,0,0,0,0)) as being zero-based?
943948800.0

Regards,
Bengt Richter
 
D

Donn Cave

What you're doing wrong is: not using the datetime module...

'Sat Feb 29 00:00:00 1992'


$ python
Python 2.2 (#1, 11/12/02, 23:31:59)
[GCC Apple cpp-precomp 6.14] on darwin
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):

Well, who knows, maybe datetime is the answer for him,
but if not, I would just use 24*60*60 instead of trying
to get one day in seconds out of mktime(). (I think if
you look at the date closer, it isn't decremented all!)

Donn Cave, (e-mail address removed)
 
M

Michael Hoffman

Donn said:
$ python
Python 2.2 (#1, 11/12/02, 23:31:59)
[GCC Apple cpp-precomp 6.14] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named datetime

The datetime module is new in Python 2.3.
 
C

Cameron Laird

.
.
.
Well, who knows, maybe datetime is the answer for him,
but if not, I would just use 24*60*60 instead of trying
to get one day in seconds out of mktime(). (I think if
you look at the date closer, it isn't decremented all!)

Donn Cave, (e-mail address removed)

While I quickly lost track of who said what, I've been around
long enough to know that some people are touchy about there
NOT being 24 * 60 * 60 seconds in a day near "daylight-savings"
switches. I have no idea whether that's the case for Mr.
Stockwell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top