Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Given a date, how to find the beginning date and ending date of that week (http://www.velocityreviews.com/forums/t316116-given-a-date-how-to-find-the-beginning-date-and-ending-date-of-that-week.html)

Matt 11-08-2003 08:01 PM

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

please advise!

Richard Heathfield 11-08-2003 08:30 PM

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

> 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.


--
Richard Heathfield : binary@eton.powernet.co.uk
"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

Simon Biber 11-08-2003 08:51 PM

Re: Given a date, how to find the beginning date and ending date of that week
 
"Richard Heathfield" <dontmail@address.co.uk.invalid> 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.



Richard Heathfield 11-08-2003 09:07 PM

Re: Given a date, how to find the beginning date and ending date of that week
 
Simon Biber wrote:

> "Richard Heathfield" <dontmail@address.co.uk.invalid> 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 : binary@eton.powernet.co.uk
"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


All times are GMT. The time now is 06:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57