using localtime

Discussion in 'C Programming' started by Raj, Mar 2, 2005.

  1. Raj

    Raj Guest

    Hi

    I'm very new to C and am trying to get the time a month ago in the format
    mmyy.

    What I have tried so far is:

    char *last_mth;
    size_t maxsize = 4;
    struct tm *timeptr = localtime(time(0));
    strftime(last_mth, maxsize, "%m%y", timeptr);

    to get the current mmyy, but I don't know how to access the tm struct so
    that I can decrement the month.

    I know this is the simplest of things to do, but I haven't found it in the
    FAQ. If this is a silly request, please let me know. I'm off to look in
    tutorials in case they can tell me there.

    Thanks in advance for any help provided.

    Regards,
    --
    Raj Kothary :: one|concept
    http://www.oneconcept.net

    + 44 (0)79 5647 2746

    oneconcept limited :: 17 York Avenue, Stanmore, Middlesex HA7 2HT

    Confidentiality notice:
    The information transmitted in this email and/or any attached document(s) is
    confidential and intended only for the person or entity to which it is
    addressed and may contain privileged material. Any review, retransmission,
    dissemination or other use of, or taking of any action in reliance upon this
    information by persons or entities other than the intended recipient is
    prohibited. If you received this in error, please contact the sender and
    delete the material from any computer.
     
    Raj, Mar 2, 2005
    #1
    1. Advertising

  2. Raj wrote:

    > I'm very new to C and am trying to get the time a month ago in the format
    > mmyy.
    >
    > What I have tried so far is:
    >
    > char *last_mth;
    > size_t maxsize = 4;
    > struct tm *timeptr = localtime(time(0));
    > strftime(last_mth, maxsize, "%m%y", timeptr);
    >
    > to get the current mmyy, but I don't know how to access the tm struct so
    > that I can decrement the month.


    These are the members of struct tm:

    int tm_sec; // seconds after the minute -- [0, 60]
    int tm_min; // minutes after the hour -- [0, 59]
    int tm_hour; // hours since midnight -- [0, 23]
    int tm_mday; // day of the month -- [1, 31]
    int tm_mon; // months since January -- [0, 11]
    int tm_year; // years since 1900
    int tm_wday; // days since Sunday -- [0, 6]
    int tm_yday; // days since January 1 -- [0, 365]
    int tm_isdst; // Daylight Saving Time flag

    So to decrease the month value you simply do this:
    timeptr->tm_mon -= 1;
    The case where tm_mon is already zero is left as an exercise...


    Christian
     
    Christian Kandeler, Mar 2, 2005
    #2
    1. Advertising

  3. Raj

    Eric Sosman Guest

    Raj wrote:

    > Hi
    >
    > I'm very new to C and am trying to get the time a month ago in the format
    > mmyy.
    >
    > What I have tried so far is:
    >
    > char *last_mth;
    > size_t maxsize = 4;
    > struct tm *timeptr = localtime(time(0));


    Why did you ignore the compiler's complaint about
    this line? I was going to write you a nice explanation
    of what was going wrong and how to fix it -- in fact, I
    actually did write it -- but decided it would be wasted
    on someone who won't even accept help from his compiler.

    Please work on your code at least until the compiler
    accepts it without complaint, or until you get a complaint
    that you find baffling. Then post a minimal, complete
    program demonstrating the problem at hand -- once you've
    given evidence of having made an honest try, I for one
    will be happy to give help.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Mar 2, 2005
    #3
  4. Raj

    tigervamp Guest

    Raj wrote:
    > Hi
    >
    > I'm very new to C and am trying to get the time a month ago in the

    format
    > mmyy.
    >
    > What I have tried so far is:
    >
    > char *last_mth;
    > size_t maxsize = 4;
    > struct tm *timeptr = localtime(time(0));
    > strftime(last_mth, maxsize, "%m%y", timeptr);


    There are many mistakes with your code. Firstly, you pass an
    uninitialized pointer (last_mth) to strftime. Next, maxsize should be
    at least 5, strings in C are terminated with a nul character, you need
    to leave room for strftime to add this. The time function returns a
    value of type time_t, localtime takes a pointer to time_t, you are
    passing a time_t to localtime, not a pointer.

    Here is a working version (without error checking):

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

    int main (void) {
    char last_month[10];
    size_t length = sizeof(last_month)/sizeof(last_month[0]);
    time_t mytime = time(NULL);
    struct tm *timeptr = localtime(&mytime);

    strftime(last_month, length, "%m%y", timeptr);
    printf("%s\n", last_month);
    return 0;
    }

    > to get the current mmyy, but I don't know how to access the tm struct

    so
    > that I can decrement the month.


    The tm struct contains members tm_mday, tm_mon, and tm_year (among
    others) which are integers representing day of month, month of year,
    and year respectively, month is zero based. So, to answer your
    question, just decrement the tm_mon member of an initialized struct tm:

    timeptr->tm_mon--;

    Of course you will want to check if the current month is 0 (January)
    and if so you probably want to set the month to 11 (December) and
    decrement the year. Then you have to decide what to do if the day in
    the current month does not exist in the new month, etc.

    > I know this is the simplest of things to do, but I haven't found it

    in the
    > FAQ. If this is a silly request, please let me know. I'm off to

    look in
    > tutorials in case they can tell me there.


    You should try taking a look at your library documentation or pick up a
    good C book.

    > Thanks in advance for any help provided.
    >
    > Regards,
    > --
    > Raj Kothary :: one|concept
    > http://www.oneconcept.net
    >
    > + 44 (0)79 5647 2746


    Rob Gamble
     
    tigervamp, Mar 2, 2005
    #4
  5. Raj

    Al Bowers Guest

    Raj wrote:
    > Hi
    >
    > I'm very new to C and am trying to get the time a month ago in the format
    > mmyy.
    >
    > What I have tried so far is:
    >
    > char *last_mth;
    > size_t maxsize = 4;
    > struct tm *timeptr = localtime(time(0));
    > strftime(last_mth, maxsize, "%m%y", timeptr);
    >
    > to get the current mmyy, but I don't know how to access the tm struct so
    > that I can decrement the month.
    >


    Function localtime takes a pointer argument. Also, you
    need to first check the value returned from function time
    to make sure that the value represents time, then use a
    pointer to the time_t value should it represent a valid time.
    To reduce the month after the function localtime call, use:
    timeptr->tm_mon--; or timeptr->tm_mon -= 1;
    Then use function mktime to normalize the values in the struct
    tm. You can put all this in a function. Example below.

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

    char *MinusOneMonth(time_t tvalue)
    {
    struct tm *t;
    static char stime[5];

    if(tvalue != (time_t)-1)
    {
    t = localtime(&tvalue);
    t->tm_mon--;
    if((tvalue = mktime(t)) != (time_t)-1)
    {
    int tmp = t->tm_year%100;
    sprintf(stime,"%02d%02d",t->tm_mon+1,tmp);
    }
    }
    return tvalue==(time_t)-1?NULL:stime;
    }

    int main(void)
    {
    char *s;
    struct tm *t;
    time_t now = time(NULL);

    if((s = MinusOneMonth(now)) != NULL)
    {
    printf("Now is: %sOne month ago in "
    "format(mmyy) is: \"%s\" \n",ctime(&now), s);
    puts("\nTest for the month Jan\n");
    t = localtime(&now);
    t->tm_mon = 0; /* Makes it month January */
    now = mktime(t);
    if((s = MinusOneMonth(now)) != NULL)
    printf("Now is: %sOne month ago "
    "in format(mmyy) is: \"%s\"\n",ctime(&now), s);
    else puts("Time is not available");
    }
    else puts("Time is not available");
    return 0;
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Mar 2, 2005
    #5
  6. Raj

    tigervamp Guest

    Al Bowers wrote:
    > Raj wrote:
    > > Hi
    > >
    > > I'm very new to C and am trying to get the time a month ago in the

    format
    > > mmyy.
    > >
    > > What I have tried so far is:
    > >
    > > char *last_mth;
    > > size_t maxsize = 4;
    > > struct tm *timeptr = localtime(time(0));
    > > strftime(last_mth, maxsize, "%m%y", timeptr);
    > >
    > > to get the current mmyy, but I don't know how to access the tm

    struct so
    > > that I can decrement the month.
    > >

    >
    > Function localtime takes a pointer argument. Also, you
    > need to first check the value returned from function time
    > to make sure that the value represents time, then use a
    > pointer to the time_t value should it represent a valid time.
    > To reduce the month after the function localtime call, use:
    > timeptr->tm_mon--; or timeptr->tm_mon -= 1;
    > Then use function mktime to normalize the values in the struct
    > tm. You can put all this in a function. Example below.
    >
    > #include <stdio.h>
    > #include <time.h>
    >
    > char *MinusOneMonth(time_t tvalue)
    > {
    > struct tm *t;
    > static char stime[5];
    >
    > if(tvalue != (time_t)-1)
    > {
    > t = localtime(&tvalue);
    > t->tm_mon--;
    > if((tvalue = mktime(t)) != (time_t)-1)
    > {
    > int tmp = t->tm_year%100;
    > sprintf(stime,"%02d%02d",t->tm_mon+1,tmp);
    > }
    > }
    > return tvalue==(time_t)-1?NULL:stime;
    > }
    >
    > int main(void)
    > {
    > char *s;
    > struct tm *t;
    > time_t now = time(NULL);
    >
    > if((s = MinusOneMonth(now)) != NULL)
    > {
    > printf("Now is: %sOne month ago in "
    > "format(mmyy) is: \"%s\" \n",ctime(&now), s);
    > puts("\nTest for the month Jan\n");
    > t = localtime(&now);
    > t->tm_mon = 0; /* Makes it month January */
    > now = mktime(t);
    > if((s = MinusOneMonth(now)) != NULL)
    > printf("Now is: %sOne month ago "
    > "in format(mmyy) is: \"%s\"\n",ctime(&now), s);
    > else puts("Time is not available");
    > }
    > else puts("Time is not available");
    > return 0;
    > }


    This will produce incorrect results if the current day of the month
    does not exist in the previous month, for example if the current day is
    Dec 31, or Mar 30, etc.

    > --
    > Al Bowers
    > Tampa, Fl USA
    > mailto: (remove the x to send email)
    > http://www.geocities.com/abowers822/


    Rob Gamble
     
    tigervamp, Mar 2, 2005
    #6
    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. Stephen
    Replies:
    1
    Views:
    1,262
    Purl Gurl
    May 19, 2004
  2. Gerry George

    Time::localtime problems

    Gerry George, May 26, 2004, in forum: Perl
    Replies:
    2
    Views:
    1,183
    Joe Smith
    May 26, 2004
  3. Gerry George

    localtime problems with month

    Gerry George, May 26, 2004, in forum: Perl
    Replies:
    1
    Views:
    522
    Jeff Schwab
    May 26, 2004
  4. Datamon
    Replies:
    1
    Views:
    107
  5. John
    Replies:
    2
    Views:
    120
Loading...

Share This Page