Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Function to find DayofWeek from a given date ?

1 view
Skip to first unread message

Sebastien Stormacq

unread,
Oct 9, 1995, 3:00:00 AM10/9/95
to
Hi,
does anyone where I can find the code for a little function to find the day of
the week (monday, sunday, ...) from a given date ?

Many thanks

PS : reply by e-mail, I will post a digest of the answers in the list.
--
--Seb
___________________________________________________________________________
! Sebastien Stormacq ! Murphy's Laws : !
! Ambrasoft ! !
---------------------------------! - If anything can go wrong, it will !
! e-mail : s...@ambrasoft.lu ! - Nothing is as easy as it looks !
! Phone : +352 403 420 ! - Everything takes longer than !
! Fax : +352 403 419 ! one thinks !
---------------------------------------------------------------------------


James Kanze US/ESC 60/3/141 #40763

unread,
Oct 10, 1995, 3:00:00 AM10/10/95
to
In article <45def6$p...@mephisto.eo.net> s...@ambrasoft.lu (Sebastien
Stormacq) writes:

|> In article <45b9i8$6...@mephisto.eo.net>, s...@ambrasoft.lu says...


|> >
|> >Hi,
|> >does anyone where I can find the code for a little function to find the day of
|> >the week (monday, sunday, ...) from a given date ?
|> >
|> >Many thanks
|> >

|> Here's the digest :
|> I received three answers but none of theme where fully acceptable
|> Here's the algo I wrote. It works between 1970 and 2038
|> which is acceptable for out need.

It seems unduly complicated. What's wrong with simply:

int
dayOfWeek( int year , int month , int day )
{
tm time ;
time.tm_year = year ;
time.tm_mon = month ;
time.tm_yday = day ;
time.tm_hour = 12 ;
time.tm_min = 0 ;
time.tm_sec = 0 ;
time.tm_isdst = 0 ;
mktime( &time ) ;
return time.tm_wday ;
}

Since someone else has done the work for you...
--
James Kanze Tel.: (+33) 88 14 49 00 email: ka...@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung

Joseph M. Schmitt

unread,
Oct 12, 1995, 3:00:00 AM10/12/95
to
mktime will not correctly calculate the day of week for dates before Jan
1, 1970 (at least in DOS/WIN), every day will be Sunday (if only that
were true). The following code will work all the time.

/*----------------------------------------------------------------------
*
* Compute_Day
* Compute Day of Week
*
* Usage
*
* int
* Compute_Day(date, month, year)
* int date ;
* int month ;
* int year ;
*
* Parameters
*
* date Date of Month (1 - 31)
* month Month of Year (1 - 12)
* year Year (e.g., 1989)
*
* Description
*
* Compute_Day() computes the day of the week for the given date
* using the Zeller Congruence. It returns a positive value from
* 0 to 6 for the day of the week
*
* Notes
*
* The Zeller Congruence maps Saturday as day 0. Most applications
* treat Sunday as day 0. The parameter ZELLER_OFFSET is used to
* convert the day of the week from Zeller to local.
*
*-
*/

int CMaskStr::ComputeDay(int date, int month, int year )
{
int day;
int yr;
int mn;
int n1;
int n2;

/* Offset from Zeller to local */
/* --------------------------- */
#define ZELLER_OFFSET -1

yr = year;
mn = month ;

/* January or February? */
/* -------------------- */
if ( mn < 3 )
{
/* Yes, make these part of last year */
/* --------------------------------- */
mn += 12;
yr -= 1;
}

n1 = (26 * (mn + 1)) / 10 ;
n2 = (int) ((125 * (long) yr) / 100) ;

day = ((date + n1 + n2 - (yr / 100) + (yr / 400) + ZELLER_OFFSET) %
7);

return day;
}


Kai Hofmann

unread,
Oct 12, 1995, 3:00:00 AM10/12/95
to
Joseph M. Schmitt schrieb in comp.lang.c++ ueber "Re: Function to find
DayofWeek from a given date ?":

> mktime will not correctly calculate the day of week for dates before Jan
>
> 1, 1970 (at least in DOS/WIN), every day will be Sunday (if only that
> were true). The following code will work all the time.
>
[code removed]

But your code will not work for things earlier than 11.1582 or later
than 3200 ....


TITLE
date.library

VERSION
33.158

RELEASE DATE
01.08.1995

