473,416 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

formula used in calculating diffrence between dates

Hi folks,
Al Bowers wrote this program on comp.lang.c
Date: 2001-07-09 13:41:58 PST
#include <stdio.h>

int isleap (unsigned yr);
static unsigned months_to_days (unsigned month);
static long years_to_days (unsigned yr);
long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day);

int main(void) {
unsigned y1 = 2000, m1 = 2, d1 = 1, /* represent 01FEB2000 */
y2 = 2000, m2 = 3,d2 = 1; /* represent 01MAR2000 */
long date1,date2;

date1 = ymd_to_scalar(y1,m1,d1);
date2 = ymd_to_scalar(y2,m2,d2);
printf("There are %ld days\n",date2-date1);
printf("It %s a leap year\n",(isleap(y1))?"is":"is not");
return 0;
}

int isleap (unsigned yr)
{ return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);}

static unsigned months_to_days (unsigned month)
{ return (month * 3057 - 3007) / 100; }

static long years_to_days (unsigned yr)
{return yr * 365L + yr / 4 - yr / 100 + yr / 400;}

long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day)
{
long scalar;
scalar = day + months_to_days(mo);
if ( mo > 2 ) /* adjust if past February */
scalar -= isleap(yr) ? 1 : 2;
yr--;
scalar += years_to_days(yr);
return scalar;
}

can anybody explain the logic or formula applied in functions:-

static unsigned months_to_days (unsigned month);
static long years_to_days (unsigned yr):
if want to know about the formula which is used here
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 14 '05 #1
2 3137
--------long reply below

"celsius" <at*******@yahoo.co.uk> wrote in message
news:cl****************@plethora.net...
Hi folks,
Al Bowers wrote this program on comp.lang.c
Date: 2001-07-09 13:41:58 PST
#include <stdio.h>

int isleap (unsigned yr);
static unsigned months_to_days (unsigned month);
static long years_to_days (unsigned yr);
long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day);

int main(void) {
unsigned y1 = 2000, m1 = 2, d1 = 1, /* represent 01FEB2000 */
y2 = 2000, m2 = 3,d2 = 1; /* represent 01MAR2000 */
long date1,date2;

date1 = ymd_to_scalar(y1,m1,d1);
date2 = ymd_to_scalar(y2,m2,d2);
printf("There are %ld days\n",date2-date1);
printf("It %s a leap year\n",(isleap(y1))?"is":"is not");
return 0;
}

int isleap (unsigned yr)
{ return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);}

static unsigned months_to_days (unsigned month)
{ return (month * 3057 - 3007) / 100; }

static long years_to_days (unsigned yr)
{return yr * 365L + yr / 4 - yr / 100 + yr / 400;}

long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day)
{
long scalar;
scalar = day + months_to_days(mo);
if ( mo > 2 ) /* adjust if past February */
scalar -= isleap(yr) ? 1 : 2;
yr--;
scalar += years_to_days(yr);
return scalar;
}

can anybody explain the logic or formula applied in functions:-

static unsigned months_to_days (unsigned month);
static long years_to_days (unsigned yr):
if want to know about the formula which is used here
--
IMHO the formula in months_to_days is ad hoc, made up to work.
Remember that integer divides truncate. Follow the flow of control
to figure out what is done where. Start at main, January is month 1
since Feb is month 2. ymd_to_scalar is called. Clearly this takes
year-month-day to a long (i.e. scalar).

In ymd_to_scalar, months_to_days(month) is called. This returns
0 for January, 31 for February. Jan 1 is 0 days into the year,
Feb 1 is 31 days into the year. For March the formula gives 61.64,
truncates to 61, 2 days more than Mar 1 is into a non-leap year(31+28=59).
This goes on with 2 days extra. (I checked it for December).
Return to ymd_to_scalar and you see:
if ( mo > 2 ) /* adjust if past February */
scalar -= isleap(yr) ? 1 : 2;
This takes out the extra day in leap year and extra 2 days in
non-leap years.

