#Calendar conversions
5 messages in this thread
I'm trying to write a macro for Final Writer that needs to know what day
of the week the first day of the month is for any month and year. I found
the code below and tried to convert it to ARexx with no success. I used
the ARexx operator '//' for the C modulus operator (%). I think that they
perform the same function. The second to last line:
index = index – 7 * (index / 7)
looks like it should always be zero. Any help would be greatly
appreciated.
/*****
datename()
This function returns the day of the week for a date. It is based
on Zeller's Congruence.
Argument list: int month the month
int day the day
int year the year
Return value: char * a pointer to the day of the week
*****/
char *datename(int day, int month, int year)
{
static char *days[] = {"Sun.", "Mon.", "Tues", "Wed.",
"Thur", "Fri.", "Sat.", "Sun."};
int index;
if (year < 100) /* If century is missing */
year += 1900;
if (month > 2) { /* Everything is from February */
month -= 2;
} else {
month += 10;
year–;
}
index = ((13 * month – 1) / 5) + day + (year % 100) + ( (year % 100) /
4) + ((year / 100) / 4) – 2 * (year / 100) + 77;
index = index – 7 * (index / 7);
return days[index];
}
Thanx.
William,
Try the following code: CALCDATE: PARSE ARG FYEAR, FMONTH, FDAY IF FMONTH <
3 THEN DO
FYEAR = FYEAR – 1
FMONTH = FMONTH + 13 END ELSE DO
FMONTH = FMONTH + 1 END
/* USE INTEGER DIVIDE % OPERATOR */ N =
(1461 * FYEAR)%4 + (153*FMONTH)%5 + FDAY ND=((N-621049)//7)+1 /*
SUNDAY-SATURDAY = 1-7 RESPECTIVLY*/ RETURN ND
I have all the code to convert the dates, and currently I am trying to port it
to ARexx but for some reason I can only boot my mcahine in AmigaDOS 1.3 (my 2.0
is not working) so A-Rexx is not accessible but if you want the easy way out
and if you are not going to be multitasking while doing this. The easieast way
to do this is using the foollowing function:
/* easy way out REXX program for day of the week
caution this program changes the date for a few seconds
making it a bad solution for multitasking machines */
DDATE: PROCEDURE
ARG InDate, Param
SaveDate = DATE('U') /*saves the current date */
'date' InDate /* assigns InDate to todays date */
RValue = DATE(Param) /* get date in desire format*/
'date' SaveDate /*restore todays date */
return RValue
/* example call: */
TodaysDay = DDATE('04/02/95', 'w')
say TodaysDay
/* should come up with Sunday */
This function will work similar to REXX DATE except it need todays date.
I have not try this in the Amiga, but it works great under OS/2, as soon as I
get my machine up I'll post the A-REXX code up.
hope this helps
Eduard Joseph
Oops I forgot Amiga's dates are different than the PeeCee's therefore the
following should be needed (boy I wish I could get A-DOS 2.0 working again, if
I only had the time)
/* easy way out REXX program for day of the week
caution this program changes the date for a few seconds
making it a bad solution for multitasking machines
also there might be problems running this too close to midnight*/
DDATE: PROCEDURE
ARG InDate, Param
SaveDate = DATE('N') /*saves the current date */
parse SaveDate DD MM YY /* get day month year */
SaveDate = DD'-'MM'-'YY /* set save date in AMIGA format */
'date' InDate /* assigns InDate to todays date */
RValue = DATE(Param) /* get date in desire format*/
'date' SaveDate /*restore todays date */
return RValue
/* example call */
say DDATE('02-APR-95', 'w')
/* should return with Sunday */
I hope this one works and it helps
Eduard Joseph
Looking quickly at my sample codes I found this one.
This one should work OK, and its a lot cleaner
/* REXX routine that returns index of week, 1 = monday. */
/* requires year to be a 4 digit year */
DayOfTheWeek: PROCEDURE
PARSE ARG month, day, year
IF ((month = 1) | (month = 2)) THEN DO
month = month + 12
year = year – 1
END
leapyear = (year % 4) – (year % 100) + (year % 400)
RValue = ((day + (2 * month) + (3 * (month + 1) % 5 ) + year + leapyear) //
7) + 1
RETURN RValue
say DayOfTheWeek(4,2,1995)
/* should return 7 */
well I hope this one those it
Eduard Joseph