|
-
Nov 11th, 2002, 09:43 AM
#1
Thread Starter
Stuck in the 80s
[Resolved] Date Stuff
How can I get last monday's date?
Specifically, I want something like:
Code:
if (today == friday || today == saturday || today == sunday) {
//get last monday
} else {
//if today is monday, tuesday, wednesday, etc. get two mondays ago
}
Any ideas?
Last edited by The Hobo; Nov 11th, 2002 at 04:04 PM.
-
Nov 11th, 2002, 01:06 PM
#2
Frenzied Member
This shows you how to get the day of the week for any date
from Jan 1, 1900 on.
difftime returns the number of seconds between to time_t time values.
You can use that and mktime to make dates in the past.
Code:
#include <time.h>
#include <string.h>
/* function to return day of the week using input date format MM-DD-YYYY*/
char * weekday(char *dest, char *date){
struct tm t;
int i;
time_t time_of_day;
char tmp[20];
char *buf;
for(buf=date,i=0;i<6;i++,buf++);
t.tm_year=1900-atoi(buf);
strcpy(tmp,date);
for(buf=tmp,i=0;i<3;i++,buf++);
*(buf+2)=0x00;
t.tm_mon=atoi(buf);
buf=tmp;
*(buf+2)=0x00;
t.tm_mday=atoi(buf)-1;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
t.tm_isdst=0;
time_of_day=mktime(&t);
memset(tmp,0x00,sizeof(tmp));
strcpy(tmp,ctime(&time_of_day));
strcpy(dest,tmp);
for(buf=dest;*buf!=0x20;buf++);
*buf=0x00;
return dest;
}
char *weekday(char *,char *);
int main(){
char result[10];
printf("%s\n",weekday(result,"04-04-2001") );
return 0;
}
-
Nov 11th, 2002, 01:16 PM
#3
Frenzied Member
Oh.
'Today' is:
Code:
char *today(char *dest){
time_t lt;
struct tm *ptr;
lt=time(NULL);
ptr=localtime(<);
strftime(dest,"%d-%m-%Y",ptr);
return dest;
}
Look up the strftime function to format your dates the way you want.
-
Nov 11th, 2002, 01:54 PM
#4
Thread Starter
Stuck in the 80s
Thanks for your help thus far, Jim.
I know how to get today's date and how to format the date (I actually learned from some of your old posts a few months ago), but how do I get last Monday's date?
I even know how to find out the day (as in Tuesday), and I can even tell how many days ago last Monday was. But where to go from there?
I can't simply subtract that number from a date variable.
Any ideas on this?
-
Nov 11th, 2002, 02:10 PM
#5
Thread Starter
Stuck in the 80s
That first code you provided just bombs out when I try it 
It executes up until this line:
Code:
strcpy(tmp,ctime(&time_of_day));
Last edited by The Hobo; Nov 11th, 2002 at 02:13 PM.
-
Nov 11th, 2002, 02:36 PM
#6
Thread Starter
Stuck in the 80s
in the time.h header file, I looked at this:
Code:
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
tm_wday looks like it might hold the key, but how do I access that?
-
Nov 11th, 2002, 02:54 PM
#7
Thread Starter
Stuck in the 80s
This is what I have so far:
Code:
#include <time.h>
#include <iostream>
using namespace std;
void main()
{
char tmpbuf[128];
time_t ltime;
struct tm *today;
int days, diff;
time(<ime);
today = localtime(<ime);
strftime(tmpbuf, 128,"%w", today);
days = atoi(tmpbuf);
switch (days) {
case 0: //sunday
diff = 6; break;
case 1: //monday
diff = 7; break;
case 2: //tuesday
diff = 8; break;
case 3: //wednesday
diff = 9; break;
case 4: //thursday
diff = 10;
case 5: //friday
diff = 4; break;
case 6: //saturday
diff = 5; break;
}
cout << diff << endl << endl;
//need to subtract diff from today to get
//last monday's date.
}
Is what I have so far.
-
Nov 11th, 2002, 02:55 PM
#8
Frenzied Member
The code runs fine for me... both unix and Borland.
...maybe you need _Ctime or some other MS malapropism.

subtracting days from a date:
Code:
#include <stdio.h>
#include <time.h>
#define DAY 24*3600
time_t date_subtract(time_t, int);
int main(){
time_t now, then;
now=time(NULL);
then = date_subtract(now, 5); /* five days ago */
printf(ctime(&then));
return 0;
}
time_t date_subtract(time_t now, int days){
time_t tmp;
tmp=days * DAY;
return difftime(now,tmp);
}
-
Nov 11th, 2002, 02:59 PM
#9
Thread Starter
Stuck in the 80s
Got it:
Code:
#include <time.h>
#include <iostream>
using namespace std;
void main()
{
char tmpbuf[128];
time_t ltime;
struct tm *today;
struct tm when;
time_t now, result;
int days, diff;
time(<ime);
when = *localtime( <ime );
today = localtime(<ime);
strftime(tmpbuf, 128,"%w", today);
days = atoi(tmpbuf);
switch (days) {
case 0: //sunday
diff = 6; break;
case 1: //monday
diff = 7; break;
case 2: //tuesday
diff = 8; break;
case 3: //wednesday
diff = 9; break;
case 4: //thursday
diff = 10;
case 5: //friday
diff = 4; break;
case 6: //saturday
diff = 5; break;
}
when.tm_mday = when.tm_mday - diff;
mktime( &when );
cout << asctime( &when ) << endl << endl;
}
Edit: Little more simplified:
Code:
#include <time.h>
#include <iostream>
using namespace std;
void main()
{
char tmpbuf[128];
time_t atime;
struct tm *today;
struct tm when;
int diff;
time(&atime);
when = *localtime( &atime );
today = localtime(&atime);
strftime(tmpbuf, 128,"%w", today);
diff = atoi(tmpbuf);
if (diff > 4) {
diff = diff - 1;
} else {
diff = diff + 6;
}
when.tm_mday = when.tm_mday - diff;
mktime( &when );
cout << "Last Monday: " << asctime( &when ) << endl << endl;
}
Last edited by The Hobo; Nov 11th, 2002 at 03:07 PM.
-
Nov 11th, 2002, 03:07 PM
#10
Frenzied Member
Try dummying up your code for Dec 1 (or set your system time forward). It won't work. tm_days will be one, subtract 5, gives minus 4. The results from that are undefined, I believe.
Use difftime.
-
Nov 11th, 2002, 03:11 PM
#11
Thread Starter
Stuck in the 80s
Works fine, unless I did the test wrong:
Code:
#include <time.h>
#include <iostream>
using namespace std;
void main()
{
char tmpbuf[128];
time_t atime;
struct tm *today;
struct tm when;
int diff;
time(&atime);
when = *localtime( &atime );
today = localtime(&atime);
strftime(tmpbuf, 128,"%w", today);
diff = atoi(tmpbuf);
if (diff > 4) {
diff = diff - 1;
} else {
diff = diff + 6;
}
when.tm_mday = 1; //when.tm_mday - diff;
when.tm_mon = 11;
mktime( &when ); //december 1st
cout << asctime( &when ) << endl << endl;
when.tm_mday = when.tm_mday - 5;
mktime( &when ); //december 1st minus 5 days
cout << asctime( &when ) << endl << endl;
}
Gives output:
Sun Dec 01 15:11:26 2002
Tue Nov 26 15:11:26 2002
Press any key to continue
-
Nov 11th, 2002, 04:31 PM
#12
Thread Starter
Stuck in the 80s
this is my final code, just incase anyone is wondering:
Code:
#include <time.h>
#include <iostream>
using namespace std;
tm lastmonday();
int main() {
tm when;
char date[128];
when = lastmonday();
strftime(date, 128, "%#x", &when);
cout << "Last Monday: " << date << endl << endl;
return 0;
}
//function to retrieve the date of last monday
tm lastmonday() {
time_t atime;
tm when;
int diff;
time(&atime);
when = *localtime(&atime);
diff = when.tm_wday;
if (diff > 4) {
diff = diff - 1;
} else {
diff = diff + 6;
}
when.tm_mday -= diff;
mktime(&when);
return when;
}
Thanks for your help, Jim.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|