|
Kimpa Tammas
Registered User
Join date: 25 Feb 2007
Posts: 3
|
01-14-2008 14:05
I try to calculate the week number in a year (i.e. week #2 for the january 14 or week #11 for the march 16). I make this script for this: list daysPerMonth; integer year = 2008; integer month = 1; integer day = 13; default { state_entry() { // Days per month if (year % 4 == 0) { // If leap year daysPerMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; } else { daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; } // Total of days since year begining float temp = 0.0; integer i; for (i = 0; i < month - 1; i++) { // Months increment temp += llList2Integer(daysPerMonth, i); // Add number of days } // Add remaning days for the current month temp += day; // Convert to week temp = (temp / 7.0) + 1; llSay(0, "Week #" + (string)llFloor(temp)); } } This script works for the current year (200  , but doesn't works for some months of 2009. If someone can help me...
|
|
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
|
01-15-2008 04:22
The problem seems to be, that you don't count the fact, that the 1st of a month isn't always a monday... I added another global var that indicates which weekday is the first day of the year... This _might_ solve the problem - I only did a few tests, though... Also, I wondered why you made your var «temp» a float - an integer would do fine (I changed this as well)
list daysPerMonth; integer year = 2009; integer month = 2; integer day = 2; integer startingDay = 4; // in 2009 the 1st of Januar is a Thursday -> the 4th day in this week
default { state_entry() { // Days per month if (year % 4 == 0) { // If leap year daysPerMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; } else { daysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; } // Total of days since year begining integer temp = 0; integer i; for (i = 0; i < month - 1; i++) { // Months increment temp += llList2Integer(daysPerMonth, i); // Add number of days } // Add remaning days for the current month temp += day + startingDay; // Add the starting Day to the calculation // Convert to week temp = (temp / 7) + 1; llSay(0, "Week #" + (string)llFloor(temp)); } }
|
|
Beverly Ultsch
Registered User
Join date: 6 Sep 2007
Posts: 229
|
01-15-2008 13:52
Im sorry but i dont have time to do the code just at the moment. But you need to takei nto account two things. 1) Week one is the fisrst week that contaiins a Thursday, so 2010 week one starts on the fourth of January the other 3 days are week 53 of 2009. So 2009 is a 53 week year. 2) A leap year can not be calculated by dividing by 4, if i remember correctly it is a leap year if it is divisiable by 4, but not if it can also be divided by a hundred, unless it can be divided by a 1,000. So 2,000 was a leap year, but 2,400 will not be. I will try and post some code later if no one beats me to it 
|
|
Kimpa Tammas
Registered User
Join date: 25 Feb 2007
Posts: 3
|
Calculate week number
01-15-2008 15:37
Thanks a lot for those informations. I wait your code.
|
|
Beverly Ultsch
Registered User
Join date: 6 Sep 2007
Posts: 229
|
01-17-2008 05:04
Code as promised, this one seems to work integer getWeek(integer year, integer month, integer day) {
//First calculate the Julien day number integer a = (14 - month) / 12; integer y = year + 4800 - a; integer m = month + 12 * a - 3; integer JD = day + (153 * m + 2) / 5 + y * 365 + y / 4 - y / 100 + y / 400 - 32045;
//Then find the week number integer d4 = (JD + 31741 - (JD % 7)) % 146097 % 36524 % 1461; integer L = d4 / 1460; integer d1 = ((d4 - L) % 365) + L; integer WeekNumber = d1 / 7 + 1; return WeekNumber; }
edit: it can probably be optimised, it was a cut and paste job
|
|
Kimpa Tammas
Registered User
Join date: 25 Feb 2007
Posts: 3
|
Calculate week number
01-17-2008 08:53
Thanks to have help me Beverly! This script works good now. I don't think that I would make this script myself...
|
|
Beverly Ultsch
Registered User
Join date: 6 Sep 2007
Posts: 229
|
01-17-2008 09:42
I take no credit for the maths i just converted it the sourc is here http://webexhibits.org/calendars/week.htmland my spelling is dreadfull
|