• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

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?
*/
0
bganoush
Asked:
bganoush
  • 2
  • 2
1 Solution
 
brettmjohnsonCommented:
Do you have any questions?
0
 
bganoushAuthor Commented:

Um... no... thanks for the help!
0
 
brettmjohnsonCommented:
Accepted? I am a bit confused.


0
 
bganoushAuthor Commented:

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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now