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

    Matt Guest

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

    please advise!
     
    Matt, Nov 8, 2003
    #1
    1. Advertising

  2. 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 :
    "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
    #2
    1. Advertising

  3. Matt

    Simon Biber Guest

    "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
    #3
  4. 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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    605
    Matthew Speed
    Nov 8, 2003
  2. Matt
    Replies:
    2
    Views:
    519
    Pete Becker
    Nov 8, 2003
  3. Matt
    Replies:
    11
    Views:
    407
    Aaron Bertrand [MVP]
    Nov 8, 2003
  4. Chuck Remes
    Replies:
    7
    Views:
    211
    Gary Wright
    Feb 9, 2010
  5. James Edward Gray II

    [ANN] Red Dirt RubyConf CFP Ending in One Week

    James Edward Gray II, Feb 21, 2010, in forum: Ruby
    Replies:
    0
    Views:
    89
    James Edward Gray II
    Feb 21, 2010
Loading...

Share This Page