Date functions in C
Author Message
Date functions in C

Does anyone know of any date functions in 'C'?  I need to be able to
find the day of week given a date (day[1..31],month[1..12],year)
ANy help would be much appreciated !!

Fri, 24 Jun 1994 08:50:39 GMT
Date functions in C

Quote:
> Does anyone know of any date functions in 'C'?  I need to be able to
> find the day of week given a date (day[1..31],month[1..12],year)
> ANy help would be much appreciated !!

I've got a library that will do that.  I don't know exactly how portable it
is (was written in MSC, I use it with TurboC), but I'd be willing to send
you the source if you haven't found anything else...

=============================================================================

Nothing is impossible for the man who doesn't have to do it.
=============================================================================

Sat, 25 Jun 1994 07:41:34 GMT
Date functions in C

|
|> Does anyone know of any date functions in 'C'?  I need to be able to
|> find the day of week given a date (day[1..31],month[1..12],year)
|> ANy help would be much appreciated !!
|
|I've got a library that will do that.  I don't know exactly how portable it
|is (was written in MSC, I use it with TurboC), but I'd be willing to send
|you the source if you haven't found anything else...

I like this little hack (it only works for this century):

int S[12] = { 1, 4, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6 };

int DayOfWeek(int Y, int M, int D) {

int C;

C= Y/12 + Y%12 + (((Y%4)||(M < 3))?0:1);

Y/= 12;

return (C+Y/4+S[M-1]+D)%7;

Quote:
} /* DoyOfWeek */

This function returns 0 for Saturday, 1 for Sunday, etc.

Hope you like it ...

regards

Sat, 25 Jun 1994 20:39:50 GMT
Date functions in C

I have a problem with a C program I'm writing.  I need to write to a
printer some simple output.  Normally I would use:
fprintf (stdprn, "Hello, World. \n");

and the output would go to the printer.  However, on our PC network
we have a number of different printers; one attached to LPT1:, another
to LPT2: and a third to LPT3:.  How can I, within the program, choose
between the different ports?

I have examined  stdio.h  and come to the conclusion that the machine
data (i.e. where is stdin, stdout, stdaux and stdprn) must be made at
compile time.  Am I right, and can anyone help me?

--
Malcolm Smith              yellnet:    +47 2 637690
Alcatel Telecom Norway AS

-------------------  "But you can't dig your way off a planet!"  --

Sat, 25 Jun 1994 21:45:35 GMT
Date functions in C

Quote:

>I have a problem with a C program I'm writing.  I need to write to a
>printer some simple output.  Normally I would use:
>            fprintf (stdprn, "Hello, World. \n");

>to LPT2: and a third to LPT3:.  How can I, within the program, choose
>between the different ports?
>--
>            Malcolm Smith          yellnet:    +47 2 637690

I'm not sure this will work, but you might want to give it a try:

main()
{
FILE *fp;
char filename[8] = "LPT1:";

fp = fopen(filename, "wt");           /* open up LPT1: as a file */
fprintf(fp, "Hello world\n");

fclose(fp);

Quote:
};

If the above works, then it is simply a matter of selecting and
changing the filename variable to LPTx: in your program. I am not sure this
will work, like I said, but I seem to recall reading somewhere that DOS
(and UNIX for that matter) can treat peripherals such as COMx:, LPTx:, CON:
etc. as files and so you can read/write to them. Certain operations probably
won't make sense though, such as expecting input from the printer or sending
output to the keyboard, in which case I don't know how DOS will react.

--
'The sky above the port was    ___   ____
the color of television,     /___\_U_A_/_"  Rishad J. Quazi

Sun, 26 Jun 1994 13:43:59 GMT
Date functions in C

Quote:

> I have a problem with a C program I'm writing.  I need to write to a
> printer some simple output.  Normally I would use:
>             fprintf (stdprn, "Hello, World. \n");

> and the output would go to the printer.  However, on our PC network
> we have a number of different printers; one attached to LPT1:, another
> to LPT2: and a third to LPT3:.  How can I, within the program, choose
> between the different ports?

> I have examined  stdio.h  and come to the conclusion that the machine
> data (i.e. where is stdin, stdout, stdaux and stdprn) must be made at
> compile time.  Am I right, and can anyone help me?

There is a 'better' way to print characters: the function 'biosprint'
(this is for Turbo-C but there must exist an equivalent for other
compilers). This function accept the output port as parameters.

-----------------------------------------------------------------------------

S.A. MBLE
member of Philips group
Belgium

My opinions do not represent those of my company.

Sun, 26 Jun 1994 20:45:20 GMT
Date functions in C
Quote:

>I have a problem with a C program I'm writing.  I need to write to a
>printer some simple output.  Normally I would use:
>            fprintf (stdprn, "Hello, World. \n");

>and the output would go to the printer.  However, on our PC network
>we have a number of different printers; one attached to LPT1:, another
>to LPT2: and a third to LPT3:.  How can I, within the program, choose
>between the different ports?