AUTHOR
Kai Hofmann (i0...@zfn.uni-bremen.de)
(http://www.informatik.uni-bremen.de/~i07m)

DESCRIPTION
A portable ANSI-C/C++ library that gives you low level functions for
date calculations.
Includes the following features:

- Support for six different date/time measurement systems:
Julian, Gregorian, Heis, Julian Day, Modified Julian Day,
Scaliger Year (other systems will follow).
- Month/Weekday and other date/time string support for 12 languages.
- Support for different countries (implementation not finished!).
- Calculating leap years.
- Functions to calculate the days of a month or a year or between
two dates.
- Functions to calculate the weeknumber and the weekday.
- Check the validity of a date.
- Calculating Easter.
- Comparing dates.
- Powerfull functions to handle differences between dates.
- Transformations from one time measurement system to the others.
- Calculating your local time zone out of your position on the earth.
- Calculating the age of the moon and the moon phase.
- Check the validity of a time.
- Comparing times.
- Transforming 24h time format into seconds and back.
- Transforming 24h time format to Julian Day time format and back.
- Powerfull textengine for creating date/time strings in different
languages, that supports nearly all features of the date library.
- A C++ class concept, which makes it very easy to use the complex
functionality of the date library.
- Autodoc describing all functions in the library.
- Including ANSI-C/C++ sources and test example.

NEW FEATURES
- Better code
- Bug fixes
- More C++ support
- Fixing type conversion bugs
- Better Autodocs
- Supporting more languages
- Functions for checking validity of dates/times
- Julian calendar is stable since 8 and NOT since -7!
- Textengine
- DateClass (basic concept)

SPECIAL REQUIREMENTS
ANSI-C or/and C++ Compiler.

AVAILABILITY
ftp://wuarchive.wustl.edu/pub/aminet/dev/c/date.lha
And all other Aminet sites.

ftp://OAK.Oakland.Edu/pub/SimTel/msdos/c/kh-date.zip
And all other SimTel sites.

Actual beta versions (for all systems) are available from:
ftp://ftp.uni-bremen.de/pub/amiga/date/

PRICE
For NON-COMMERCIAL USE this is Giftware!
(Non-commercial includes Giftware and Shareware!)

Permission for COMMERCIAL USE is only given by an extra available
commercial license that must be validated!
Contact me directly for this license, because it will be individually
handed out per your needs!

But in both cases you have to follow the two guidelines below:

1) You *must* include the following notice in your product and in
your documentation:
"This software uses the date library that is copyrighted 1994-95
by Kai Hofmann"

2) You "must" send me a full version of your product at no cost
including free updates!

DISTRIBUTION
You can copy and distribute this library as long as you do not take
more than $5 for one disk or $40 for one CD!

-------------------------------------------------------------------
Kai Hofmann EMail: i0...@zfn.uni-bremen.de
FB 3, Informatik i0...@informatik.uni-bremen.de
Universität Bremen WWW : http://www.informatik.uni-bremen.de
IRC : PowerStat


Jonathan Polzin

unread,
Oct 13, 1995, 3:00:00 AM10/13/95
to s...@ambrasoft.lu

On 10 Oct 1995, Sebastien Stormacq wrote:

> In article <45b9i8$6...@mephisto.eo.net>, s...@ambrasoft.lu says...
> >
> >Hi,
> >does anyone where I can find the code for a little function to find the day of
> >the week (monday, sunday, ...) from a given date ?
> >
> >Many thanks
> >
> Here's the digest :
> I received three answers but none of theme where fully acceptable
> Here's the algo I wrote. It works between 1970 and 2038
> which is acceptable for out need.
>

> BYTE GetDayFromDate(CTime &Date)
[...]

There is a super-compact algorithm that returns the day of the week of a
supplied date. It is a C expression with total length 44 characters. It is
from the Journal of Recreational Mathematics, Vol 22, No. 4, p. 280-2,
1990. ISSN 0022-412X; the article is credited to Michael Keith and Tom Craver.

The expression is:

(23*m/9+d+4+(m<3?y--:y-2)+y/4-y/100+y/400)%7

It is applicable to the Gregorian calendar, with m=month, d=day, y=year.

--
Jonathan Polzin ///////////////////////////////////////////////////////
Toronto, Canada //////////////////////////////////////////////////////
cs93...@ariel.cs.yorku.ca http://yucc.yorku.ca/~polzinj/ ///////////
"There Are No Alarm Clocks in the Land of Critical Mass" ///////////
-Bob Brinker's MoneyTalk ///////////////////////////////////////////

Thomas Armstrong Kobrich

unread,
Oct 19, 1995, 3:00:00 AM10/19/95
to
Kai Hofmann (i0...@informatik.uni-bremen.de) wrote:
: Joseph M. Schmitt schrieb in comp.lang.c++ ueber "Re: Function to find
: DayofWeek from a given date ?":
: > mktime will not correctly calculate the day of week for dates before Jan

: >
: > 1, 1970 (at least in DOS/WIN), every day will be Sunday (if only that
: > were true). The following code will work all the time.
: >
: [code removed]

: But your code will not work for things earlier than 11.1582 or later
: than 3200 ....

I've done it using a trick (it works fine in DOS):

- get the current DOS date (getdate)
- set the DOS date to the date I want to know the day of
week (setdate)
- get the new DOS date (getdate), which returns a field
containing the dayofweek !!
- restore the old DOS date.

Hope this helps...

--
------------------------------------------------------------------
| Thomas Armstrong Kobrich | tarm...@caleuche.dis.udec.cl |
| Concepcion - Chile | Universidad de Concepcion |
------------------------------------------------------------------

0 new messages