confused on calculating date difference in days.

2007-10-16 Thread krishnakant Mane
hello,
I am strangely confused with a date calculation problem.
the point is that I want to calculate difference in two dates in days.
there are two aspects to this problem.
firstly, I can't get a way to convert a string like 1/2/2005 in a
genuan date object which is needed for calculation.
now once this is done I will create a another date object with
today = datetime.datetime.now()
and then see the difference between this today and the string that I
converted to date.
now in the first place I can't recall how I can convert a string to a date.
then now I don't know how to calculate difference in days between
today and the string converted date.
any help will be appreciated.
regards,
Krishnakant.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confused on calculating date difference in days.

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:

 firstly, I can't get a way to convert a string like 1/2/2005 in a
 genuan date object which is needed for calculation.

Why?  Split the string up, convert the parts to `int` and just create a
`datetime.date` object.

 now once this is done I will create a another date object with
 today = datetime.datetime.now()
 and then see the difference between this today and the string that I
 converted to date.
 now in the first place I can't recall how I can convert a string to a date.
 then now I don't know how to calculate difference in days between
 today and the string converted date.

In [421]: '1/2/2005'.split('/')
Out[421]: ['1', '2', '2005']

In [422]: map(int, '1/2/2005'.split('/'))
Out[422]: [1, 2, 2005]

In [423]: month, day, year = map(int, '1/2/2005'.split('/'))

In [424]: a = datetime.date(year, month, day)

In [425]: b = datetime.date.today() - a

In [426]: b.days
Out[426]: 1017

Maybe you should read the docs next time.  ;-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confused on calculating date difference in days.

2007-10-16 Thread Ben Finney
krishnakant Mane [EMAIL PROTECTED] writes:

 firstly, I can't get a way to convert a string like 1/2/2005 in a
 genuan date object which is needed for calculation.

Until recently, this was a wart in the Python standard library:
datetime objects exist in the 'datetime' module, but parsing strings
to get date/time components was only available in the 'time' module.

In Python 2.5, though, we have 'datetime.datetime.strptime'
URL:http://docs.python.org/lib/datetime-datetime.html#l2h-634, which
parses a string according to a specified format, and returns the
corresponding datetime value.

 import datetime
 datetime.datetime.strptime(2007-10-16t17:40:00, %Y-%m-%dt%H:%M:%S)
datetime.datetime(2007, 10, 16, 17, 40)

 now once this is done I will create a another date object with
 today = datetime.datetime.now()
 and then see the difference between this today and the string that I
 converted to date.

That's simple: datetime objects support difference via the subtraction
arithmetic operator, returning a datetime.timedelta instance
URL:http://docs.python.org/lib/datetime-timedelta.html.

 value = datetime.datetime(2007, 10, 16, 15, 30, 45)
 value - datetime.datetime(2007, 10, 16, 15, 20, 00)
datetime.timedelta(0, 645)
 value - datetime.datetime(2007, 10, 12, 8, 25, 19)
datetime.timedelta(4, 25526)
 value - datetime.datetime(2007, 11, 26, 0, 0, 0)
datetime.timedelta(-41, 55845)

-- 
 \ Experience is that marvelous thing that enables you to |
  `\  recognize a mistake when you make it again.  -- Franklin P. |
_o__)Jones |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confused on calculating date difference in days.

2007-10-16 Thread Ben Finney
Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes:

 On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:
 
  firstly, I can't get a way to convert a string like 1/2/2005 in
  a genuan date object which is needed for calculation.
 
 Why?  Split the string up, convert the parts to `int` and just
 create a `datetime.date` object.

What, re-implement 'strptime' in every program that needs it? And then
debug the result every time?

Even if one doesn't have Python 2.5 or above, surely getting the
string parsed into int values by the standard 'time.strptime' is
better than re-implementing it every time.

 Maybe you should read the docs next time.  ;-)

Back at you.

-- 
 \   If you do not trust the source do not use this program. |
  `\—Microsoft Vista security dialogue |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: confused on calculating date difference in days.

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 18:10:54 +1000, Ben Finney wrote:

 Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes:
 
 On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:
 
  firstly, I can't get a way to convert a string like 1/2/2005 in
  a genuan date object which is needed for calculation.
 
 Why?  Split the string up, convert the parts to `int` and just
 create a `datetime.date` object.
 
 What, re-implement 'strptime' in every program that needs it? And then
 debug the result every time?

Yes.  Seems easier to me.  :-)

 Even if one doesn't have Python 2.5 or above, surely getting the
 string parsed into int values by the standard 'time.strptime' is
 better than re-implementing it every time.
 
 Maybe you should read the docs next time.  ;-)
 
 Back at you.

Got me.  I didn't know that `datetime` has a `strptime` now.  I just
looked at `date`.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confused on calculating date difference in days.

2007-10-16 Thread krishnakant Mane
hello,
thanks all of you for providing valuable help.
right now I am confused about the delta object.
how can I extract the difference between two dates in terms of day
using the delta object?
I tried reading the python docs but did not understand the concept of
delta object and how can I measure the difference in terms of days
between two dates.
I expect that the days would be integers.
secondly the format of my date is actually 16/10/2007, and this is
all in varchar field inside a postgresql database.
I understand that datetime.datetime.strptime would convert this string
16/10/2007 into a date object which I can then compare with the
current date created by datetime.now().
is that right?
if yes then please explain me how I can get the delta object to give
me results in days.
regards,
Krishnakant.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confused on calculating date difference in days.

2007-10-16 Thread Diez B. Roggisch
krishnakant Mane wrote:

 hello,
 thanks all of you for providing valuable help.
 right now I am confused about the delta object.
 how can I extract the difference between two dates in terms of day
 using the delta object?
 I tried reading the python docs but did not understand the concept of
 delta object and how can I measure the difference in terms of days
 between two dates.
 I expect that the days would be integers.
 secondly the format of my date is actually 16/10/2007, and this is
 all in varchar field inside a postgresql database.
 I understand that datetime.datetime.strptime would convert this string
 16/10/2007 into a date object which I can then compare with the
 current date created by datetime.now().
 is that right?
 if yes then please explain me how I can get the delta object to give
 me results in days.

The documentation is very clear about this:

http://docs.python.org/lib/datetime-timedelta.html


Instance attributes (read-only):

days  Between -9 and 9 inclusive
seconds  Between 0 and 86399 inclusive
microseconds  Between 0 and 99 inclusive


So (date_a - date_b).days will give you what you need.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confused on calculating date difference in days.

2007-10-16 Thread Shane Geiger
# Example

import datetime
def days_old(birth_year=1974,birth_month=12,birth_day=7):
return (datetime.date.today() -
datetime.date(birth_year,birth_month,birth_day) ).days

 days_old()
12000



krishnakant Mane wrote:
 hello,
 thanks all of you for providing valuable help.
 right now I am confused about the delta object.
 how can I extract the difference between two dates in terms of day
 using the delta object?
 I tried reading the python docs but did not understand the concept of
 delta object and how can I measure the difference in terms of days
 between two dates.
 I expect that the days would be integers.
 secondly the format of my date is actually 16/10/2007, and this is
 all in varchar field inside a postgresql database.
 I understand that datetime.datetime.strptime would convert this string
 16/10/2007 into a date object which I can then compare with the
 current date created by datetime.now().
 is that right?
 if yes then please explain me how I can get the delta object to give
 me results in days.
 regards,
 Krishnakant.
   

-- 
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard

-- 
http://mail.python.org/mailman/listinfo/python-list