473,804 Members | 3,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subtract dates with time module

I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date. I'm a beginner
so any help would be appreciated. Here is the code:

def OBweeks(ptID):
qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' %
(ptID)
results = EMR_utilities.g etAllData(qry)
for items in results:
r = re.search('\d\d \d\d-\d\d-\d\d', items)
if r:
d = time.strptime(r .group(), "%Y-%m-%d') -
time.localtime( )
weeks, days = divmod(d.days, 7)
return '%s %s/7 weeks' % (weeks, days)

This isn't working. I'm getting "unsupporte d operand type for -:
'time.struct_ti me' and 'time.struct_ti me" error.

Thanks, Mike
Mar 26 '08 #1
2 5433
barronmo wrote:
I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date. I'm a beginner
so any help would be appreciated. Here is the code:
Use strptime to create time-tuples, and use the fields in there to create
datetime-objects. Then do the math with them.

Diez
Mar 27 '08 #2
barronmo wrote:
I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date.
datetime.dateti me.strptime was introduced in Python 2.5; what version
are you using?

If you really want to get to datetime, here's a quick bridge:
>>import time, datetime
def mystrptime(astr , format):
.... return datetime.dateti me(*time.strpti me(astr, format)[:6])
....
>>mystrptime('2 008-03-31', '%Y-%m-%d')
datetime.dateti me(2008, 3, 31, 0, 0)

I'm a beginner
so any help would be appreciated. Here is the code:

def OBweeks(ptID):
qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' %
(ptID)
results = EMR_utilities.g etAllData(qry)
for items in results:
r = re.search('\d\d \d\d-\d\d-\d\d', items)
if r:
d = time.strptime(r .group(), "%Y-%m-%d') -
You have " at the start and ' at the end of what's supposed to be a
string constant. That's a syntax error; it won't run. Please don't serve
up what you thought you might have run -- use copy/paste.

In this particular case, you can just use time.mktime to convert a time
tuple into days since the epoch.

# untested
days = time.mktime(tim e.strptime(r.gr oup(), "%Y-%m-%d")) -
int(time.mktime (time.localtime ()))
weeks, days = divmod(days, 7)
time.localtime( )
weeks, days = divmod(d.days, 7)
return '%s %s/7 weeks' % (weeks, days)

This isn't working. I'm getting "unsupporte d operand type for -:
'time.struct_ti me' and 'time.struct_ti me" error.
It *is* working. That is the correct result of the code that you
executed. There is is nothing in the time docs to suggest that
attempting to subtract time tuples produces anything useful.

HTH,
John
Mar 27 '08 #3

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

Similar topics

3
11085
by: Ken | last post by:
$expirationdate = 4/15/2005 $startdate = 3/1/2005 I would like to determine the number of days between the above dates. My approach: $startdate = strtotime($startdate); // change to a string $expirationdate = strtotime($expirationdate); $delta = $expirationdate - $startdate; // $delta = 1110175200
11
1912
by: Amy G | last post by:
I have seen something about this beofore on this forum, but my google search didn't come up with the answer I am looking for. I have a list of tuples. Each tuple is in the following format: ("data", "moredata", "evenmoredata", "date string") The date string is my concern. This is the date stamp from an email. The problem is that I have a whole bunch of variations when it comes to the format that the date string is in. For example...
8
25391
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month" size=3> Month: <input name="m1" alt="Month" size=3> Year: <input name="y1" alt="Year" size=5> (4 digits for year, e.g.
5
6520
by: cvisal | last post by:
Hi all Im working on productivity calculations (Time calculations) and need some help in coding. Database Tool:MS-Access 2003. The general operator punch-in time is 5:30 AM and the punch-out time is 2:00PM. The Break times are 1) 9:30 AM to 9:45 AM 2) 11:00AM to 11:30 AM 3) 12:30PM to 12:45 PM
4
16716
by: meltedown | last post by:
I can't see what I'm doing wrong. I'm subtracting 60*60*24 from a unix time stamp and the result is 23 hours earlier, not 24. Start with a unix time stamp: $unixtime=1144018006; convert it to a date: $date=getdate($unixtime); date Array (
8
12829
by: Remington | last post by:
I am using windows 2000pro with access 2000. I am trying to make a database for our HR department, that would allow our HR Director to type in an employee's ID number into a form and then select the dates the employee took off from work. (I have the calander add-in 8.0 setup to easily select the dates already) There are two Date Fields, "Leave Date", and "Return Date" where the calander selections are stored. They are in the 12/25/2006...
10
12453
by: dan | last post by:
Am i breaking any rules when I loop dates like // Determine Memorial Day intFlag = 0; memDayHol = new Date (currentYear, 4, 31); while (intFlag == 0) { if (memDayHol.getDay() == 1) {intFlag =1;} else {memDayHol = memDayHol - 1;} }
6
4857
by: lptl | last post by:
I know the title is misleading. I am working on a class project where we are trying to setup a notification system written in PHP and using an MySQL database. We are trying to set-up a notification page that will show what users are nearing expiration. The expiration dates are entered in manually and are not a timestamp. The dates are entered in on another page into the SQL database. Then what we need is to be able to take those dates and find...
6
3540
by: W. eWatson | last post by:
That's the question in Subject. For example, the difference between 08/29/2008 and 09/03/2008 is +5. The difference between 02/28/2008 and 03/03/2008 is 4, leap year--extra day in Feb. I'm really only interested in years between, say, 1990 and 2050. In other words not some really strange period of time well outside our current era of history. -- W. eWatson (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
0
9706
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
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10321
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9152
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
7620
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
6853
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
5522
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...
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.