LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-22-2005, 01:28 PM   #1
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
Calculating the week number for american calendars


In american calendars the first day in a week is sunday. What week
was started on Dec. 26th 2004. Is it the first week of 2005 or the
last week of 2004?
Can anybody have a look in his local calendar?

I have tried it in Java with the GregorianCalendar and
Locale.US.

And how can this be done with c++?
I tried:


Code:
#include <ctime>
#include <iostream>
using namespace std;

// call with prog YEAR MOTH DATE
int main() {
	if (argc<4) return 0;

	struct tm _tm;
	time_t now = time(NULL);
	gmtime_r( &now, &_tm);
	_tm.tm_year = atoi(argv[1]) - 1900;
	_tm.tm_mon  = atoi(argv[2]) - 1;
	_tm.tm_mday = atoi(argv[3]);
	
	cout << _tm.tm_year << _tm.tm_mon << _tm.tm_mday << endl;
	time_t sec = mktime(&_tm);
	cout << "Seconds: " << sec << "  now: " << now << endl;
	
	char buf[8];
	size_t size = strftime(buf, 8, "%G%V", &_tm);
	
	cout << "Week: " << buf << endl;
}
I started this with LANG=en_US, and it prints

Code:
Week: 200452
Which is correct for european calendars.
 
Old 04-22-2005, 02:30 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here's the output of cal 12

Code:
       January               February                 March        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa 
                1  2       1  2  3  4  5  6          1  2  3  4  5
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    6  7  8  9 10 11 12
10 11 12 13 14 15 16   14 15 16 17 18 19 20   13 14 15 16 17 18 19
17 18 19 20 21 22 23   21 22 23 24 25 26 27   20 21 22 23 24 25 26
24 25 26 27 28 29 30   28 29                  27 28 29 30 31
31
        April                   May                   June         
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa 
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

        July                  August                September      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa 
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
       October               November               December       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa 
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31
 
Old 04-23-2005, 04:29 AM   #3
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Original Poster
Rep: Reputation: 35
Thanks for your reply. But what particular week started on Dec 26h 2004.
I guess it should be the last week of 2005.

Code:
       October               November               December
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6             1  2  3  4
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    5  6  7  8  9 10 11
10 11 12 13 14 15 16   14 15 16 17 18 19 20   12 13 14 15 16 17 18
17 18 19 20 21 22 23   21 22 23 24 25 26 27   19 20 21 22 23 24 25
24 25 26 27 28 29 30   28 29 30               26 27 28 29 30 31
31
My Java program looks like this:
Code:
	public static void calendarTest(){
		Locale.setDefault(Locale.GERMAN);
		//Locale.setDefault(Locale.UK);
		//Locale.setDefault(Locale.US);
		for (int date=25; date <=27; ++ date) {
			int year = 2004, month = 11;
			Calendar cal = new GregorianCalendar(year, month, date);
			
			DateFormat sdf = SimpleDateFormat.getDateInstance(DateFormat.SHORT);
			DateFormat df = new SimpleDateFormat("yyyyww");
			System.out.println("Date: " + sdf.format(cal.getTime()) 
					+ "  Week:" + df.format(cal.getTime()));
		}
	}
If you try different locales, you get fairly different results. If you try the GERMAN Locale you get:
Code:
Date: 25.12.04  Week:200452
Date: 26.12.04  Week:200452
Date: 27.12.04  Week:200453
The US Locale gives:

Code:
Date: 12/25/04  Week:200452
Date: 12/26/04  Week:200401
Date: 12/27/04  Week:200401
So i wonder if really the first week of 2005 started on Dec, 26th. The UK Locale differs from
the US locale that the week starts on Monday instead of sunday. But It also starts the 'new
year' in december.

I did not check the ISO standard for week calculation for now. But i think the weeks
are defined there as starting on monday.
 
Old 04-23-2005, 05:35 AM   #4
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Original Poster
Rep: Reputation: 35
I found another interessting thing:
In the GERMAN locale the first year-week 199801
started on Dec, 29th 1997. I guess this is ISO 8601 conform.

I guess my question is (yeah, sounds silly!): What is the
american standard for defining week numbers.
here are
some informations on this. But this:
Quote:
US Internal Revenue Service Weeks

The US IRS evidently clusters weeks into monthly 4/5-week Report Cycles, numbering the weeks as YYYYWW from Jan 1 or near that; details are unknown.
does not help very much.

Thanks for your convenience.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Evolution remote calendars blueplazma Linux - Software 0 11-17-2004 04:26 PM
Sharing calendars Clemente Linux - Software 4 05-10-2004 01:13 PM
American assholes.. Björneborg General 4 02-29-2004 09:03 AM
Calculating Surface Distance! john23 Programming 1 11-27-2003 01:45 PM
SCO is American GtkUser General 2 06-16-2003 08:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration