?
Solved

Manipulating the day of week and getting the last day of week.

Posted on 2006-05-18
6
Medium Priority
?
410 Views
Last Modified: 2010-10-05

#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
Comment
Question by:bganoush
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 16711988
Do you have any questions?
0
 

Author Comment

by:bganoush
ID: 16711992

Um... no... thanks for the help!
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 200 total points
ID: 16712070
Accepted? I am a bit confused.


0
 

Author Comment

by:bganoush
ID: 16884766

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question