bganoush
asked on
Manipulating the day of week and getting the last day of week.
#include <stdio.h>
#include <time.h>
char szDays[7][4] = { "SUN","MON", "TUE", "WED", "THU", "FRI", "SAT" };
#define kSUNDAY 0
#define kMONDAY 1
#define kTUESDAY 2
#define kWEDNESDAY 3
#define kTHURSDAY 4
#define kFRIDAY 5
#define kSATURDAY 6
#define kWEEKINDAYS 7
#define kTENDAYS 10
char *dayofweekstr(int day,int month,int year)
{
static struct tm oldtime;
oldtime.tm_mday = day;
oldtime.tm_mon = month - 1;
oldtime.tm_year = year - 1900;
mktime(&oldtime);
return(szDays[oldtime.tm_w
}
int dayofweek(int day,int month,int year)
{
static struct tm oldtime;
oldtime.tm_mday = day;
oldtime.tm_mon = month - 1;
oldtime.tm_year = year - 1900;
mktime(&oldtime);
return(oldtime.tm_wday);
}
int dayofweektoday()
{
time_t clock;
time (&clock);
return(localtime(&clock)->
}
time_t findLast(int dow)
{
time_t clock;
time (&clock);
int todaydow = localtime(&clock)->tm_wday
int numDaysSinceLast = todaydow - dow;
if (numDaysSinceLast < 0) numDaysSinceLast += 7;
clock -= (numDaysSinceLast * 86400);
return (clock);
}
time_t findLastAtTime(int dow, int hour, int min, int sec)
{
time_t clock;
struct tm * lastTime;
time (&clock);
int todaydow = localtime(&clock)->tm_wday
int numDaysSinceLast = todaydow - dow;
if (numDaysSinceLast < 0) numDaysSinceLast += 7;
clock -= (numDaysSinceLast * 86400L);
lastTime = localtime(&clock);
lastTime->tm_hour = hour;
lastTime->tm_min = min;
lastTime->tm_sec = sec;
clock = mktime(lastTime);
return (clock);
}
time_t findPreviousNumDays(time_t
{
return(clock - (numDays * 86400L));
}
int main(int argc, char *argv[])
{
time_t clock;
char rightNow[20];
time_t clockLastMonday;
char lastWeek[20];
time_t clockLastWeek;
char* dow;
char lastMonday[20];
time_t clockPreviousMonday;
char previousMonday[20];
time (&clock);
strftime(rightNow, 20, "%Y-%m-%d-%H.%M.%S", localtime(&clock));
printf("The current date and time is: %s\n", rightNow);
clockLastWeek = findPreviousNumDays(clock,
strftime(lastWeek, 20, "%Y-%m-%d-%H.%M.%S", localtime(&clockLastWeek))
printf("Seven days ago to the second is: %s\n", lastWeek);
dow = dayofweekstr(localtime(&cl
printf("The day of the week is: %s\n", dow);
printf("Today's day of week is: %s\n", szDays[dayofweektoday()]);
clockLastMonday = findLast(kMONDAY);
strftime(lastMonday, 20, "%Y-%m-%d-%H.%M.%S", localtime(&clockLastMonday
printf("Last Monday was: %s\n", lastMonday);
clockLastMonday = findLastAtTime(kMONDAY, 1,00,00);
strftime(lastMonday, 20, "%Y-%m-%d-%H.%M.%S", localtime(&clockLastMonday
printf("Last Monday morning was: %s\n", lastMonday);
clockPreviousMonday = findPreviousNumDays(clockL
strftime(previousMonday, 20, "%Y-%m-%d-%H.%M.%S", localtime(&clockPreviousMo
printf("Previous Monday morning was: %s\n", previousMonday);
clockPreviousMonday = findPreviousNumDays(clockL
strftime(previousMonday, 20, "%Y-%m-%d-%H.%M.%S", localtime(&clockPreviousMo
printf("Ten days before the last Monday morning was: %s\n", previousMonday);
return(1);
}
/*
Any questions?
*/
Do you have any questions?
ASKER
Um... no... thanks for the help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No there was no question... This is an answer for another user who asked about this... I'm not sure who it is and where the question is but I posted the answer and then the message disappeared so I posted a new question with the answer on it...
Do what you want with this... the guy probably found the answer already.