Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/misc/weekno.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <bio.h>


enum {
	Min = 60,
	Hour = Min * 60,
	Day = Hour * 24,
	Week = Day * 7,
};


static int
weekstart(int year, int week)
{
	Tm *tp;
	long utc;

	tp = localtime(time(nil));
	if(year == -1)
		year = tp->year;

	/* ISO 8600 defines week zero as the one including the 4th Jan */
	tp->year = year;
	tp->mon = 0;	
	tp->mday = 4;
	tp->hour = 12;
	tp->min = 0;
	tp->sec = 0;
	utc = tm2sec(tp);

	/* find the utc of the Sunday in week zero (NB: might be previous year) */ 
	tp = localtime(utc);
	utc -= tp->wday * Day;

	/* return utc of Sunday in the requested week */
	return utc + week * Week;
}


void
main(int argc, char *argv[])
{
	int i;
	Tm *tm;
	static char *months[] = {
		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
	};


	for(i = 0; i < 52; i++){
		tm = localtime(weekstart(-1, i));
		print("Wk %2d    %2d-%s-%d\n", i+1, tm->mday, months[tm->mon], tm->year + 1900);
	}

	exits(nil);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].