| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Day of year |
Hi Frederic,
You asked:
FT> How do you calculate the day of the year in C?
Lots of ways...
There is one in SNIPPETS, but I consider the method used cumbersome and
slower than needed.
Code sample:
/* 21/12/98 G. White */
/* Use freely, no responsibility taken */
/* Prototype */
int day_no (int year,int month,int day);
/* Assumes valid calendar dates:- */
/* year = full calendar year */
/* month: 1 to 12 */
/* day: 1 to 31 */
/* So for the writing date:- year = 1998; month = 12; day = 21; */
/* If months are zero based remove the first element of days[] */
/* If days are zero based increase the return value by one */
const int days[] = {0,0,31,59,90,120,151,181,212,243,273,304,334};
int day_no (int year,int month,int day)
{
if ((month > 2) && ((((year % 4) == 0) && ((year % 100) != 0)) ||
((year % 400) == 0)))
return days[month] + day + 1;
else
return days[month] + day;
}
/* End sample */
There are plenty of other ways of organising even something this simple,
especially the leap year checking line...
Untested, typed straight into the message. (Get out just in case ).
George
* SLMR 2.1a * Wishing you a Happy Christmas and a Peaceful New Year
--- Maximus/2 3.01
* Origin: DoNoR/2,Woking UK (44-1483-717904) (2:440/4)SEEN-BY: 396/1 632/0 371 633/260 262 267 270 371 634/397 635/506 728 639/252 SEEN-BY: 670/218 @PATH: 440/4 255/1 251/25 396/1 633/260 635/506 728 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.