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?
3 1634
To add "60" days to a string date takes some machinations: -
>>> import time
-
>>> import datetime as dt
-
>>> theDate, theTime, theZone = "2007/08/20 09:27:16.593 GMT+1".split()
-
>>> timeObj = time.strptime(" ".join([theDate, theTime.split('.')[0]]), "%Y/%m/%d %H:%M:%S")
-
>>> toAdd = dt.timedelta(days = 60)
-
>>> timeObj
-
(2007, 8, 20, 9, 27, 16, 0, 232, -1)
-
>>> dtObj = dt.datetime(*timeObj[:6])
-
>>> dtObj
-
datetime.datetime(2007, 8, 20, 9, 27, 16)
-
>>> dtObj + toAdd
-
datetime.datetime(2007, 10, 19, 9, 27, 16)
-
>>>
Of course, if your DB is MySQL you could call ADDDATE(): - SELECT ADDTIME('1997-12-31 23:59:59.999999', '1 1:1:1.000002');
-> '1998-01-02 01:01:01.000001'
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: -
import time
-
import datetime as dt
-
theDate, theTime, theZone = "2007/08/20 09:27:16.593 GMT+1".split()
-
noDec = theTime.split('.')[0]
-
timeObj = time.strptime(" ".join([theDate, noDec]), "%Y/%m/%d %H:%M:%S")
-
print timeObj
-
toAdd = dt.timedelta(days = 60)
-
dtObj = dt.datetime(*timeObj[:6])
-
print repr(dtObj)
-
strToInsert = " ".join([str(dtObj + toAdd), theZone])
-
print strToInsert
-
-
# output
-
(2007, 8, 20, 9, 27, 16, 0, 232, -1)
-
datetime.datetime(2007, 8, 20, 9, 27, 16)
-
2007-10-19 09:27:16 GMT+1
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |