Hello

I am making a function that takes in a date (dd/mm/yy) & returns what week number of the year the date occurs on.

For eg;
If I enter: 01/01/09 then the week number of that year will be 1
If I enter: 04/09/09 then the week number of that year will be 35

I have this function I altered that gives the week number of the current date ONLY.
Is there a way to alter the below function where it takes in a parameter of a date(dd/mm/yy) & returns what number week that is of the year??

Ie, alter this function to take in a given date & return what week number that would be.

I hope that makes sense :P

void getWeek(int& week) {
     
    time_t rawtime;
	struct tm * timeinfo;
	char buffer[4];

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );

	strftime (buffer,4,"%W",timeinfo);   // '%W' = week number of the year, eg 1/1/09 == 1
	
    week = atoi(buffer);     // convert char to int
}

for example

void getWeek(string& date) {  // date format 'dd/mm/yy'
     
    time_t rawtime;
	struct tm * timeinfo;
	char buffer[4];

	string d = date[0] + date[1];
	string m = date[3] + date[4];
	string y = date[6] + date[7];
               
	time ( &rawtime );
	timeinfo = date; // is that  correct????

	strftime (buffer,4,"%W",timeinfo);   // '%W' = week number of the year, eg 1/1/09 == 1
	
    week = atoi(buffer);     // convert char to int
}

very similar, but you could fill in a struct tm with the date then call mktime() to fill in the rest, including the day-of-week. Make sure you memset the struct tm with all 0's before you start or mktime() will return an error condition.

very similar, but you could fill in a struct tm with the date then call mktime() to fill in the rest, including the day-of-week. Make sure you memset the struct tm with all 0's before you start or mktime() will return an error condition.

Thanks :)
I'm very new to this libray (ctime & time) would you be able to suggest how to code this?

Is this what you mean? :)

void getWeek(string date) {
     
                time_t rawtime;
	struct tm * date;      // date is a string tho?? 'dd/mm/yy'
	char buffer[4];

                mktime(date);          // correct syntax??

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );

	strftime (buffer,4,"%W",date);   
	
    week = atoi(buffer);     
}

>>// date is a string tho?? 'dd/mm/yy'
Your program will have to parse that string to extract month, day, and year. This is not shown in the code below.

struct tm tm; // do not user a pointer here!
memset(&tm, 0, sizeof(struct tm)); // set everything to 0s
// Lets say you want to get the day of week for 
// 5 Sep 2009 (or any other day)
tm.tm_year = 2009 - 1900;
tm.tm_mon = 8; // where 0 = Jan, 1 = Feb, etc
tm.tm_mday = 5;
time_t t = mktime(&tm);
// now, hopefully, you will have day of week in the tm structure

Cool thanks, :)

One thing tho, with your code, you said it should return the day of week, will my code below(your code altered :) ) give the week number not day of the week?

void getWeek(string& date) {
     
    char buffer[4];  
    
    struct tm tm; // do not user a pointer here!
    memset(&tm, 0, sizeof(struct tm)); // set everything to 0s
    
    // 
    string d = date[0] + date[1];
	string m = date[3] + date[4];
	string y = date[6] + date[7];
	
	int dd = atoi(d);
	int mm = atoi(m)-1;
	int yy = atoi(y);

    tm.tm_year = yy - 1900;
    tm.tm_mon = mm; // where 0 = Jan, 1 = Feb, etc
    tm.tm_mday = dd;
    time_t t = mktime(&tm); // Am I correct that  this will return the day of the week?
    // if so how would I  get the week number of the year
    
    // to get the week, would it be
    strftime (buffer,4,"%W",tm);  // instead of tm should it be 't'?
    
    int week = atoi(buffer);

}

read the structure here

Your code contains a few little bugs. For example: int dd = atoi(d.c_str());

read the structure here

Your code contains a few little bugs. For example: int dd = atoi(d.c_str());

Ah I see, to display the day I can just say the below & to get the week number I can just use some maths to get it.

cout << tm.tm_yday << endl; // eg today will be 279 (out of 365 days)

But one problem, :P again:

I am implementing the above code, but it is displaying todays values not the given date?? So when I write cout << tm.tm_yday with the input date 01/01/09, it should output 1 but it outputs 279 (todays day number).

int getWeek(int a, int b, int c) {  // where a = day, b=month, c=year

                // a = 1, b = 1, c = 2009 (01/01/2009)
	struct tm tm; // do not user a pointer here!
	memset(&tm, 0, sizeof(struct tm)); // set everything to 0s

	tm.tm_year = c - 1900;
	tm.tm_mon = b-1; 
	tm.tm_mday = a;
	time_t t = mktime(&tm); 

	cout << tm.tm_yday << endl;  // should output - 1  but it outputs todays day number - 279

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.