Now, scalar is the number of days into the year, based on day of month,
month and leapyear. Then scalar is adjusted by the number of days since
some the fictitious year zero, including adjustments for leap days. The
leap day formula in isleap() and years_to_days() is based on
the Gregorian calendar which did not exist until 1582 (see the calendar FAQ
http://www.tondering.dk/claus/calendar.html ). Therefore, DO NOT USE
this program for years before Gregorian calendar was introduced.

yr--; //NEW NOTE calculate days that occurred
before this year
scalar += years_to_days(yr);
static long years_to_days (unsigned yr)
{return yr * 365L + yr / 4 - yr / 100 + yr / 400;}
Here, yr*365L is the days if all years are non-leap years (365L is a long
constant).
+ yr/4 adds a day for each four year cycle
-yr/100 subtracts a day for each century year (like 1900,
2000)
+yr/400 adds a day for century years that are leap years
(2000).

---------------- Bill
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.

--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 14 '05 #2
celsius wrote:
Hi folks,
Al Bowers wrote this program on comp.lang.c
Date: 2001-07-09 13:41:58 PST
#include <stdio.h>

int isleap (unsigned yr);
static unsigned months_to_days (unsigned month);
static long years_to_days (unsigned yr);
long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day);

int main(void) {
unsigned y1 = 2000, m1 = 2, d1 = 1, /* represent 01FEB2000 */
y2 = 2000, m2 = 3,d2 = 1; /* represent 01MAR2000 */
long date1,date2;

date1 = ymd_to_scalar(y1,m1,d1);
date2 = ymd_to_scalar(y2,m2,d2);
printf("There are %ld days\n",date2-date1);
printf("It %s a leap year\n",(isleap(y1))?"is":"is not");
return 0;
}

int isleap (unsigned yr)
{ return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);}

static unsigned months_to_days (unsigned month)
{ return (month * 3057 - 3007) / 100; }

static long years_to_days (unsigned yr)
{return yr * 365L + yr / 4 - yr / 100 + yr / 400;}

long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day)
{
long scalar;
scalar = day + months_to_days(mo);
if ( mo > 2 ) /* adjust if past February */
scalar -= isleap(yr) ? 1 : 2;
yr--;
scalar += years_to_days(yr);
return scalar;
}

can anybody explain the logic or formula applied in functions:-

static unsigned months_to_days (unsigned month);
static long years_to_days (unsigned yr):
if want to know about the formula which is used here


The years_to_days() function is based on the formula used to calculate
when a year is a leap year or not. This is years evenly divisible by 4
are leap years, unless they are also evenly divisable by 100 with the
exception they are evenly divisible by 400. Clear as mud, right?
Basically, it calculates the number of days in a year (365L) and adjusts
for all the leap years (366 days a year) that have intervened.

I am not quite sure of what the month_to_days() function is based on,
but I feel strongly that it is premised on the fact that months are
either 30 or 31 days, with the exception of February which has 28 or 29
days. The function is flawed because it makes the adjustment for leap
years outside of the of the function. By this I mean that
month_to_days() should take care of make the adjustments, not the
calling function. If I were writing this I would do the following:

1. Determine if the year is a leap year and store that value ( 0 or 1 ):
is_it_a_leap_year = isleap( yr );

2. Determine the number of days elapsed for years and store that:
days = years_to_days( yr );

Also, years_to_days() should make the adjustment of subtracting 1
from the year.

3. Determine the number of days elapsed in the current year as follows:
days += month_to_days( month, day, is_it_a_leap_year );

The month_to_days() function would make the adjustment for leap years
(subtract 2 days for non leap years, or 1 day for leap years).
--
Regards,
Stan Milam.
-----------------------------------------------------------------------------
Vita Brevis. Carpe Guitarum! - Jamie Kinscherff
-----------------------------------------------------------------------------
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
4
by: John | last post by:
hey all..... alright, I am frusterated to the point of throwing my machine out the window (this board went down, trying to find stuff on google, this has been a nightmare) so I hope you guys can...
2
by: Andreas Schmitt | last post by:
Hi, Sorry for posting in German before, totally forgot about that when I was pasting this in here from another German newsgroup I was writing to, trying to get help I am programming a simple...
2
by: alex | last post by:
I need a more advanced formula than just an average for calculating items rating. I have: raitng value is on scale 1 to 10. s - sum of all ratings for an item n - number of rates (votes)
5
by: nedian | last post by:
Hello I am student and trying to learn access.I am having problem in making a program can any one help me? i wanted to make 2 tables in access and then create a link.First i am telling u what...
5
by: steve | last post by:
Hi All Not sure if this is the right forum, but I need the formula for calculating the amount of Sales tax (GST) from the tax included price In Australia GST is 10% and the standard formula is...
9
by: mankolele | last post by:
Hi all I need an idea on where to start when calculating a total amount betwee n two dates from a database like I want amount of money paid out from 30/03/2004 and 30/07/2004 ,but in the database...
1
by: Victor | last post by:
Can someone help me with the formula(s) to calculate the RGB components for a gradient from green to red? I'm trying to display a nice graphic with a gradient that reflects the profit or loss and I...
2
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.