473,783 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(a Date,"%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 3651
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(a Date,"%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(a Date, '%Y%m%d')
print x (1992, 2, 28, 0, 0, 0, 4, 59, -1) d = datetime.dateti me.fromtimestam p(time.mktime(x ))
d datetime.dateti me(1992, 2, 28, 0, 0) y = d + datetime.timede lta(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*******@hotm ail.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(a Date,"%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.333333333 334

import time
aDate = '19920228'
x = time.strptime(a Date,"%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)+oned ay) '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************ ********@powerg ate.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(a Date,"%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(a Date, '%Y%m%d')
>>> print x (1992, 2, 28, 0, 0, 0, 4, 59, -1) >>> d = datetime.dateti me.fromtimestam p(time.mktime(x ))
>>> d datetime.dateti me(1992, 2, 28, 0, 0) >>> y = d + datetime.timede lta(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.washingt on.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************ ************@gn us01.u.washingt on.edu>,
Donn Cave <do**@u.washing ton.edu> wrote:
In article <Qc************ ********@powerg ate.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
4682
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 def ToNTPTime(d,h): "Retuen a NTP time" year,month,day=d.split("/")
8
2800
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 _build_used(): .... y = x + 1 .... return x, y ....
10
2328
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 31 2005, 11:33:52) ] on freebsd4 Type "help", "copyright", "credits" or "license" for more information. >>> import time
2
1271
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
4211
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 the endianness of a data structure "in place". template <typename T> void reverseEndian(T&); Using boost, it is possible to provide the default implementation for all POD
2
3486
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 questions are stimulated by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303439 Looking at that, what fails if I leave out the following line? ::
13
2095
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 you will please: Is there a way to use multiple separator characters for split similar to awk's style? Could you point to an example of a python time_t struct?
4
2432
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 uniform_tuple <3, doublet; ? Alex Vinokur
0
1793
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 reasonable since generators require iteration and stop iteration and what not.
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.