Link to home
Start Free TrialLog in
Avatar of bganoush
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_wday]);
}

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)->tm_wday);
}

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 clock, int numDays)
{
      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, 7);
      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(&clock)->tm_mday, localtime(&clock)->tm_mon + 1, localtime(&clock)->tm_year + 1900);
      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(clockLastMonday, kWEEKINDAYS);
      strftime(previousMonday, 20, "%Y-%m-%d-%H.%M.%S", localtime(&clockPreviousMonday));
      printf("Previous Monday morning was: %s\n", previousMonday);
      
      clockPreviousMonday = findPreviousNumDays(clockLastMonday, kTENDAYS);
      strftime(previousMonday, 20, "%Y-%m-%d-%H.%M.%S", localtime(&clockPreviousMonday));
      printf("Ten days before the last Monday morning was: %s\n", previousMonday);
      
      return(1);
}

/*
Any questions?
*/
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Do you have any questions?
Avatar of bganoush
bganoush

ASKER


Um... no... thanks for the help!
ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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.