adding a day to time_t

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abubakar

    #1

    adding a day to time_t

    Hi,
    Lets say I have three integers (date, month, and year) as part of a struct
    lets say like
    struct mydate{ int day, month, year};
    I want to add x number of days to this date. And it should be a real date
    calculation thing. Like if the date is 31st december 2008, when i add 1 to
    this, it should become 1st feb 2009.
    This of course may require converting the above struct values to some data
    type that crt's date functons can understand. I want to do that but havent
    been able to yet. Thanks for help.
    I'm using visual c++ 2k5.
    Regards,

    ...ab


  • Doug Harrison [MVP]

    #2
    Re: adding a day to time_t

    On Mon, 19 May 2008 09:57:01 +0500, "Abubakar" <q@y.comwrote :
    >Hi,
    >Lets say I have three integers (date, month, and year) as part of a struct
    >lets say like
    >struct mydate{ int day, month, year};
    >I want to add x number of days to this date. And it should be a real date
    >calculation thing. Like if the date is 31st december 2008, when i add 1 to
    >this, it should become 1st feb 2009.
    >This of course may require converting the above struct values to some data
    >type that crt's date functons can understand. I want to do that but havent
    >been able to yet. Thanks for help.
    The mktime function takes a broken-down local time value represented as a
    struct tm, normalizes the struct fields, and returns a time_t.

    --
    Doug Harrison
    Visual C++ MVP

    Comment

    • Abubakar

      #3
      Re: adding a day to time_t

      yes but how can it help me add days to my own specified date ?

      ...ab

      "Doug Harrison [MVP]" <dsh@mvps.orgwr ote in message
      news:c73234hvl8 o5m5bru76onif2k 3fb8ddi93@4ax.c om...
      On Mon, 19 May 2008 09:57:01 +0500, "Abubakar" <q@y.comwrote :
      >
      >>Hi,
      >>Lets say I have three integers (date, month, and year) as part of a struct
      >>lets say like
      >>struct mydate{ int day, month, year};
      >>I want to add x number of days to this date. And it should be a real date
      >>calculation thing. Like if the date is 31st december 2008, when i add 1 to
      >>this, it should become 1st feb 2009.
      >>This of course may require converting the above struct values to some data
      >>type that crt's date functons can understand. I want to do that but havent
      >>been able to yet. Thanks for help.
      >
      The mktime function takes a broken-down local time value represented as a
      struct tm, normalizes the struct fields, and returns a time_t.
      >
      --
      Doug Harrison
      Visual C++ MVP

      Comment

      • Doug Harrison [MVP]

        #4
        Re: adding a day to time_t

        On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
        >The mktime function takes a broken-down local time value represented as a
        >struct tm, normalizes the struct fields, and returns a time_t.
        >
        >yes but how can it help me add days to my own specified date ?
        For example, add x number of days to the tm_mday field before calling
        mktime. Although your thread subject mentions time_t, your original post
        talked about a broken down time in your own format, which you can easily
        express as a struct tm for use with mktime. If you actually have time_t
        values, you can convert them to struct tm with localtime or gmtime.

        --
        Doug Harrison
        Visual C++ MVP

        Comment

        • David Wilkinson

          #5
          Re: adding a day to time_t

          Abubakar wrote:
          Hi,
          Lets say I have three integers (date, month, and year) as part of a struct
          lets say like
          struct mydate{ int day, month, year};
          I want to add x number of days to this date. And it should be a real date
          calculation thing. Like if the date is 31st december 2008, when i add 1 to
          this, it should become 1st feb 2009.
          This of course may require converting the above struct values to some data
          type that crt's date functons can understand. I want to do that but havent
          been able to yet. Thanks for help.
          I'm using visual c++ 2k5.
          Regards,
          ab:

          time_t measures time in seconds, so to add a day you add 86400.

          --
          David Wilkinson
          Visual C++ MVP

          Comment

          • Abubakar

            #6
            Re: adding a day to time_t

            I have written something like:

            void AddDate (int day, int month, int year, int daystoadd)
            {
            tm mt;
            mt.tm_sec = 1;
            mt.tm_min = 1;
            mt.tm_hour = 1;
            mt.tm_wday = day;
            mt.tm_mon = month - 1;
            mt.tm_year = year - 1900;
            mt.tm_mday = day;

            time_t tmptime = mktime (&mt);
            tm * srctime = localtime (&tmptime);

            char buf[100];
            strftime (buf, 100, "%d/%m/%Y", srctime);
            printf("%s + %d = ", buf, daystoadd);
            // add days ...
            srctime->tm_mday += daystoadd;
            time_t finaldate = mktime (srctime);

            strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
            printf("%s", buf);

            }

            If there are any problems with the code please let me know.

            Regards,

            ...ab

            "Doug Harrison [MVP]" <dsh@mvps.orgwr ote in message
            news:or52349k4p f81rso7okogomeu io3bnf3nc@4ax.c om...
            On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
            >
            >>The mktime function takes a broken-down local time value represented as
            >>a
            >>struct tm, normalizes the struct fields, and returns a time_t.
            >>
            >>yes but how can it help me add days to my own specified date ?
            >
            For example, add x number of days to the tm_mday field before calling
            mktime. Although your thread subject mentions time_t, your original post
            talked about a broken down time in your own format, which you can easily
            express as a struct tm for use with mktime. If you actually have time_t
            values, you can convert them to struct tm with localtime or gmtime.
            >
            --
            Doug Harrison
            Visual C++ MVP

            Comment

            • Doug Harrison [MVP]

              #7
              Re: adding a day to time_t

              On Tue, 20 May 2008 19:34:39 +0500, "Abubakar" <q@y.comwrote :
              >I have written something like:
              >
              >void AddDate (int day, int month, int year, int daystoadd)
              >{
              tm mt;
              mt.tm_sec = 1;
              mt.tm_min = 1;
              mt.tm_hour = 1;
              mt.tm_wday = day;
              mt.tm_mon = month - 1;
              mt.tm_year = year - 1900;
              mt.tm_mday = day;
              >
              time_t tmptime = mktime (&mt);
              tm * srctime = localtime (&tmptime);
              >
              char buf[100];
              strftime (buf, 100, "%d/%m/%Y", srctime);
              printf("%s + %d = ", buf, daystoadd);
              // add days ...
              srctime->tm_mday += daystoadd;
              time_t finaldate = mktime (srctime);
              >
              strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
              printf("%s", buf);
              >
              >}
              >
              >If there are any problems with the code please let me know.
              There's no point in setting tm_wday, because mktime calculates its value
              from the other fields and overwrites it. Also, you need to set tm_isdst to
              one of the values documented as meaningful for it.

              --
              Doug Harrison
              Visual C++ MVP

              Comment

              • Abubakar

                #8
                Re: adding a day to time_t

                Ok cool. Thanks for the help.

                ...ab
                There's no point in setting tm_wday, because mktime calculates its value
                from the other fields and overwrites it. Also, you need to set tm_isdst to
                one of the values documented as meaningful for it.
                >
                --
                Doug Harrison
                Visual C++ MVP

                Comment

                • Ben Voigt [C++ MVP]

                  #9
                  Re: adding a day to time_t

                  Abubakar wrote:
                  I have written something like:
                  >
                  void AddDate (int day, int month, int year, int daystoadd)
                  {
                  tm mt;
                  mt.tm_sec = 1;
                  mt.tm_min = 1;
                  mt.tm_hour = 1;
                  mt.tm_wday = day;
                  mt.tm_mon = month - 1;
                  mt.tm_year = year - 1900;
                  mt.tm_mday = day;
                  >
                  time_t tmptime = mktime (&mt);
                  So now you have a time_t. If you aren't worried about leap seconds, then
                  just add 24*60*60 to the time_t (which is measured in seconds) for each day.
                  tm * srctime = localtime (&tmptime);
                  >
                  char buf[100];
                  strftime (buf, 100, "%d/%m/%Y", srctime);
                  printf("%s + %d = ", buf, daystoadd);
                  // add days ...
                  srctime->tm_mday += daystoadd;
                  time_t finaldate = mktime (srctime);
                  >
                  strftime (buf, 100, "%d/%m/%Y", localtime (&finaldate)) ;
                  printf("%s", buf);
                  >
                  }
                  >
                  If there are any problems with the code please let me know.
                  >
                  Regards,
                  >
                  ..ab
                  >
                  "Doug Harrison [MVP]" <dsh@mvps.orgwr ote in message
                  news:or52349k4p f81rso7okogomeu io3bnf3nc@4ax.c om...
                  >On Mon, 19 May 2008 10:56:16 +0500, "Abubakar" <q@y.comwrote :
                  >>
                  >>>The mktime function takes a broken-down local time value
                  >>>represente d as a
                  >>>struct tm, normalizes the struct fields, and returns a time_t.
                  >>>
                  >>yes but how can it help me add days to my own specified date ?
                  >>
                  >For example, add x number of days to the tm_mday field before calling
                  >mktime. Although your thread subject mentions time_t, your original
                  >post talked about a broken down time in your own format, which you
                  >can easily express as a struct tm for use with mktime. If you
                  >actually have time_t values, you can convert them to struct tm with
                  >localtime or gmtime. --
                  >Doug Harrison
                  >Visual C++ MVP

                  Comment

                  Working...