Date Comparison and Manipulation Functions?

W

W. eWatson

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/>
 
P

Paul Rudin

W. eWatson said:
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
 
Z

zuul

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,28)
print "The date after", t1.d, "is", t1.plus(day=1).d
t1 = Time(2001,2,28)
print "The date after", t1.d, "is", t1.plus(day=1).d
t1 = Time(2004,2,28)
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

W. eWatson

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,28)
print "The date after", t1.d, "is", t1.plus(day=1).d
t1 = Time(2001,2,28)
print "The date after", t1.d, "is", t1.plus(day=1).d
t1 = Time(2004,2,28)
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

W. eWatson

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,28)
print "The date after", t1.d, "is", t1.plus(day=1).d
t1 = Time(2001,2,28)
print "The date after", t1.d, "is", t1.plus(day=1).d
t1 = Time(2004,2,28)
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
<ttp://www.ferg.org/pyfdate/download.html> page.
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_Playground\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.

Comments?

--
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/>
 
J

John Machin

I'm using IDLE for Python 2.4, and put pfydate distribution in
C:\Python24\Lib\site-packages\pfydate, as required by the
<ttp://www.ferg.org/pyfdate/download.html> page.
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

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

W. eWatson

John said:
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.


--
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/>
 
J

John Machin

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

W. eWatson

John said:
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

W. eWatson

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?
 
N

norseman

W. eWatson said:
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?

===================
Hummmm....


The old 'ditty' to help remember:
Thirty days have September, April, June and November
All the rest have 31 except February
Which has but 28 'til Leap Year gives it 29

and:
2008 08 29 DOY = 242 366 in year
2009 03 01 DOY = 60


