473,395 Members | 1,457 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,395 software developers and data experts.

Python date

Hi,

I am using zope with an SQL database in the background. I have a date stored in the database as "2007/08/20 09:27:16.593 GMT+1".

Is there anyway in python to add an int of "60" to the date?
Nov 9 '07 #1
3 1634
bartonc
6,596 Expert 4TB
To add "60" days to a string date takes some machinations:
Expand|Select|Wrap|Line Numbers
  1. >>> import time
  2. >>> import datetime as dt
  3. >>> theDate, theTime, theZone = "2007/08/20 09:27:16.593 GMT+1".split()
  4. >>> timeObj = time.strptime(" ".join([theDate, theTime.split('.')[0]]), "%Y/%m/%d %H:%M:%S")
  5. >>> toAdd = dt.timedelta(days = 60)
  6. >>> timeObj
  7. (2007, 8, 20, 9, 27, 16, 0, 232, -1)
  8. >>> dtObj = dt.datetime(*timeObj[:6])
  9. >>> dtObj
  10. datetime.datetime(2007, 8, 20, 9, 27, 16)
  11. >>> dtObj + toAdd
  12. datetime.datetime(2007, 10, 19, 9, 27, 16)
  13. >>> 
Nov 10 '07 #2
bartonc
6,596 Expert 4TB
Of course, if your DB is MySQL you could call ADDDATE():
Expand|Select|Wrap|Line Numbers
  1. SELECT ADDTIME('1997-12-31 23:59:59.999999', '1 1:1:1.000002');
-> '1998-01-02 01:01:01.000001'
Nov 10 '07 #3
bartonc
6,596 Expert 4TB
Hi,

I am using zope with an SQL database in the background. I have a date stored in the database as "2007/08/20 09:27:16.593 GMT+1".

Is there anyway in python to add an int of "60" to the date?
Here's a slightly more complete example:
Expand|Select|Wrap|Line Numbers
  1. import time
  2. import datetime as dt
  3. theDate, theTime, theZone = "2007/08/20 09:27:16.593 GMT+1".split()
  4. noDec = theTime.split('.')[0]
  5. timeObj = time.strptime(" ".join([theDate, noDec]), "%Y/%m/%d %H:%M:%S")
  6. print timeObj
  7. toAdd = dt.timedelta(days = 60)
  8. dtObj = dt.datetime(*timeObj[:6])
  9. print repr(dtObj)
  10. strToInsert = " ".join([str(dtObj + toAdd), theZone])
  11. print strToInsert
  12.  
  13. # output
  14. (2007, 8, 20, 9, 27, 16, 0, 232, -1)
  15. datetime.datetime(2007, 8, 20, 9, 27, 16)
  16. 2007-10-19 09:27:16 GMT+1
Nov 11 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
1
by: David Mertz, Ph.D. | last post by:
I decided to write a pure Python hashcash implementation. I have seen David McNab's Python implementation. Unfortunately, as near as I can tell (which is supported on the hashcash mailing list...
11
by: Magnus Lycka | last post by:
While the official Python Tutorial has served its purpose well, keeping it up to date is hardly anyones top priority, and there are others who passionately create really good Python tutorials on...
7
by: fyleow | last post by:
Hi guys, I'm a student/hobbyist programmer interested in creating a web project. It's nothing too complicated, I would like to get data from an RSS feed and store that into a database. I want...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32) Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71) RFE : 223 open...
20
by: Shawn Milo | last post by:
I'm new to Python and fairly experienced in Perl, although that experience is limited to the things I use daily. I wrote the same script in both Perl and Python, and the output is identical. The...
24
by: Joe Salmeri | last post by:
I just upgraded from Python 2.4.2 to Python 2.5.1 and have found some unexpected behavior that appears to be a bug in the os.stat module. My OS is Windows XP SP2 + all updates. I have several...
3
by: brad | last post by:
I have some c++ binaries that do rather intense number computations. They do it well and rather quickly compared to other languages (not just Python). An example: In this case, c++ does one...
3
by: Phillip B Oldham | last post by:
Hi. I'm stretching my boundaries in programming with a little python shell-script which is going to loop through a list of domain names, grab the whois record, parse it, and put the results into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.