Given a date, how to find the beginning date and ending date of that week

M

Matt

Given a date, how to find the beginning date and ending date of that week

please advise!
 
R

Richard Heathfield

Matt said:
Given a date, how to find the beginning date and ending date of that week

please advise!

Initialise a struct tm like this:

struct tm tmt = {0};

Populate the fields you know. You will need, at a minimum, the day, month,
and year. Months are 0-11, and you will need to subtract 1900 from the
year.

Pass to mktime(). That will give you a time_t.

Pass its address to localtime(), giving you a pointer to a struct tm.

Read that struct tm's tm_wday field, where 0 means Sunday and 6 means
Saturday. If it's, say, 3, you know it's Wednesday. Decide what you mean by
"beginning of week". If you think Sunday is the first day of the week, for
example, then you know you're three away from the beginning of the week. So
subtract three from the monthday in the struct tm, and pass it back to
mktime() to normalise it. Then you can pass it to localtime() - again! - to
get a more useful form. Use a similar technique for the end of the week.

Perhaps I'm making this more complicated than it need be, but I don't see a
way to make it shorter.
 
S

Simon Biber

Richard Heathfield said:
Initialise a struct tm like this:

struct tm tmt = {0};

Populate the fields you know. You will need, at a minimum, the day,
month, and year. Months are 0-11, and you will need to subtract 1900
from the year.
Yes.

Pass to mktime(). That will give you a time_t.

But you don't need the time_t. Throw it away.
Pass its address to localtime(), giving you a pointer to a struct tm.

This is entirely unnecessary. mktime fills in the required field
(tm_wday) through the pointer you gave it.
Read that struct tm's tm_wday field, where 0 means Sunday and 6 means
Saturday. If it's, say, 3, you know it's Wednesday. Decide what you
mean by "beginning of week". If you think Sunday is the first day of
the week, for example, then you know you're three away from the
beginning of the week. So subtract three from the monthday in the
struct tm, and pass it back to mktime() to normalise it.

Correct so far.
Then you can pass it to localtime() - again! - to get a more useful
form. Use a similar technique for the end of the week.

Again unnecessary. You can use the struct tm immediately after calling
mktime.
Perhaps I'm making this more complicated than it need be, but I don't
see a way to make it shorter.

A little shorter, yes:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv)
{
if(argc != 4)
{
puts("Require 3 args: YYYY MM DD");
}
else
{
char buf[81];
struct tm date = {
.tm_year = strtol(argv[1], 0, 0) - 1900,
.tm_mon = strtol(argv[2], 0, 0) - 1,
.tm_mday = strtol(argv[3], 0, 0)
};
mktime(&date);
strftime(buf, 80, "%Y-%m-%d is a %A\n", &date);
puts(buf);

/* You can adjust the next line if you don't consider
* Sunday as the first day of the week.
*/
date.tm_mday -= date.tm_wday;
mktime(&date);
strftime(buf, 80, "Start of week is %A %Y-%m-%d", &date);
puts(buf);

date.tm_mday += 6;
mktime(&date);
strftime(buf, 80, " End of week is %A %Y-%m-%d", &date);
puts(buf);
}
return 0;
}

No time_t used at all!
 
R

Richard Heathfield

Simon said:
But you don't need the time_t. Throw it away.

Ah, of course - it hacks the struct as it goes, which is a huge saving.
Silly of me. Thanks.

(Ref: 7.23.2.3)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,371
Latest member
TroyHursey

Latest Threads

Top