Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Difference between current system time and time from user input in c

Difference between current system time and time from user input in c

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
10 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14979108
    wrote on last edited by
    #1

    How to find different between current system time and user given time in c (only C not in c++) program. Is there any method that I get only time (without date) as output (not as char as time_t type).

    Mircea NeacsuM D 2 Replies Last reply
    0
    • M Member_14979108

      How to find different between current system time and user given time in c (only C not in c++) program. Is there any method that I get only time (without date) as output (not as char as time_t type).

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      You could calculate the number of seconds from midnight for current time and user given time. Subtract the two and convert back to hours, minutes seconds:

      time_t now;
      time (&now); //missed this
      struct tm *systime = localtime(&now);
      struct tm user_time = {56, 34, 12}; // user = 12:34:56
      int sys_sec = systime->tm_hour*3600 + systime->tm_min*60 + stystime->tm_sec;
      int user_sec = user_time.tm_hour*3600 + user_time.tm_min*60 + user_time.tm_sec;
      int diff = sys_sec - user_sec;

      Of course you have to figure out what to do when time wraps around at midnight but that's your problem :). I'm just showing how you can shoot yourself in the foot

      Mircea

      Richard Andrew x64R 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        You could calculate the number of seconds from midnight for current time and user given time. Subtract the two and convert back to hours, minutes seconds:

        time_t now;
        time (&now); //missed this
        struct tm *systime = localtime(&now);
        struct tm user_time = {56, 34, 12}; // user = 12:34:56
        int sys_sec = systime->tm_hour*3600 + systime->tm_min*60 + stystime->tm_sec;
        int user_sec = user_time.tm_hour*3600 + user_time.tm_min*60 + user_time.tm_sec;
        int diff = sys_sec - user_sec;

        Of course you have to figure out what to do when time wraps around at midnight but that's your problem :). I'm just showing how you can shoot yourself in the foot

        Mircea

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Mircea Neacsu wrote:

        struct tm *systime = localtime(&now);

        I've never understood declaring a variable as struct*. struct is a keyword used to define a UDF. It's not a type in itself.

        The difficult we do right away... ...the impossible takes slightly longer.

        Mircea NeacsuM L 2 Replies Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Mircea Neacsu wrote:

          struct tm *systime = localtime(&now);

          I've never understood declaring a variable as struct*. struct is a keyword used to define a UDF. It's not a type in itself.

          The difficult we do right away... ...the impossible takes slightly longer.

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          tm is a regular structure and localtime (as well as gmtime) return a pointer to an internal buffer. Quoting from cplusplus.com:[^]

          Quote:

          A pointer to a tm structure with its members filled with the values that correspond to the local time representation of timer. The returned value points to an internal object whose validity or value may be altered by any subsequent call to gmtime or localtime.

          Now that I look again at the code I see that I missed the call to time(&now). Oops!

          Mircea

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            Mircea Neacsu wrote:

            struct tm *systime = localtime(&now);

            I've never understood declaring a variable as struct*. struct is a keyword used to define a UDF. It's not a type in itself.

            The difficult we do right away... ...the impossible takes slightly longer.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            It is simply a pointer to a structure that gets filled in by the system call. What is wrong with that?

            Richard Andrew x64R 1 Reply Last reply
            0
            • L Lost User

              It is simply a pointer to a structure that gets filled in by the system call. What is wrong with that?

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              It bothers me because it would be akin to saying:

              class *Foo = new Foo();

              Is that legal C++? EDIT: Oh wait, I didn't notice that there is a struct name followed by a variable name. I see now.

              The difficult we do right away... ...the impossible takes slightly longer.

              L 1 Reply Last reply
              0
              • M Member_14979108

                How to find different between current system time and user given time in c (only C not in c++) program. Is there any method that I get only time (without date) as output (not as char as time_t type).

                D Offline
                D Offline
                Daniel Pfeffer
                wrote on last edited by
                #7

                You can use the [difftime()](http://www.cplusplus.com/reference/ctime/difftime/) function to subtract two time_t values. If you have the time in a struct tm, you can convert it to time_t using the [mktime()](http://www.cplusplus.com/reference/ctime/mktime/) function. The [strftime()](http://www.cplusplus.com/reference/ctime/strftime/) function formats a struct tm to text format. These APIs are all available in C90, but some options were introduced as of C99. difftime() should incorporate leap-seconds for past dates, but I don't know if actual implementations do so. Note that issues such as daylight-saving time, time zones, etc. are left to the programmer. Ignoring these is a common way for programmers to shoot themselves in the foot. :)

                Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  It bothers me because it would be akin to saying:

                  class *Foo = new Foo();

                  Is that legal C++? EDIT: Oh wait, I didn't notice that there is a struct name followed by a variable name. I see now.

                  The difficult we do right away... ...the impossible takes slightly longer.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Richard Andrew x64 wrote:

                  Is that legal C++?

                  Yes, and necessary. Pointers must actually point to something, or they are just empty space.

                  Richard Andrew x64R 1 Reply Last reply
                  0
                  • L Lost User

                    Richard Andrew x64 wrote:

                    Is that legal C++?

                    Yes, and necessary. Pointers must actually point to something, or they are just empty space.

                    Richard Andrew x64R Offline
                    Richard Andrew x64R Offline
                    Richard Andrew x64
                    wrote on last edited by
                    #9

                    You misunderstood what I was demonstrating. I was demonstrating declaring a variable of type "class*". Is it possible to declare a variable of type "class"? Is there such a type? I don't think that's legal in C++. I haven't tried it though.

                    The difficult we do right away... ...the impossible takes slightly longer.

                    L 1 Reply Last reply
                    0
                    • Richard Andrew x64R Richard Andrew x64

                      You misunderstood what I was demonstrating. I was demonstrating declaring a variable of type "class*". Is it possible to declare a variable of type "class"? Is there such a type? I don't think that's legal in C++. I haven't tried it though.

                      The difficult we do right away... ...the impossible takes slightly longer.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Richard Andrew x64 wrote:

                      Is it possible to declare a variable of type "class"?

                      No, because class is an abstract concept, not a type in the same sense that int is. The actual type will be the name used in the class definition. For example consider the following:

                      class Foo
                      {
                      public:
                      Foo() { std::cout << "Constructing a Foo object" << std::endl; }
                      };

                      int main(int argc, char** argv)
                      {
                      Foo aFoo;
                      Foo* pFoo = new Foo();
                      }

                      A new object aFoo is created and stored on the local stack. A second new object is instantiated on the heap and its address passed back to be stored in the pointer pFoo. The type of both objects is Foo not class. Does that make sense?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups