gmtime_r or strftime problem or just my code?????

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eskgwin@gmail.com

    gmtime_r or strftime problem or just my code?????

    I have this piece of code that takes a day of the year and converts it
    to the month and day of month:

    char start_mon[40];
    char start_day[40];
    int s_day = 84;
    time_t daysecs = (s_day * 86400) - 1;
    struct tm tm_result;
    struct tm *tm_time = &tm_result;
    tm_time = gmtime_r(&dayse cs,&tm_result);

    strftime(start_ mon, sizeof(start_mo n), (char*)"%m", tm_time);
    strftime(start_ day, sizeof(start_da y), (char*)"%d", tm_time);

    I am just testing it with 84, but this is a parameter to the method.
    Anyway, the month and day should be 03 24, but I am getting 03 25. It
    seems like it is not taking into account leap year. Is it gmtime_r or
    strftime or something else? Do I need to actually check the year and
    figure out if it is a leap year? I was hoping there was something that
    would do that automatically, but probably not. Thanks.

    Allyson
  • Victor Bazarov

    #2
    Re: gmtime_r or strftime problem or just my code?????

    eskgwin@gmail.c om wrote:
    I have this piece of code that takes a day of the year and converts it
    to the month and day of month:
    >
    char start_mon[40];
    char start_day[40];
    int s_day = 84;
    time_t daysecs = (s_day * 86400) - 1;
    struct tm tm_result;
    struct tm *tm_time = &tm_result;
    tm_time = gmtime_r(&dayse cs,&tm_result);
    >
    strftime(start_ mon, sizeof(start_mo n), (char*)"%m", tm_time);
    strftime(start_ day, sizeof(start_da y), (char*)"%d", tm_time);
    >
    I am just testing it with 84, but this is a parameter to the method.
    Anyway, the month and day should be 03 24, but I am getting 03 25. It
    seems like it is not taking into account leap year. Is it gmtime_r or
    strftime or something else? Do I need to actually check the year and
    figure out if it is a leap year? I was hoping there was something that
    would do that automatically, but probably not. Thanks.
    What's "gmtime_r"?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Victor Bazarov

      #3
      Re: gmtime_r or strftime problem or just my code?????

      eskgwin@gmail.c om wrote:
      On Apr 7, 10:51 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
      >eskg...@gmail. com wrote:
      >>I have this piece of code that takes a day of the year and converts
      >>it to the month and day of month:
      >>
      >>char start_mon[40];
      >>char start_day[40];
      >>int s_day = 84;
      >>time_t daysecs = (s_day * 86400) - 1;
      >>struct tm tm_result;
      What is this for?
      >>struct tm *tm_time = &tm_result;
      Why do you need to initialise 'tm_time' just to lose the value and get
      a new one in the next statement?
      >>tm_time = gmtime_r(&dayse cs,&tm_result);
      >>
      >>strftime(star t_mon, sizeof(start_mo n), (char*)"%m", tm_time);
      >>strftime(star t_day, sizeof(start_da y), (char*)"%d", tm_time);
      >>
      >>I am just testing it with 84, but this is a parameter to the method.
      >>Anyway, the month and day should be 03 24, but I am getting 03 25.
      Have you tried subtracting more than 1 when calculating 'daysecs'?
      Put it in a loop and see when you get a different value. Just curiuos
      because it seems that day 0 would actually mean that 'daysecs' is -1,
      which doesn't seem right.
      >>It seems like it is not taking into account leap year. Is it
      >>gmtime_r or strftime or something else? Do I need to actually check
      >>the year and figure out if it is a leap year? I was hoping there
      >>was something that would do that automatically, but probably not.
      >>Thanks.
      >>
      >What's "gmtime_r"?
      >>
      >V
      >--
      >Please remove capital 'A's when replying by e-mail
      >I do not respond to top-posted replies, please don't ask- Hide
      >quoted text -
      >>
      >- Show quoted text -
      >
      >
      gmtime_r() -- Convert Time (Restartable)
      [..]
      So, it's exacly like 'gmtime', only the standard function does not
      define 'time_t' type as the number of seconds since some date. There
      is no requirement that 'time_t' is implemented in seconds.
      >
      Calendar time is the number of seconds that have elapsed since EPOCH,
      which is 00:00:00, January 1, 1970 Universal Coordinate Time (UTC).
      Well, this is apparently the definition on *your* system, it is not
      the general definition, just so that you know...

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • eskgwin@gmail.com

        #4
        Re: gmtime_r or strftime problem or just my code?????

        On Apr 7, 11:10 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        eskg...@gmail.c om wrote:
        On Apr 7, 10:51 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        eskg...@gmail.c om wrote:
        >I have this piece of code that takes a day of the year and converts
        >it to the month and day of month:
        >
        >char start_mon[40];
        >char start_day[40];
        >int s_day = 84;
        >time_t daysecs = (s_day * 86400) - 1;
        >struct tm tm_result;
        >
        What is this for?
        >
        >struct tm *tm_time = &tm_result;
        >
        Why do you need to initialise 'tm_time' just to lose the value and get
        a new one in the next statement?
        >
        >tm_time = gmtime_r(&dayse cs,&tm_result);
        >
        >strftime(start _mon, sizeof(start_mo n), (char*)"%m", tm_time);
        >strftime(start _day, sizeof(start_da y), (char*)"%d", tm_time);
        >
        >I am just testing it with 84, but this is a parameter to the method.
        >Anyway, the month and day should be 03 24, but I am getting 03 25.
        >
        Have you tried subtracting more than 1 when calculating 'daysecs'?
        Put it in a loop and see when you get a different value.  Just curiuos
        because it seems that day 0 would actually mean that 'daysecs' is -1,
        which doesn't seem right.
        >
        >
        >
        >
        >
        >It seems like it is not taking into account leap year. Is it
        >gmtime_r or strftime or something else? Do I need to actually check
        >the year and figure out if it is a leap year? I was hoping there
        >was something that would do that automatically, but probably not.
        >Thanks.
        >
        What's "gmtime_r"?
        >
        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask- Hide
        quoted text -
        >
        - Show quoted text -
        >
        gmtime_r() -- Convert Time (Restartable)
        [..]
        >
        So, it's exacly like 'gmtime', only the standard function does not
        define 'time_t' type as the number of seconds since some date.  There
        is no requirement that 'time_t' is implemented in seconds.
        >
        >
        >
        Calendar time is the number of seconds that have elapsed since EPOCH,
        which is 00:00:00, January 1, 1970 Universal Coordinate Time (UTC).
        >
        Well, this is apparently the definition on *your* system, it is not
        the general definition, just so that you know...
        >
        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask- Hide quoted text-
        >
        - Show quoted text -
        What is the general definition? Maybe I have it wrong.

        I changed it to this:

        char start_mon[40];
        char start_day[40];
        int s_day = 84;

        time_t daysecs = (s_day * 86400);
        struct tm tm_result;
        struct tm *tm_time = &tm_result;
        gmtime_r(&dayse cs,&tm_result);

        strftime(start_ mon, sizeof(start_mo n), (char*)"%m", tm_time);
        strftime(start_ day, sizeof(start_da y), (char*)"%d", tm_time);

        and get these results:

        tm_time->tm_mday = 26
        tm_time->tm_mon = 2
        tm_time->tm_yday = 84
        tm_time->tm_isdst = 0
        start_mon = 03
        start_day = 26


        Maybe I am going about it the wrong way. You would think it would be
        easy. I have a number of days since Jan. 1 of the year I am in (2008
        in this case). I just want to find the month/day of that year. I guess
        I will have to find out if it is a leap year or not.


        Is there an easier way to do this? I am open to all criticisms and/or
        suggestions. Thanks.

        Allyson

        Comment

        • Victor Bazarov

          #5
          Re: gmtime_r or strftime problem or just my code?????

          eskgwin@gmail.c om wrote:
          [..time_t is not defined in terms of seconds..]
          What is the general definition? Maybe I have it wrong.
          You have it specific to your platform. time_t is an arithmetic
          type, but it isn't necessarily the number of seconds, generally.
          I changed it to this:
          >
          char start_mon[40];
          char start_day[40];
          int s_day = 84;
          >
          time_t daysecs = (s_day * 86400);
          struct tm tm_result;
          struct tm *tm_time = &tm_result;
          gmtime_r(&dayse cs,&tm_result);
          >
          strftime(start_ mon, sizeof(start_mo n), (char*)"%m", tm_time);
          strftime(start_ day, sizeof(start_da y), (char*)"%d", tm_time);
          >
          and get these results:
          >
          tm_time->tm_mday = 26
          tm_time->tm_mon = 2
          tm_time->tm_yday = 84
          tm_time->tm_isdst = 0
          What's tm_time->tm_year? Maybe this will open your eyes...
          start_mon = 03
          start_day = 26
          >
          >
          Maybe I am going about it the wrong way.
          What year do you get? You want to get the current year, don't you?
          You would think it would be
          easy. I have a number of days since Jan. 1 of the year I am in (2008
          in this case). I just want to find the month/day of that year. I guess
          I will have to find out if it is a leap year or not.
          You have the number of days since Jan 1st 2008. 'gmtime' needs the
          number of seconds since Jan 1st 1970. Where did the 38 years go?
          Did you just omit them? You know, thirty-eight years is, like, half
          a live of an average human; you can't just discount that... :-)
          Is there an easier way to do this? I am open to all criticisms and/or
          suggestions. Thanks.
          You're almost there. You just have to understand what exactly your
          'gmtime_r' gives you if you supply (number * 86400) to it. What is
          the meaning of the number?

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask


          Comment

          • eskgwin@gmail.com

            #6
            Re: gmtime_r or strftime problem or just my code?????

            On Apr 7, 11:56 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            eskg...@gmail.c om wrote:
            [..time_t is not defined in terms of seconds..]
            What is the general definition? Maybe I have it wrong.
            >
            You have it specific to your platform.  time_t is an arithmetic
            type, but it isn't necessarily the number of seconds, generally.
            >
            >
            >
            >
            >
            I changed it to this:
            >
            char start_mon[40];
            char start_day[40];
            int s_day = 84;
            >
            time_t daysecs = (s_day * 86400);
            struct tm tm_result;
            struct tm *tm_time = &tm_result;
            gmtime_r(&dayse cs,&tm_result);
            >
            strftime(start_ mon, sizeof(start_mo n), (char*)"%m", tm_time);
            strftime(start_ day, sizeof(start_da y), (char*)"%d", tm_time);
            >
            and get these results:
            >
            tm_time->tm_mday = 26
            tm_time->tm_mon = 2
            tm_time->tm_yday = 84
            tm_time->tm_isdst = 0
            >
            What's tm_time->tm_year?  Maybe this will open your eyes...
            >
            start_mon = 03
            start_day = 26
            >
            Maybe I am going about it the wrong way.
            >
            What year do you get?  You want to get the current year, don't you?
            >
            You would think it would be
            easy. I have a number of days since Jan. 1 of the year I am in (2008
            in this case). I just want to find the month/day of that year. I guess
            I will have to find out if it is a leap year or not.
            >
            You have the number of days since Jan 1st 2008.  'gmtime' needs the
            number of seconds since Jan 1st 1970.  Where did the 38 years go?
            Did you just omit them?  You know, thirty-eight years is, like, half
            a live of an average human; you can't just discount that... :-)
            >
            Is there an easier way to do this? I am open to all criticisms and/or
            suggestions. Thanks.
            >
            You're almost there.  You just have to understand what exactly your
            'gmtime_r' gives you if you supply (number * 86400) to it.  What is
            the meaning of the number?
            >
            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask- Hide quoted text-
            >
            - Show quoted text -
            I do get the correct year (2008). So, I have to supply it the number
            of seconds since Jan 1, 1970? That seems like a huge number. Is there
            a standard for it somewhere that is the correct number of seconds?
            Maybe then I can get it to work.

            Thanks for the help and advice. I hope I can get this soon. It is
            really bugging me.

            Allyson

            Comment

            • Victor Bazarov

              #7
              Re: gmtime_r or strftime problem or just my code?????

              eskgwin@gmail.c om wrote:
              On Apr 7, 11:56 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
              >eskg...@gmail. com wrote:
              >>[..time_t is not defined in terms of seconds..]
              >>What is the general definition? Maybe I have it wrong.
              >>
              >You have it specific to your platform. time_t is an arithmetic
              >type, but it isn't necessarily the number of seconds, generally.
              >>
              >>
              >>
              >>
              >>
              >>I changed it to this:
              >>
              >>char start_mon[40];
              >>char start_day[40];
              >>int s_day = 84;
              >>
              >>time_t daysecs = (s_day * 86400);
              >>struct tm tm_result;
              >>struct tm *tm_time = &tm_result;
              >>gmtime_r(&day secs,&tm_result );
              >>
              >>strftime(star t_mon, sizeof(start_mo n), (char*)"%m", tm_time);
              >>strftime(star t_day, sizeof(start_da y), (char*)"%d", tm_time);
              >>
              >>and get these results:
              >>
              >>tm_time->tm_mday = 26
              >>tm_time->tm_mon = 2
              >>tm_time->tm_yday = 84
              >>tm_time->tm_isdst = 0
              >>
              >What's tm_time->tm_year? Maybe this will open your eyes...
              >>
              >>start_mon = 03
              >>start_day = 26
              >>
              >>Maybe I am going about it the wrong way.
              >>
              >What year do you get? You want to get the current year, don't you?
              >>
              >>You would think it would be
              >>easy. I have a number of days since Jan. 1 of the year I am in (2008
              >>in this case). I just want to find the month/day of that year. I
              >>guess I will have to find out if it is a leap year or not.
              >>
              >You have the number of days since Jan 1st 2008. 'gmtime' needs the
              >number of seconds since Jan 1st 1970. Where did the 38 years go?
              >Did you just omit them? You know, thirty-eight years is, like, half
              >a live of an average human; you can't just discount that... :-)
              >>
              >>Is there an easier way to do this? I am open to all criticisms
              >>and/or suggestions. Thanks.
              >>
              >You're almost there. You just have to understand what exactly your
              >'gmtime_r' gives you if you supply (number * 86400) to it. What is
              >the meaning of the number?
              >>
              >V
              >--
              >Please remove capital 'A's when replying by e-mail
              >I do not respond to top-posted replies, please don't ask- Hide
              >quoted text -
              >>
              >- Show quoted text -
              >
              I do get the correct year (2008).
              You do? Really??? So, if you supply 0 as your 'daysecs' value,
              you get the year 2008? I strongly doubt it, considering the
              definition of 'gmtime_r' you've given.
              So, I have to supply it the number
              of seconds since Jan 1, 1970? That seems like a huge number. Is there
              a standard for it somewhere that is the correct number of seconds?
              I don't know.
              Maybe then I can get it to work.
              >
              Thanks for the help and advice. I hope I can get this soon. It is
              really bugging me.
              Well, there are probably other ways which would require you to do
              a bit more calculations than just multiplying by 86400 and calling
              a function. They might be more reliable and portable, of course.
              The sequence 0,31,59,90,120, 151,... is the number of days from the
              beginning of the year to the first of the respective month. This
              requires a correction for a leap year (and for the next ninety
              years you can simply use !(year % 4) as your leap year indicator.
              So, you could subtract the numbers in the sequence to learn the
              month (while adjusting for the leap) and the difference would give
              you the day of the month (less one). Should be relatively easy to
              implement. Try it. There is one loop, one 'if', probably. One
              minus, two pluses. No need to multiply.

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              • Jerry Coffin

                #8
                Re: gmtime_r or strftime problem or just my code?????

                In article <d046102a-3266-41f4-ac7d-96a289872bd3
                @a1g2000hsb.goo glegroups.com>, eskgwin@gmail.c om says...
                I have this piece of code that takes a day of the year and converts it
                to the month and day of month:
                Presumably you want it for the current year. In this case, I'd start by
                using time() to get the current time. I'd then convert it to a struct tm
                using localtime. After that, change the month to 0, and the day of month
                to the day in the year you want to convert. Call mktime on the resulting
                struct tm.

                If this is successful, the tm_yday member should be set to the value you
                originally put into the tm_mday member and mktime should return a value
                other than (time_t)-1. tm_mon and tm_mday should now be set to their
                correct values.

                --
                Later,
                Jerry.

                The universe is a figment of its own imagination.

                Comment

                Working...