366-242 = 124 + 60 = 184 - 1 (we don't count today) = 183
30 + 31 + 30 + 31 + 31 + 28 + 2 (left in aug.) = 183
S O N D J F A

366+60-242-1(for today)=183

Puts us on 2009 Feb. 28

case #2: I think something is described other than as used.

08 29
01 31
-----
09 60

2008 09 01 DOY = 245
245 + 60 -1(today) = 304 which is DOY for 2008 10 30

or (depending on what the written meant)

08 31
01 00
-----
09 31

2008 09 00 DOY = 245
245 + 31 -1(today) = 274 = 2008 Sept. 30

pyfdate is using actual days per month and DOY to compute.
It's not quite the same as the 'rule of thumb' that is in general public
use - but it is accurate. Most of us would just add the months and use
the day or one close to it.




Steve
(e-mail address removed)
 
J

John Machin

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.

Presuming that we are talking about the Gregorian calendar, and not
one of your own invention, you are (one trusts) alone in that belief.
There are SEVEN whole months and a bit between August 29, 2008 and
April 1, 2009. Count the months: Sep, Oct, Nov, Dec, Jan, Feb, Mar.
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?

Because the number of days in a month is not constant, adding a number
of months to a date is capable of more than one interpretation. Most
folk are happy with adding the months on and then ensuring that the
day is not later than the last day of the resultant (year, month)
combination -- this is what the pyfdate routine appears to be doing.
However there are some interesting ideas floating around e.g. IIRC an
eminent personage once asserted in this newsgroup that adding 1 month
to 31 Jan in a non-leap year should produce 3 Mar.

There is also the general question with date intervals of whether the
first day is included in the calculation or not. E.g. work on Monday,
Tuesday, Wednesday: that's 3 days service. Put money into the bank on
Monday, withdraw it on Wednesday: that's likely to attract 2 days
interest.

One needs to understand exactly what calculation is required, and
exactly what calculation is provided by the software that is proposed
to be used.

HTH,

John
 
W

W. eWatson

John said:
Presuming that we are talking about the Gregorian calendar, and not
one of your own invention, you are (one trusts) alone in that belief.
There are SEVEN whole months and a bit between August 29, 2008 and
April 1, 2009. Count the months: Sep, Oct, Nov, Dec, Jan, Feb, Mar.


Because the number of days in a month is not constant, adding a number
of months to a date is capable of more than one interpretation. Most
folk are happy with adding the months on and then ensuring that the
day is not later than the last day of the resultant (year, month)
combination -- this is what the pyfdate routine appears to be doing.
However there are some interesting ideas floating around e.g. IIRC an
eminent personage once asserted in this newsgroup that adding 1 month
to 31 Jan in a non-leap year should produce 3 Mar.

There is also the general question with date intervals of whether the
first day is included in the calculation or not. E.g. work on Monday,
Tuesday, Wednesday: that's 3 days service. Put money into the bank on
Monday, withdraw it on Wednesday: that's likely to attract 2 days
interest.

One needs to understand exactly what calculation is required, and
exactly what calculation is provided by the software that is proposed
to be used.

HTH,

John
What I'm trying to do is adjust date-time stamped file names for date and
time errors. The software program collects through a period that roughly
coincides with night hours every day and according to the OS clock. It
sometimes happens that a user sets the clock to the wrong day or hour,
possibly both. Possibly even the month or year. I'm trying to allow a user
the opportunity to repair the problem. (Date-time stamp part of the name is
yyyymmdd_hhmmss.) Correcting the date needs to be done easily and
accurately. For example, if on August 25, he mistakenly sets the date to
July 25, and discovers this problem on the real Oct. 5, he should be able to
shift all dates from July 25 through Sept. 5 to Aug. 25 through early Oct.,
allowing for day oddities in a month during the period. (I hope I got those
dates right; otherwise, I think you get the idea. In other words, he needs
to shift about 40 days of data to the correct dates.) Or:

True calendar period: August 25 to Oct. 5
Recorded calendar period: July 25 to Sept. 5 (roughly 5)

A second function is to correct the time stamp for drift in the clock. For
this, I'm expecting the user knows the daily drift, +/-, in seconds of the
clock. When he decides, for example, that he's let the clock drift for more
than, say, 120 seconds, he may want to adjust the time stamp for all files
collected since the last time he set the clock properly. About the best
anyone can hope for is that the data is accurate to within 4 to 5 seconds,
so over periods of say a month between adjustments this should be OK. The
computers used do not have time data other than that provided by the h/w
clock on the computer. This method is not meant to be a cure all, just to
get the time stamp within a reasonable value. Personally, I reset the time
about every 2-3 weeks. Problems that arise here are associated with working
near midnight. Again, it's possible to set some time or date component
incorrectly each time one needs to get drift under control.

The OSes involved can be Win XP, Win 2000, or even older Win OSes, varieties
of Apple and Linux. I don't want to go below the level of the simple h/w
clock a typical user might have access to through the OS s/w user interface.
However, I do not need to get into OS details to solve the above problems.

There are of course times when a mistaken setting is caught early, so the
adjustment becomes easy. Suppose the day is taken as May 3 on May 5, and two
days later the mistake is noticed. Changing the date for these files is
pretty easy (with the program).

Well, back to the drawing board for awhile to see how this plays against
pyfdate.
 
J

John Machin

What I'm trying to do is adjust date-time stamped file names for date and
time errors. The software program collects through a period that roughly
coincides with night hours every day and according to the OS clock. It
sometimes happens that a user sets the clock to the wrong day or hour,
possibly both. Possibly even the month or year. I'm trying to allow a user
the opportunity to repair the problem. (Date-time stamp part of the name is
yyyymmdd_hhmmss.) Correcting the date needs to be done easily and
accurately. For example, if on August 25, he mistakenly sets the date to
July 25, and discovers this problem on the real Oct. 5, he should be able to
shift all dates from July 25 through Sept. 5 to Aug. 25 through early Oct.,
allowing for day oddities in a month during the period. (I hope I got those
dates right; otherwise, I think you get the idea. In other words, he needs
to shift about 40 days of data to the correct dates.)

.... all of which is absolutely nothing to do with your surprise at the
result of whatever.plus(months=6).

So for some period from recorded date X to recorded date Y, the
recorded dates of out of kilter by D days. X = Jul 25 2008, Y Sep 5
2008, and D is 31 (days from Jul 25 to Aug 25). All you have to do is
(pseudocode):

if X <= recorded_date <= Y:
new_recorded_date = recorded_date.plus(days=D)

HTH,
John
 
W

W. eWatson

John said:
... all of which is absolutely nothing to do with your surprise at the
result of whatever.plus(months=6).
Really? It opened new insights for me. The example above is not the only
correction I need to deal with. Further, the author is likely to soon
clarify some of the date rules in the tutorial that were not obvious or
mentioned there.
 
W

W. eWatson

W. eWatson said:
Really? It opened new insights for me. The example above is not the only
correction I need to deal with. Further, the author is likely to soon
clarify some of the date rules in the tutorial that were not obvious or
mentioned there.
Strange how my post got hooked into this side spur. I'll re-post.
 
W

W. eWatson

John said:
... all of which is absolutely nothing to do with your surprise at the
result of whatever.plus(months=6).
Really? It opened new insights for me. The example above is not the only
correction I need to deal with. Further, the author is likely to soon
clarify some of the date rules in the tutorial that were not obvious nor
mentioned there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top