C Language Development Kit/Snippets/Miscellaneous/GetWeekNumber

From OptiWiki

Jump to: navigation, search
Description Return a short integer value representing the current week number of todays date.
Syntax
short GetWeekNumber( void );
Arguments None.
Returns short integer value representing the week number of todays date.
Remarks None.
Example
#include "lib.h"
#include <stdio.h>
 
// Returns the week number of todays date
short GetWeekNumber( void )
{
    unsigned long ulF, ulS, ulT;
    unsigned long m, y, a;
    unsigned long ulJDN;
    struct date d = {0};
 
    getdate( &d );
 
    a = (14UL - d.da_mon ) / 12UL;
    y = d.da_year + 4800UL - a;
    m = d.da_mon + (12UL*a) - 3UL;
 
    ulJDN = d.da_day + (( 153UL * m + 2UL) / 5UL) + (365UL * y) + (y / 4UL) - (y / 100UL) + (y / 400UL) - 32045UL;
 
    ulF = ( ulJDN + 31741UL - ( ulJDN % 7UL )) % 146097UL % 36524UL % 1461UL;
    ulS = ulF / 1460UL;
    ulT = (( ulF - ulS ) % 365UL) + ulS;
 
    return (short)((ulT / 7UL) + 1UL);
}
 
void main( void )
{
    struct date d = {0};
 
    putchar('\f');
    for(;;)
    {
        getdate( &d );
        gotoxy( 0, 0);
        printf("%02d-%02d-%04d  \nWeek number %d", d.da_day, d.da_mon, d.da_year, GetWeekNumber() );
 
        delay( 250 ); // wait 5 seconds before update
    }
}


Views
Personal tools