473,408 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

incrementing a time tuple by one day

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

Jul 18 '05 #1
5 3626
David Stockwell wrote:
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...
aDate = '19920228'
x = time.strptime(aDate, '%Y%m%d')
print x (1992, 2, 28, 0, 0, 0, 4, 59, -1) d = datetime.datetime.fromtimestamp(time.mktime(x))
d datetime.datetime(1992, 2, 28, 0, 0) y = d + datetime.timedelta(days=1)
y.ctime()

'Sat Feb 29 00:00:00 1992'
-Peter
Jul 18 '05 #2
On Thu, 23 Sep 2004 16:22:08 +0000, "David Stockwell" <wi*******@hotmail.com> wrote:
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:
time.mktime((0,0,1,0,0,0,0,0,0)) 944035200.0

Or in days: time.mktime((0,0,1,0,0,0,0,0,0))/(60*60*24) 10926.333333333334

import time
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))
z = time.mktime(x) + 24*60*60
print time.ctime(y) Thu Jan 27 08:00:00 2022 print time.ctime(z) Sat Feb 29 00:00:00 1992 print time.ctime(time.mktime(x)) 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)
time.mktime((0,0,1,0,0,0,0,0,0)) - time.mktime((0,0,0,0,0,0,0,0,0)) 86400.0 oneday = time.mktime((0,0,1,0,0,0,0,0,0)) - time.mktime((0,0,0,0,0,0,0,0,0))
time.ctime(time.mktime(x)+oneday) '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?
time.mktime((0,0,0,0,0,0,0,0,0))

943948800.0

Regards,
Bengt Richter
Jul 18 '05 #3
In article <Qc********************@powergate.ca>,
Peter Hansen <pe***@engcorp.com> wrote:
David Stockwell wrote:
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...
>>> aDate = '19920228'
>>> x = time.strptime(aDate, '%Y%m%d')
>>> print x (1992, 2, 28, 0, 0, 0, 4, 59, -1) >>> d = datetime.datetime.fromtimestamp(time.mktime(x))
>>> d datetime.datetime(1992, 2, 28, 0, 0) >>> y = d + datetime.timedelta(days=1)
>>> y.ctime() '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.
import datetime Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named datetime


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, do**@u.washington.edu
Jul 18 '05 #4
Donn Cave wrote:
$ 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.
import datetime


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


The datetime module is new in Python 2.3.
--
Michael Hoffman
Jul 18 '05 #5
In article <do************************@gnus01.u.washington.ed u>,
Donn Cave <do**@u.washington.edu> wrote:
In article <Qc********************@powergate.ca>,
Peter Hansen <pe***@engcorp.com> wrote:
David Stockwell wrote:

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Benoit BESSE | last post by:
Hi, I try to write a fonction which take a date and time and convert it into a NTP time. I have to use mktime but I did not work at all. Here is my code dans the exécution. Please help. Thanks...
8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
10
by: Maksim Kasimov | last post by:
hi all, sorry if i'm reposting why time.strptime and time.localtime returns tuple with different DST (9 item of the tuple)? is there some of setting to fix the problem? Python 2.2.3 (#1, May...
2
by: Sergey | last post by:
There is function mktime() -- convert local time tuple to seconds since Epoch in module time. But how about to convert *GMT time tuple* to seconds since Epoch? Is there such function?
14
by: Dave Rahardja | last post by:
Is there a way to generate a series of statements based on the data members of a structure at compile time? I have a function that reverses the endianness of any data structure: /// Reverse...
2
by: Alan Isaac | last post by:
I am probably confused about immutable types. But for now my questions boil down to these two: - what does ``tuple.__init__`` do? - what is the signature of ``tuple.__init__``? These...
13
by: HMS Surprise | last post by:
I need to convert the string below into epoch seconds so that I can perform substractions and additions. I assume I will need to break it up into a time_t struct and use mktime. Two questions if...
4
by: Alex Vinokur | last post by:
Here is some tuple (triple in this case) with uniform types (for instance, double): boost::tuple<double, double, doublet; Is there any way (in boost::tuple) to define such tuples something like...
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.