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. get the a 2days time dif fromcurrent time.

get the a 2days time dif fromcurrent time.

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 5 Posters 0 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.
  • R Offline
    R Offline
    ramina sen
    wrote on last edited by
    #1

    Hi ALL, I am trying to get the timing exactly 2 days diff from current time.please advice i am not getting it correctly. here is my code (-2 does not work)

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

    int main() {

    time\_t current\_time;
    char\* c\_time\_string;
    
    current\_time = time(NULL);
    current\_time = current\_time -2
    
    /\* Convert to local time format. \*/
    c\_time\_string = ctime(&current\_time);
    
    printf("Current time is %s", c\_time\_string);
    
    return 0;
    

    }

    D J CPalliniC 3 Replies Last reply
    0
    • R ramina sen

      Hi ALL, I am trying to get the timing exactly 2 days diff from current time.please advice i am not getting it correctly. here is my code (-2 does not work)

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

      int main() {

      time\_t current\_time;
      char\* c\_time\_string;
      
      current\_time = time(NULL);
      current\_time = current\_time -2
      
      /\* Convert to local time format. \*/
      c\_time\_string = ctime(&current\_time);
      
      printf("Current time is %s", c\_time\_string);
      
      return 0;
      

      }

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

      http://en.cppreference.com/w/c/chrono/time[^] Read the notes carefully.

      If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

      1 Reply Last reply
      0
      • R ramina sen

        Hi ALL, I am trying to get the timing exactly 2 days diff from current time.please advice i am not getting it correctly. here is my code (-2 does not work)

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

        int main() {

        time\_t current\_time;
        char\* c\_time\_string;
        
        current\_time = time(NULL);
        current\_time = current\_time -2
        
        /\* Convert to local time format. \*/
        c\_time\_string = ctime(&current\_time);
        
        printf("Current time is %s", c\_time\_string);
        
        return 0;
        

        }

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        The time function returns the time as the number of seconds since the Epoch (1970-01-01 00:00:00 +0000 UTC). See also the C runtime time function: http://linux.die.net/man/2/time[^] (Linux) and https://msdn.microsoft.com/en-us/library/1f4c8f33.aspx[^] (Windows). So you must add / subtract 60 * 60 * 2 = 7200 seconds to apply a difference of two days.

        P 1 Reply Last reply
        0
        • J Jochen Arndt

          The time function returns the time as the number of seconds since the Epoch (1970-01-01 00:00:00 +0000 UTC). See also the C runtime time function: http://linux.die.net/man/2/time[^] (Linux) and https://msdn.microsoft.com/en-us/library/1f4c8f33.aspx[^] (Windows). So you must add / subtract 60 * 60 * 2 = 7200 seconds to apply a difference of two days.

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          Oops! Try 60 * 60 * 24 * 2. btw, seconds in a day is "eight six four two zeros" ie 86 400. Cheers, Peter

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          J 1 Reply Last reply
          0
          • P Peter_in_2780

            Oops! Try 60 * 60 * 24 * 2. btw, seconds in a day is "eight six four two zeros" ie 86 400. Cheers, Peter

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            Ooops. Lack of coffee. Thank you for the correction.

            1 Reply Last reply
            0
            • R ramina sen

              Hi ALL, I am trying to get the timing exactly 2 days diff from current time.please advice i am not getting it correctly. here is my code (-2 does not work)

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

              int main() {

              time\_t current\_time;
              char\* c\_time\_string;
              
              current\_time = time(NULL);
              current\_time = current\_time -2
              
              /\* Convert to local time format. \*/
              c\_time\_string = ctime(&current\_time);
              
              printf("Current time is %s", c\_time\_string);
              
              return 0;
              

              }

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              The time function returns seconds since Epoch. In order to obtain two days back you have to subtract the correct amount of seconds, e.g.

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

              #define SECONDS_PER_DAY 86400

              int main()
              {
              time_t current_time;
              char* c_time_string;

              current\_time = time(NULL);
              current\_time = current\_time - 2 \* SECONDS\_PER\_DAY;
              
              /\* Convert to local time format. \*/
              c\_time\_string = ctime(&current\_time);
              
              printf("Current time is %s\\n", c\_time\_string);
              
              return 0;
              

              }

              In testa che avete, signor di Ceprano?

              D 1 Reply Last reply
              0
              • CPalliniC CPallini

                The time function returns seconds since Epoch. In order to obtain two days back you have to subtract the correct amount of seconds, e.g.

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

                #define SECONDS_PER_DAY 86400

                int main()
                {
                time_t current_time;
                char* c_time_string;

                current\_time = time(NULL);
                current\_time = current\_time - 2 \* SECONDS\_PER\_DAY;
                
                /\* Convert to local time format. \*/
                c\_time\_string = ctime(&current\_time);
                
                printf("Current time is %s\\n", c\_time\_string);
                
                return 0;
                

                }

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

                The C-2011 standard does not specify the epoch or the resolution of time(). The POSIX standard specifies the epoch as 1970-01-01 00:00:00, and the resolution as seconds. The fact that most runtime libraries follow the POSIX standard should not blind you to the fact that using this function to perform date arithmetic is inherently non-portable. If you wish to perform date/time manipulations, you should use the functions that operate on the 'tm' structure.

                If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                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