Time Conversion

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

    Time Conversion

    I have 2 time values:


    System time and an input from the user.


    1) System time is in the form of seconds from 1/1/1970 calculated by
    using


    time_t secs;
    SYSTEMTIME stime;


    time(&secs);


    2) The input from the user is in form hr:min:sec which is a string
    value.


    But the seperate values have been obtained by using


    sscanf(storedTi meValue, "%d:%d:%d", &hour,&minutes, &seconds);


    So now I have 3 integer values for hour,minutes and seconds.


    3) I have a function which calculates the time difference between 2
    time values,
    but both these time values need to be seconds from 1/1/1970.


    Is there any way by which,I could convert the user given time into time

    in seconds from 1/1/1970,
    so that I can get the difference between the system time and the user
    given time.

  • Richard Bos

    #2
    Re: Time Conversion

    "moni" <mons.2110@gmai l.comwrote:
    I have 2 time values: System time and an input from the user.
    1) System time is in the form of seconds from 1/1/1970
    ISO C doesn't guarantee that. Nor does it give you any way to do maths
    on time_t. But all is not lost...
    time_t secs;
    SYSTEMTIME stime;
    This type doesn't exist in C. It's also not necessary.
    time(&secs);
    2) The input from the user is in form hr:min:sec which is a string
    value. But the seperate values have been obtained by using
    sscanf(storedTi meValue, "%d:%d:%d", &hour,&minutes, &seconds);
    So now I have 3 integer values for hour,minutes and seconds.
    Good. Note that you'll still need the relevant date, but that can be had
    from the time_t you got from time().
    3) I have a function which calculates the time difference between 2
    time values, but both these time values need to be seconds from 1/1/1970.
    Then you have the wrong function. The right function to calculate the
    difference between two time_t's is in <time.h>, and it's called
    difftime(). This must work regardless of the format of a time_t, and is
    therefore superior to a home-made function which assumes an epoch of
    1970/01/01 and a resolution of 1 second.
    Is there any way by which,I could convert the user given time into time
    in seconds from 1/1/1970,
    No. However, there _is_ a way to convert it into a time_t. It's a bit
    roundabout, but it does work anywhere.
    First you convert your time_t into a struct tm, using either gmtime() or
    localtime(). Next you set the tm_hour, tm_min and tm_sec members of this
    struct tm to the values you got from sscanf(). Then you convert this
    struct tm back to a second time_t, using mktime(). Finally you subtract
    both time_t's using difftime(), and Bob's your uncle. All of these
    functions are ISO Standard C, and all (except sscanf(), obviously) can
    be found in <time.h>.

    Richard

    Comment

    • Richard Heathfield

      #3
      Re: Time Conversion

      Richard Bos said:

      <snip>
      ISO C doesn't [...] give you any way to do maths on time_t.
      ....except for difftime.

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • moni

        #4
        Re: Time Conversion


        struct tm *newtime, *oldtime;

        time_t result;
        time_t long_time;
        double elapsed_time;


        sscanf(stringto convert, "%d:%d:%d", &hour,&minutes, &seconds);


        time( &long_time ); /* Get time as long integer. */

        newtime = localtime( &long_time ); /* Convert to local time. */
        oldtime = localtime( &long_time );




        newtime->tm_isdst = 0;
        newtime->tm_hour = hour;
        newtime->tm_min = minutes;
        newtime->tm_sec = seconds;
        //newtime->tm_year = 2006;

        result = mktime(&newtime );



        elapsed_time = difftime( result, long_time );
        printf("time is %d, %d", result, long_time);



        Here the result always come s to -1, ie. mktime is always returning -1.

        Can you tell me the reason?

        Thanx alot..

        Richard Heathfield wrote:
        Richard Bos said:
        >
        <snip>
        >
        ISO C doesn't [...] give you any way to do maths on time_t.
        >
        ...except for difftime.
        >
        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • attn.steven.kuo@gmail.com

          #5
          Re: Time Conversion

          moni wrote:
          struct tm *newtime, *oldtime;
          >
          time_t result;
          time_t long_time;
          double elapsed_time;
          >
          >
          sscanf(stringto convert, "%d:%d:%d", &hour,&minutes, &seconds);
          >
          >
          time( &long_time ); /* Get time as long integer. */
          >
          newtime = localtime( &long_time ); /* Convert to local time. */
          oldtime = localtime( &long_time );
          >
          >
          >
          >
          newtime->tm_isdst = 0;
          newtime->tm_hour = hour;
          newtime->tm_min = minutes;
          newtime->tm_sec = seconds;
          //newtime->tm_year = 2006;
          >
          result = mktime(&newtime );
          >
          >
          >
          elapsed_time = difftime( result, long_time );
          printf("time is %d, %d", result, long_time);
          >
          >
          >
          Here the result always come s to -1, ie. mktime is always returning -1.
          >
          Can you tell me the reason?

          The argument to mktime is a pointer to struct tm:

          So try:

          result = mktime(newtime) ;

          instead of

          result = mktime(&newtime );

          If you want to throw in error checking, then:

          if ( (result = mktime(newtime) ) == (time_t)-1)
          {
          fprintf(stderr, "Bad mktime\n");
          return EXIT_FAILURE;
          }

          --
          Hope this helps,
          Steven

          Comment

          • moni

            #6
            Re: Time Conversion

            hey...

            Thanx alot...

            that worked..!


            attn.steven.kuo @gmail.com wrote:
            moni wrote:
            struct tm *newtime, *oldtime;

            time_t result;
            time_t long_time;
            double elapsed_time;


            sscanf(stringto convert, "%d:%d:%d", &hour,&minutes, &seconds);


            time( &long_time ); /* Get time as long integer. */

            newtime = localtime( &long_time ); /* Convert to local time. */
            oldtime = localtime( &long_time );




            newtime->tm_isdst = 0;
            newtime->tm_hour = hour;
            newtime->tm_min = minutes;
            newtime->tm_sec = seconds;
            //newtime->tm_year = 2006;

            result = mktime(&newtime );



            elapsed_time = difftime( result, long_time );
            printf("time is %d, %d", result, long_time);



            Here the result always come s to -1, ie. mktime is always returning -1.

            Can you tell me the reason?
            >
            >
            The argument to mktime is a pointer to struct tm:
            >
            So try:
            >
            result = mktime(newtime) ;
            >
            instead of
            >
            result = mktime(&newtime );
            >
            If you want to throw in error checking, then:
            >
            if ( (result = mktime(newtime) ) == (time_t)-1)
            {
            fprintf(stderr, "Bad mktime\n");
            return EXIT_FAILURE;
            }
            >
            --
            Hope this helps,
            Steven

            Comment

            • Jordan Abel

              #7
              Re: Time Conversion

              2006-11-07 <1162926494.233 238.42790@i42g2 000cwa.googlegr oups.com>,
              moni wrote:
              hey...
              >
              Thanx alot...
              >
              that worked..!
              Top-posting isn't good.

              Anyway - you should be aware a tm_year value of 2006 refers to the year
              3906. The proper tm_year value for this year is 106.

              Comment

              • moni

                #8
                Re: Time Conversion

                Why is that?

                i dint get it...

                thanx..


                Jordan Abel wrote:
                2006-11-07 <1162926494.233 238.42790@i42g2 000cwa.googlegr oups.com>,
                moni wrote:
                hey...

                Thanx alot...

                that worked..!
                >
                Top-posting isn't good.
                >
                Anyway - you should be aware a tm_year value of 2006 refers to the year
                3906. The proper tm_year value for this year is 106.

                Comment

                • Asbjørn Sæbø

                  #9
                  OT: Q: C Unleashed - availability


                  The book "C Unleashed" seems to be out of print and somewhat hard to
                  come by. Can anybody (Mr. Heathfield, for example) tell me whether
                  there are any plans for a reprint or a new edition?

                  (Sorry for the somewhat off-topic post. I tried to contact
                  Mr. Heathfield at the email address he specifies at the bottom of his
                  posts, but it came back with a message saying:
                  "Your message cannot be delivered to the following recipients:

                  Recipient address: <xxx>@<xxxx>.or g.uk
                  Reason: Illegal host/domain name found")

                  Asbjørn Sæbø
                  --
                  A: Because it messes up the order in which people normally read text.
                  Q: Why is top-posting such a bad thing?
                  A: Top-posting.
                  Q: What is the most annoying thing on usenet and in e-mail?

                  Comment

                  • John Gordon

                    #10
                    Re: Time Conversion

                    In <1162933374.209 969.30560@k70g2 000cwa.googlegr oups.com"moni" <mons.2110@gmai l.comwrites:
                    Anyway - you should be aware a tm_year value of 2006 refers to the year
                    3906. The proper tm_year value for this year is 106.
                    Why is that?
                    i dint get it...
                    Because the definition of the tm_year field is "years since 1900", not
                    "years since 0".

                    --
                    John Gordon "... What with you being his parents and all,
                    gordon@panix.co m I think that you could be trusted not to shaft
                    him." -- Robert Chang, rec.games.board

                    Comment

                    • Richard Heathfield

                      #11
                      Re: OT: Q: C Unleashed - availability

                      Asbj?rn Sæb? said:
                      >
                      The book "C Unleashed" seems to be out of print and somewhat hard to
                      come by.
                      Yes, I'm afraid that's true. "C Unleashed" went out of print earlier this
                      year IIRC, and as far as I know there are no plans for a re-print or a new
                      edition. :-(
                      (Sorry for the somewhat off-topic post. I tried to contact
                      Mr. Heathfield at the email address he specifies at the bottom of his
                      posts, but it came back with a message saying:
                      "Your message cannot be delivered to the following recipients:
                      Mea culpa. The mail forwarding is broken at present, and I haven't got
                      around to contacting the forwarder to find out why. I'll try to remember to
                      do that tomorrow.

                      --
                      Richard Heathfield
                      "Usenet is a strange place" - dmr 29/7/1999

                      email: er, none at the moment.

                      Comment

                      • Asbjørn Sæbø

                        #12
                        Re: OT: Q: C Unleashed - availability

                        Richard Heathfield <invalid@invali d.invalidwrites :
                        Asbj?rn Sæb? said:
                        >

                        The book "C Unleashed" seems to be out of print and somewhat hard to
                        come by.
                        >
                        Yes, I'm afraid that's true. "C Unleashed" went out of print earlier this
                        year IIRC, and as far as I know there are no plans for a re-print or a new
                        edition. :-(
                        OK, thanks for the information. I'll have to look around for a bit,
                        then.

                        Asbjørn
                        --
                        A: Because it messes up the order in which people normally read text.
                        Q: Why is top-posting such a bad thing?
                        A: Top-posting.
                        Q: What is the most annoying thing on usenet and in e-mail?

                        Comment

                        • Andrew Poelstra

                          #13
                          Re: OT: Q: C Unleashed - availability

                          On Tue, 2006-11-07 at 22:23 +0100, Asbjørn Sæbø wrote:
                          The book "C Unleashed" seems to be out of print and somewhat hard to
                          come by. Can anybody (Mr. Heathfield, for example) tell me whether
                          there are any plans for a reprint or a new edition?

                          (Sorry for the somewhat off-topic post. I tried to contact
                          Mr. Heathfield at the email address he specifies at the bottom of his
                          posts, but it came back with a message saying:
                          "Your message cannot be delivered to the following recipients:

                          Recipient address: <xxx>@<xxxx>.or g.uk
                          Reason: Illegal host/domain name found")
                          I got my copy at Half Price Computer Books. I had requested it
                          from my library, but they denied it on the grounds that it was
                          "older" and "high priced".

                          You may find that contacting Sams Publishing will get you a
                          better answer than this group will.

                          --
                          Andrew Poelstra <http://www.wpsoftware. net>
                          For email, use 'apoelstra' at the above site.
                          "You're only smart on the outside." -anon.

                          Comment

                          • Keith Thompson

                            #14
                            Re: Time Conversion

                            "moni" <mons.2110@gmai l.comwrites:
                            Why is that?
                            >
                            i dint get it...
                            You didn't get what? If you didn't get Jordan's advice to avoid
                            top-posting, read the following:




                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                            We must do something. This is something. Therefore, we must do this.

                            Comment

                            • Nelu

                              #15
                              Re: OT: Q: C Unleashed - availability



                              On Nov 7, 5:53 pm, Asbjørn Sæbø <a...@stud.ntnu .nowrote:
                              Richard Heathfield <inva...@invali d.invalidwrites :
                              Asbj?rn Sæb? said:
                              >
                              The book "C Unleashed" seems to be out of print and somewhat hard to
                              come by.
                              >
                              Yes, I'm afraid that's true. "C Unleashed" went out of print earlier this
                              year IIRC, and as far as I know there are no plans for a re-print or a new
                              edition. :-(
                              OK, thanks for the information. I'll have to look around for a bit,
                              then.
                              They still have it on Amazon if you have nothing against used books.

                              --
                              Ioan - Ciprian Tandau
                              tandau _at_ freeshell _dot_ org (hope it's not too late)
                              (... and that it still works...)

                              Comment

                              Working...