Velocity Reviews > Date Comparison and Manipulation Functions?

# Date Comparison and Manipulation Functions?

W. eWatson
Guest
Posts: n/a

 08-24-2008
Are there some date and time comparison functions that would compare, say,

Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd)
Is 02/11/07 the same as 02/11/07?

Is 14:05:18 after 22:02:51? (24 hour day is fine)

How about the date after 02/28/04 is 02/29/04, or the date after 09/30/08 is
10/01/08?

How about is 03/03/04 20:10:08 after 03/07/03 14:00:00? Probably the others
above will suffice.
--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

Paul Rudin
Guest
Posts: n/a

 08-24-2008
"W. eWatson" <(E-Mail Removed)> writes:

> Are there some date and time comparison functions that would compare, say,
>
> Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd)
> Is 02/11/07 the same as 02/11/07?
>
> Is 14:05:18 after 22:02:51? (24 hour day is fine)
>
> How about the date after 02/28/04 is 02/29/04, or the date after
> 09/30/08 is 10/01/08?
>
> How about is 03/03/04 20:10:08 after 03/07/03 14:00:00? Probably the
> others above will suffice.

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

zuul@ferg.org
Guest
Posts: n/a

 08-25-2008
check out Pyfdate: http://www.ferg.org/pyfdate

from pyfdate import *

print "It is now", t.wdt

datestring1 = "2005/10/05" #year,month,day
datestring2 = "2002/09/22" #year,month,day
datestring3 = "2007/11/11" #year,month,day

year,month,day = numsplit(datestring1) # split into integers
t1 = Time(year,month,day)
for datestring in (datestring2,datestring1,datestring3):
year,month,day = numsplit(datestring)
t2 = Time(year,month,day)

if t1 > t2:
print t1.isodate, "is later than ", t2.isodate
elif t1 == t2:
print t1.isodate, "is the same as ", t2.isodate
elif t1 < t2:
print t1.isodate, "is earlier than", t2.isodate

print