>I have examined  stdio.h  and come to the conclusion that the machine
>data (i.e. where is stdin, stdout, stdaux and stdprn) must be made at
>compile time.  Am I right, and can anyone help me?

Just open the device with a call to "fopen".

printer3 = fopen( "LPT3:", "w" );

and use "fprintf( printer3, ..... );" to write to
the device.
--

Tel. (514) 343-6111,ext 5234    Fax  (514) 343-2155

Mon, 27 Jun 1994 05:01:44 GMT
Date functions in C

Quote:

>There is a 'better' way to print characters: the function 'biosprint'
>(this is for Turbo-C but there must exist an equivalent for other
>compilers). This function accept the output port as parameters.

Hardly better.  Completely nonportable, and the Turbo C "BIOS" calls are
bogus -- they check for control-C, among other things.

The handle-based DOS calls for printer I/O are quite sufficient, and this has
strayed from comp.lang.c.

Quote:
>-----------------------------------------------------------------------------

>                                 S.A. MBLE
>                                 member of Philips group
>                                 Belgium

>  My opinions do not represent those of my company.

--
Arlie Davis

Home of the Rather Long Sitename Club.

|grep ooga-booga >>~/.signature # Yes, this is the One True .signature Virus!

Mon, 27 Jun 1994 05:59:57 GMT
Date functions in C
I originally send this E-mail, but have since had enough more requests for
it that I felt it warranted a post.  Hope I'm not out of line.

Here's what I've got...  Works pretty well...  It was originally written for
MSC, I don't remember if I had to change anything for TurboC.

============================================================================
reference:
============================================================================

** Date functions

* - all date functions must include date.h

DATE julian_date ( int day, int month, int year );

Calculates the julian date for the specified day, month &
year.

returns:        julian date

int valid_date ( int day, int month, int year );

Validate the specified day, month & year. For example, the
following dates are invalid:
Feb 29, 1973 (not a leap year)
Apr 31 (30 days in April)

returns:        0       if date is valid
1       if date is invalid

int years_old ( int day, int month, int year );

include files: dos.h

Calculate how many whole years between the given day,
month, year & the system clock date.

returns:        number of whole years

void calendar_date ( DATE jdate, int *day, int *month, int *year );

Calculates the day, month & year corresponding to the
given julian date number.

returns:        nothing

int day_of_week ( DATE date );

Calculates the day of the week for the specified julian day
number.

returns:        1=Sun, 2=Mon,...7=Sat.

DATE easter ( int year );

Calculate the julian day number of Easter for the specified
year, which cannot be negative.

returns:        julian date

============================================================================
date.h:
============================================================================

typedef long DATE;

DATE julian_date(int day, int month, int year);
void calendar_date(DATE jdate, int *day, int *month, int *year);
int valid_date(int day, int month, int year);
int day_of_week(DATE date);
DATE easter(int year);
void month_calendar(int month, int year);
void year_calendar(int year);
int years_old(int day, int month, int year);

============================================================================
datelib.c:
============================================================================

#include <dos.h>  /* for date structure */
#include <stdio.h>
#include <date.h>

DATE julian_date(int day, int month, int year)

/* calculate the julian day number for the specified day, month, and year */
/* if the year is B.C. it must be negative. */

{
int a, b;
float year_corr;
/* Correct for negative year. */
year_corr = (year > 0 ? 0.0 : 0.75);
if (month <= 2)
{
year--;
month += 12;
}
b = 0;
/* Cope with the Gregorian calendar reform */
if (year * 10000.0 + month * 100.0 + day >= 15821015.0)
{
a = year /100;
b = 2 - a + a / 4;
}
return (DATE) (long) (365.25 * year - year_corr) + (long) (30.6001 * (month + 1)) +
day + 1720994L + b;

Quote:
}

int valid_date(int day, int month, int year)

/* Validate the specified day, month, and year.  For example, the following */
/* dates are not valid: */
/* February 29, 1973 (not a leap year) */
/* April 31 (April has only 30 days) */

{
int cal_day, cal_month, cal_year;
/* Convert date to Julian day number and back */
calendar_date(julian_date(day, month, year), &cal_day, & cal_month, &cal_year);
/* Date is valid if day, month, and year did not change */
return ((day == cal_day) && (month == cal_month) && ( year == cal_year));

Quote:
}

int years_old(int day, int month, int year)
{
struct date sys_date;
DATE system_date, test_date;

getdate(&sys_date);
system_date = julian_date(sys_date.da_day, sys_date.da_mon, sys_date.da_year);
test_date = julian_date(day, month, year);
return((system_date - test_date) / 365.25);

Quote:
}

void calendar_date(DATE jdate, int *day, int *month, int *year)

/* calculate the day, month, and year corresponding to a julian day number */
/* The year will be negative if it is B.C. */

