Date searching
12 messages in this thread
Hello gang,
Could someone please have a look at my 'if' setup. I wrote a function
to find all dates in a file between the from and to structs. As you see I
have set the initial variables to near infinity. If I don't change the
dates all the records in my file are called. However, if I put in a range
I often get nothing even though I have checked and confirm dates in the
range. Although this algorythm looks good to me, can anyone tell me whats
going wrong? Or, if you have some school taught method for this I would
appreciate your help as all I know from programming is what I learn from a
book at home. I'm using an old Manx 3.6 compiler.
date_search()
{
struct date actual, from, to;
from.day = 01, from.month = 01, from.year = 0000;
to.day = 31, to.month = 12, to.year = 9999;
if (actual.year >= from.year && actual.year <= to.year &&
actual.month >= from.month && actual.month <= to.month)
if (actual.day >= from.day && actual.day <= to.day ||
actual.day < from.day && actual.month > from.month ||
actual.day > to.day && actual.month < to.month)
}
THANKS FOR THE HELP! Henry
When or where are you setting the 'actual' date? It's set to random values
unless you set or calc it yourself.
Hi John,
Thanks for the response. I cut down much of the function so anyone
helping would find the problem easier. At run time the function reads in a
record at a time after I have prompted for a date range.
Peter Wade has given me the solution already, but thanks again.
Regards, Henry
Your first if statement will not work. The && on all tests means that the
month must be >= the 'from' month and <= the 'to' month, but if the date
range crosses a year end this may not be valid, e.g. if 'from' is
01/12/1993 and 'to' is 01/01/1994 then no month will pass this test (N.B.
I am using British dd/mm/yy format here).
The thing to remember is that if the year is greater than the 'from' year
you should not test the 'from' month, but if the years are equal you do
test the month. A similar condition applies to the day, and to the 'to'
month and day. For example :
date_ok = TRUE ;
if ( year < from.year || year > to.year )
date_ok = FALSE ;
else
{
if (( year == from.year && month < from.month ) ||
( year == to.year && month > to.month ))
{
date_ok = FALSE ;
}
else
{
if ((year == from.year && month == from.month && day < from.day)||
(year == to.year && month == to.month && day > to.day ))
{
date_ok = FALSE ;
}
}
}
P.S. I don't know about Manx, but SAS has library funtions which use long
integers that store time/date values as the number of seconds since
00:00:00 Greenwich Mean Time, January 1, 1970. If your compiler can do
this then it makes date/time comparisons a lot easier.
Peter Wade
Autopiloting from London, England
Peter,
A better way than using the seconds from Jan 1, 1970 would be to
convert all three dates to Julian Day Numbers and then compare them. This
will allow you to compare dates before 1970, as well as dates far into the
future. I have the Julian->Gregorian and Gregorian->Julian routines in C,
or you can look in most calendar program source.
James Du Bois via AutoPilot
Member of the Bermuda Triangle Exploratory Expedition 1992 – 1951
James,
I assume you are using the whole year as part of the Julian date? I
hope so, otherwise your code will break on decades (if only using the last
digit of the year, as the military does) or the century. I had to come up
with some work arounds due to that, for a military supply program.
"You do trust me, don't you? Of course you do."
— "To Play the King" (BBC)
Brian,
The Julian day number is the number of days since January 1, 4712 B.C.
for instance today is the 26th of July, 1994. The Julian day number is
2449559, in other words, there have been 2,449,559 days since Jan. 1, 4712
B.C. to today. Please note, this number only takes up 3 bytes, and will
go to May 9, 41222 A.D. before it goes into the 4th byte. I have never
understood why the system programmers haven't been using Julian day
numbers to do date math, since all you have to do is convert both dates to
Julian day numbers and then do the operations on them. Anyway, since you
use the whole date to get the day number, there is no problem with
breaking on decades, centuries, and millenium. Of course, there is a
function to convert Julian day numbers to the Gregorian calendar date.
James Du Bois via AutoPilot
Member of the Bermuda Triangle Exploratory Expedition 1992 – 1951
James,
I hate to pick nits (yeah right….if you believe that, I've got a
lovely bridge for sale <g>), but according to "The American Ephemeris and
Nautical Alminac" page 531:
"The Julian Day reckoning begins with Julian Day Number 0 for January
1, 4713 B.C., Julian proleptic calendar; the Julian Day Number therefore
denotes the length of time that has elapesed at Grenwich noon at the
beginning of the astronomical day, since this epoch."
Vic,
I hate to pick nits too 8-), but the routines I use have a year 0 A.D.
since they are to be used by a computer, and computers do understand the
concept of 0. Before anybody else jumps in, yes, I know there is no year
0 in the Christian Era calendar. There is also no year 1, no year 2, no
year 3, … , at least until 36? A.D. when some monk decided that the
calendar should start at the Birth of Christ. Since nobody knew when that
was, he took a W.A.G. and decided this year was approx. 360 years before.
Yes, this means the new millenium starts on January 1, 2000 instead of
January 1, 2001 as the "experts" will tell you.
James Du Bois via AutoPilot
Member of the Bermuda Triangle Exploratory Expedition 1992 – 1951
James,
that was my observation as well. Once I had coded the routines for my
supply program, I used it for all of my date calculations. It was far
cheaper, in terms of effort involved, to use those routines.
"You do trust me, don't you? Of course you do."
— "To Play the King" (BBC)
Hi Peter,
Thank you for the help with my date range problem. The function works
properly now.
Yes the Manx system also has library functions to do the work. I figured
that on the chance someones system clock wasn't set right, all their data
would be inaccurate. For that reason I went with an immediate prompt for a
range.
I was in London in Sept/85. Had a great time but got lost a lot.
Have a nice summer! Henry
Autopiloting from Montreal, Canada
Hello Henry,
Your algorythm looks good on the first pass but it is wrong. It looks
like you tried putting too much into the two if's. You will probably need
several if's.
To find your problems you can step through your code using different dates
and you will see what part needs to be changed. Try these dates to start,
from.day = 15 from.month = 10 from.year = 1960
to.day = 4 to.month = 5 to.year = 1990
then try an actual date of
day = 6 month = 3 year = 1970
and it will fail due to the month if I'm reading your code right. And in
this case the month and day do not matter because the year is between the
to and from year and not equal to either.
If the actual year was equal to 1960 or 1990 then you would need to do a
test of the month, but a different test depending on which year was equal.
And maybe a another test if all three years are equal.
Is this enough to get you going or do you need more help? I could say
more but it might be best if you worked it out yourself. Looking for and
finding all the possibilities part of programming.
//
\X/ Amiga or bust! Gary Bonnstetter, Bonnsoft