t1 = Time(2000,2,2
print "The date after", t1.d, "is", t1.plus(day=1).d
t1 = Time(2001,2,2
print "The date after", t1.d, "is", t1.plus(day=1).d
t1 = Time(2004,2,2
print "The date after", t1.d, "is", t1.plus(day=1).d

print
datestring1 = "2005/10/05 20:10:08"
datestring2 = "2005/10/05 20:10:06"
datestring3 = "2005/10/05 20:10:09"

t1 = Time(*numsplit(datestring1))
for datestring in (datestring2,datestring1,datestring3):
t2 = Time(*numsplit(datestring))

if t1 > t2:
print t1.d, t1.civiltime2, "is later than ", t2.d, t2.civiltime2
elif t1 == t2:
print t1.d, t1.civiltime2, "is the same as ", t2.d, t2.civiltime2
elif t1 < t2:
print t1.d, t1.civiltime2, "is earlier than", t2.d, t2.civiltime2

W. eWatson
Guest
Posts: n/a

 08-26-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> check out Pyfdate: http://www.ferg.org/pyfdate
>
> from pyfdate import *
>
> t = Time().add(hours=14)
> print "It is now", t.wdt
>
> datestring1 = "2005/10/05" #year,month,day
> datestring2 = "2002/09/22" #year,month,day
> datestring3 = "2007/11/11" #year,month,day
>
> year,month,day = numsplit(datestring1) # split into integers
> t1 = Time(year,month,day)
> for datestring in (datestring2,datestring1,datestring3):
> year,month,day = numsplit(datestring)
> t2 = Time(year,month,day)
>
> if t1 > t2:
> print t1.isodate, "is later than ", t2.isodate
> elif t1 == t2:
> print t1.isodate, "is the same as ", t2.isodate
> elif t1 < t2:
> print t1.isodate, "is earlier than", t2.isodate
>
> print
>
> t1 = Time(2000,2,2
> print "The date after", t1.d, "is", t1.plus(day=1).d
> t1 = Time(2001,2,2
> print "The date after", t1.d, "is", t1.plus(day=1).d
> t1 = Time(2004,2,2
> print "The date after", t1.d, "is", t1.plus(day=1).d
>
> print
> datestring1 = "2005/10/05 20:10:08"
> datestring2 = "2005/10/05 20:10:06"
> datestring3 = "2005/10/05 20:10:09"
>
> t1 = Time(*numsplit(datestring1))
> for datestring in (datestring2,datestring1,datestring3):
> t2 = Time(*numsplit(datestring))
>
> if t1 > t2:
> print t1.d, t1.civiltime2, "is later than ", t2.d, t2.civiltime2
> elif t1 == t2:
> print t1.d, t1.civiltime2, "is the same as ", t2.d, t2.civiltime2
> elif t1 < t2:
> print t1.d, t1.civiltime2, "is earlier than", t2.d, t2.civiltime2

It looks good. Thanks.

--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

W. eWatson
Guest
Posts: n/a

 08-27-2008
(E-Mail Removed) wrote:
> check out Pyfdate: http://www.ferg.org/pyfdate
>
> from pyfdate import *
>
> t = Time().add(hours=14)
> print "It is now", t.wdt
>
> datestring1 = "2005/10/05" #year,month,day
> datestring2 = "2002/09/22" #year,month,day
> datestring3 = "2007/11/11" #year,month,day
>
> year,month,day = numsplit(datestring1) # split into integers
> t1 = Time(year,month,day)
> for datestring in (datestring2,datestring1,datestring3):
> year,month,day = numsplit(datestring)
> t2 = Time(year,month,day)
>
> if t1 > t2:
> print t1.isodate, "is later than ", t2.isodate
> elif t1 == t2:
> print t1.isodate, "is the same as ", t2.isodate
> elif t1 < t2:
> print t1.isodate, "is earlier than", t2.isodate
>
> print
>
> t1 = Time(2000,2,2
> print "The date after", t1.d, "is", t1.plus(day=1).d
> t1 = Time(2001,2,2
> print "The date after", t1.d, "is", t1.plus(day=1).d
> t1 = Time(2004,2,2
> print "The date after", t1.d, "is", t1.plus(day=1).d
>
> print
> datestring1 = "2005/10/05 20:10:08"
> datestring2 = "2005/10/05 20:10:06"
> datestring3 = "2005/10/05 20:10:09"
>
> t1 = Time(*numsplit(datestring1))
> for datestring in (datestring2,datestring1,datestring3):
> t2 = Time(*numsplit(datestring))
>
> if t1 > t2:
> print t1.d, t1.civiltime2, "is later than ", t2.d, t2.civiltime2
> elif t1 == t2:
> print t1.d, t1.civiltime2, "is the same as ", t2.d, t2.civiltime2
> elif t1 < t2:
> print t1.d, t1.civiltime2, "is earlier than", t2.d, t2.civiltime2

I'm using IDLE for Python 2.4, and put pfydate distribution in
C:\Python24\Lib\site-packages\pfydate, as required by the
How to install pyfdate.

Save pyfdate.py into your PythonNN/Lib/site-packages directory.
I copied it into C:\Python24\Lib\site-packages\pfydate

Execution in IDLE produced:
---------------------------------
Traceback (most recent call last):
File
"C:\Sandia_Meteors\Improved_Sentinel\Sentinel_Play ground\date_example.py",
line 1, in ?
from pyfdate import *
ImportError: No module named pyfdate
---------------------------------
Looking in the Path Browser, I don't see pyfdate. I see PIL package and
scipy package.

--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

John Machin
Guest
Posts: n/a

 08-27-2008
On Aug 27, 10:21 am, "W. eWatson" <(E-Mail Removed)> wrote:
>
> I'm using IDLE for Python 2.4, and put pfydate distribution in
> C:\Python24\Lib\site-packages\pfydate, as required by the
> How to install pyfdate.
>
> Save pyfdate.py into your PythonNN/Lib/site-packages directory.
> I copied it into C:\Python24\Lib\site-packages\pfydate

If that means that you ended up with
C:\Python24\Lib\site-packages\pfydate\pyfdate.py
then you have *not* followed the instructions "Save pyfdate.py into
You need to end up with
C:\Python24\Lib\site-packages\pyfdate.py

If in doubt, get to a command prompt and type
dir C:\Python24\Lib\site-packages\pfydate*
and tell us what you see.

>
> Execution in IDLE produced:
> ---------------------------------
> Traceback (most recent call last):
> File
> "C:\Sandia_Meteors\Improved_Sentinel\Sentinel_Play ground\date_example.py",
> line 1, in ?
> from pyfdate import *
> ImportError: No module named pyfdate
> ---------------------------------
> Looking in the Path Browser, I don't see pyfdate. I see PIL package and
> scipy package.

W. eWatson
Guest
Posts: n/a

 08-27-2008
John Machin wrote:
> On Aug 27, 10:21 am, "W. eWatson" <(E-Mail Removed)> wrote:
>> I'm using IDLE for Python 2.4, and put pfydate distribution in
>> C:\Python24\Lib\site-packages\pfydate, as required by the
>> How to install pyfdate.
>>
>> Save pyfdate.py into your PythonNN/Lib/site-packages directory.
>> I copied it into C:\Python24\Lib\site-packages\pfydate

>
> If that means that you ended up with
> C:\Python24\Lib\site-packages\pfydate\pyfdate.py
> then you have *not* followed the instructions "Save pyfdate.py into
> your PythonNN/Lib/site-packages directory".
> You need to end up with
> C:\Python24\Lib\site-packages\pyfdate.py

None of the folders in C:\Python24\Lib\site-packages\ have py as a suffix
(as seen either by the IDLE path browser or XP). My folder is exactly
C:\Python24\Lib\site-packages\pfydate in XP and it contains about 12 py files.
There are exactly three folders under
C:\Python24\Lib\site-packages\ according to the IDLE path browser. This does
not agree with XP, which has:
Numeric
pfydate
scipy
numpy
PIL
>
> If in doubt, get to a command prompt and type
> dir C:\Python24\Lib\site-packages\pfydate*
> and tell us what you see.
>
>> Execution in IDLE produced:
>> ---------------------------------
>> Traceback (most recent call last):
>> File
>> "C:\Sandia_Meteors\Improved_Sentinel\Sentinel_Play ground\date_example.py",
>> line 1, in ?
>> from pyfdate import *
>> ImportError: No module named pyfdate
>> ---------------------------------
>> Looking in the Path Browser, I don't see pyfdate. I see PIL package and
>> scipy package.

--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

John Machin
Guest
Posts: n/a

 08-27-2008
On Aug 27, 11:24*am, "W. eWatson" <(E-Mail Removed)> wrote:
> John Machin wrote:
> > On Aug 27, 10:21 am, "W. eWatson" <(E-Mail Removed)> wrote:
> >> I'm using IDLE for Python 2.4, and put pfydate distribution in
> >> C:\Python24\Lib\site-packages\pfydate, as required by the
> >> How to install pyfdate.

>
> >> * * *Save pyfdate.py into your PythonNN/Lib/site-packages directory.
> >> I copied it into C:\Python24\Lib\site-packages\pfydate

>
> > If that means that you ended up with
> > * *C:\Python24\Lib\site-packages\pfydate\pyfdate.py
> > then you have *not* followed the instructions "Save pyfdate.py into
> > your PythonNN/Lib/site-packages directory".
> > You need to end up with
> > * *C:\Python24\Lib\site-packages\pyfdate.py

>
> None of the folders in C:\Python24\Lib\site-packages\ have py as a suffix
> (as seen either by the IDLE path browser or XP). My folder is exactly
> C:\Python24\Lib\site-packages\pfydate in XP and it contains about 12 py files.
> There are exactly three folders under
> C:\Python24\Lib\site-packages\ according to the IDLE path browser. This does
> not agree with XP, which has:
> Numeric
> pfydate
> scipy
> numpy
> PIL

(1) "pfydate" != "pyfdate"
(2) The instructions say to put pyfdate.py [that's *ONE* file, not 12
files] in the ..../site-packages folder, *not* a sub-folder

W. eWatson
Guest
Posts: n/a

 08-27-2008
John Machin wrote:
> On Aug 27, 11:24 am, "W. eWatson" <(E-Mail Removed)> wrote:
>> John Machin wrote:
>>> On Aug 27, 10:21 am, "W. eWatson" <(E-Mail Removed)> wrote:
>>>> I'm using IDLE for Python 2.4, and put pfydate distribution in
>>>> C:\Python24\Lib\site-packages\pfydate, as required by the
>>>> How to install pyfdate.
>>>> Save pyfdate.py into your PythonNN/Lib/site-packages directory.
>>>> I copied it into C:\Python24\Lib\site-packages\pfydate
>>> If that means that you ended up with
>>> C:\Python24\Lib\site-packages\pfydate\pyfdate.py
>>> then you have *not* followed the instructions "Save pyfdate.py into
>>> your PythonNN/Lib/site-packages directory".
>>> You need to end up with
>>> C:\Python24\Lib\site-packages\pyfdate.py

>> None of the folders in C:\Python24\Lib\site-packages\ have py as a suffix
>> (as seen either by the IDLE path browser or XP). My folder is exactly
>> C:\Python24\Lib\site-packages\pfydate in XP and it contains about 12 py files.
>> There are exactly three folders under
>> C:\Python24\Lib\site-packages\ according to the IDLE path browser. This does
>> not agree with XP, which has:
>> Numeric
>> pfydate
>> scipy
>> numpy
>> PIL

>
> (1) "pfydate" != "pyfdate"

typo
> (2) The instructions say to put pyfdate.py [that's *ONE* file, not 12
> files] in the ..../site-packages folder, *not* a sub-folder
>

Got it. Ah, I see upon closer inspection the other files are just
international versions. Thanks. It works.

--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>

W. eWatson
Guest
Posts: n/a

 08-29-2008
I just tried the following code, and got an unexpected result.

from pyfdate import *
t = Time()

ts = Time(2008, 8, 29,15,20,7)
tnew = ts.plus(months=6)
print "new date: ", tnew

Result:
new date: 2009-02-28 15:20:07

I believe that should be April 1, 2009. If I use months = 1 and day =31, I
get Sept. 30, 2008 and not Oct. 1, 2008. Is there a way to get around this?

--
W. Watson
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet