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

Discussion in 'C Programming' started by Matt, Nov 8, 2003.

1. ### MattGuest

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

Matt, Nov 8, 2003

2. ### Richard HeathfieldGuest

Matt wrote:

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

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.

--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Richard Heathfield, Nov 8, 2003

3. ### Simon BiberGuest

"Richard Heathfield" <> wrote:
> 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!

--
Simon.

Simon Biber, Nov 8, 2003
4. ### Richard HeathfieldGuest

Simon Biber wrote:

> "Richard Heathfield" <> wrote:
>> 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.

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

(Ref: 7.23.2.3)

--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Richard Heathfield, Nov 8, 2003