{
long a, b, c, d, e, z, alpha;
z = jdate + 1;
/* cope with the Gregorian calendar reform */
if (z < 2299161L)
{
a = z;
}
else
{
alpha = (long) ((z - 1867216.25) / 36524.25);
a = z + 1 + alpha - alpha / 4;
}
b = a + 1524;
c = (long) ((b - 122.1) / 365.25);
d = (long) (365.25 * c);
e = (long) ((b - d) /30.6001);
*day = (int) b - d - (long) (30.6001 * e);
*month = (int) (e < 13.5) ? e - 1 : e - 13;
*year = (int) (*month > 2.5) ? (c - 4716) : c - 4715;

Quote:
}

int day_of_week(DATE date)

/* Calculate the day of the week for the specified Julian day number */
/* 1 = Sunday, 2 = Monday, ..., 7 = Saturday */

{
return (int) ((date + 2) % 7 + 1);

Quote:
}

DATE easter(int year)

/* Calculate the Julian day number of Easter for the specified year, which */
/* cannot be negative. */

{
int a, b, c, d, e, f, g, h, i, k, l, m, n, p, day, month;
if (year >= 1583)
{
/* Calculate date of Easter for a year in the Gregorian calendar. */
a = year % 19;
b = year /100;
c = year % 100;
d = b / 4;
e = b % 4;
f = (b + 8) / 25;
g = (b - f + 1) / 3;
h = (19 * a + b - d - g + 15) % 30;
i = c / 4;
k = c % 4;
l = (32 + 2 * e + 2 * i - h - k) % 7;
m = (a + 11 * h + 22 * l) / 451;
n = (h + l - 7 * m + 114) / 31;
p = (h + l - 7 * m + 114) % 31;
month = n;
day = p + 1;
}
else
{
/* Calculate date of Easter for a year in the Julian calendar. */
a = year % 4;
b = year % 7;
c = year % 19;
d = (19 * c + 15) % 30;
e = (2 * a + 4 * b - d + 34) % 7;
f = (d + e + 114) / 31;
g = (d + e + 114) % 31;
month = f;
day = g + 1;
}
return julian_date(day, month, year);

Quote:
}

============================================================================
end
============================================================================

I've used this pretty extensively in TurboC code & it seems to work quite
well.  I'd like to credit the source (this is not my own code), but I
honestly don't remember where I got this from...

Jon

=============================================================================

Nothing is impossible for the man who doesn't have to do it.
=============================================================================

Tue, 28 Jun 1994 08:36:56 GMT
Date functions in C
...
Quote:
>DATE julian_date(int day, int month, int year)

>/* calculate the julian day number for the specified day, month, and year */
>/* if the year is B.C. it must be negative. */

>{
>    int a, b;
>    float year_corr;
>    /* Correct for negative year. */
>    year_corr = (year > 0 ? 0.0 : 0.75);
>    if (month <= 2)
>            {
>            year--;
>            month += 12;
>            }
>    b = 0;
>    /* Cope with the Gregorian calendar reform */
>    if (year * 10000.0 + month * 100.0 + day >= 15821015.0)
>            {
>            a = year /100;
>            b = 2 - a + a / 4;
>            }
>    return (DATE) (long) (365.25 * year - year_corr) + (long) (30.6001 * (month + 1)) +

^^^^^^^^^^^^^^^^^^^^

Quote:
> day + 1720994L + b;
>}

Having seen forms of this term fairly recently in time computation
routines in about three newsgroups, it would seem time to stomp on it.
Note that if year is 1992, the result of multiplication could be either
727578.000000... or 727577.999999... .  The latter would result in
a one-day error in the result.  (Probably got lucky here: all floating
points I know of happen to have exact representations for "small"
multiples of 0.25.)

Actually, the computation is really an integral one; here's an all-
integer version (for Gregorian and year>0 only!):
DATE julian_date(int day, int month, int year)
{
if( month <= 2) year--, month += 12;

return (DATE) (
(365L * year) +
(year/4) - (year/100) + (year/400) +
( (153 * (month + 1)) / 5) +
day +
1720996L
);

Quote:
}

I leave it as an exercise to the reader to "put back" the
adjustments for the pre-Gregorian (Julian) calendar and B.C. if desired.
(Why bother when it doesn't take into account that the year
used to start in late March; August once had 30 days;
as well as the fact the British Empire didn't adopt the Gregorian
calendar until the 1700's, the Russians until this century, and
many other countries at other times!)

By the way, it's "off by one" from what astronomers would expect.
For 1/16/1992 it returns 2448637.
At 0:00 UT on 1/16/1992 the Julian Date was 2448637.50000.
Tables are written as "days elapsed at Greenwich noon", and looking
up 1/16/1992 will show 2448638.

Quote:

>void calendar_date(DATE jdate, int *day, int *month, int *year)

This routine has similar problems.  Again, left as an exercise.

Quote:
>I've used this pretty extensively in TurboC code & it seems to work quite

PC huh?  Then I guess you'd _like_ to get rid of the floating point :-)

Quote:
>well.  I'd like to credit the source (this is not my own code), but I
>honestly don't remember where I got this from...

Well, then we can't blame you!  :-)

Quote:

>Jon

Tue, 05 Jul 1994 06:30:09 GMT

 Page 1 of 2 [ 15 post ]

Relevant Pages