[comp.lang.pascal] weeknumber

dvllhd@cs.umu.se (Lars H{gglund) (03/25/91)

Hi!
I'm looking for an algoritm or code to get the weeknumber
from a date.
ex: 1990-12-31 ==> 1

Thanks!!!
---------------------------------------------------------------
Lars Haegglund / Laser           I    Assembler ???!!!
email: dvllhd@zeus.cs.umu.se     I    Does that still exist ??
---------------------------------------------------------------

ts@uwasa.fi (Timo Salmi) (03/25/91)

In article <1991Mar24.195537.9726@cs.umu.se> dvllhd@cs.umu.se (Lars H{gglund) writes:
>I'm looking for an algoritm or code to get the weeknumber
>from a date.
>ex: 1990-12-31 ==> 1

See weeknrfn in tsunte unit in the /pc/ts/tspa23##.arc (## =
40,50,55,60) Turbo Pascal units collection available from our site. 

...................................................................
Prof. Timo Salmi        
Moderating at garbo.uwasa.fi anonymous ftp archives 128.214.12.37
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

einstein@cs.mcgill.ca (Michael CHOWET) (03/30/91)

In article <1991Mar24.195537.9726@cs.umu.se> dvllhd@cs.umu.se (Lars H{gglund) writes:

>I'm looking for an algoritm or code to get the weeknumber from a date.
>ex: 1990-12-31 ==> 1

  That ought to be simple enough. Just offhand, the following comes to
mind:

  Initialize an array of integers, 12 long. Each entry consists of how many
days have elapsed that year till the 1st of that month.

  Now, the day of the week would be:

(( 365 * year )+( year DIV 4 )+( Month_Table [month] )+day+fugefactor)div 7 + 1

  Where the fudgefactor is the amount you'd have to slide the whole thing
over, or if you want to think more meaningfully, it'd be the day of the
week of Jan 1, 0, if the Gregorian calendar had been around then.

  Note, that this will not work for dates before Pope Gregory modified the
calendar (but who wants to know about the middle ages, anyways, right? :)
Oh, note it might get outdated some few generations from now, since I heard 
talk of an additional day added every 2300 years....:)

  Hmm, come to think of it, when I last tried it, I overflowed. You might
have to use LongInt (Turbo) or hope your compiler gives you 31 bits, or
calculate the terms separately, mod-ing by 7, THEN add them.

d90-awe@dront.nada.kth.se (Assar Westerlund) (04/01/91)

In article <1991Mar24.195537.9726@cs.umu.se> dvllhd@cs.umu.se (Lars H{gglund) writes:

>I'm looking for an algoritm or code to get the weeknumber from a date.
>ex: 1990-12-31 ==> 1

Use Zeller's Congruence:

q + ((m + 1) / 10) + K + K / 4 + J / 4 - 2 * J.

Where

	q = Day of the month
	m = Month, but January and february are month #13 and #14 of prev year
	j = Century
	k = Year

Take the result MOD 7 and you'll have the day of the week.

Observe that it's MOD and not remainder what is what Turbo Pascal's operator
MOD implements.

For details, see DDJ Oct and Nov 1990.






################################################################################
Assar Westerlund
Address: d90-awe@nada.kth.se
################################################################################