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. How to get the local time in seconds?

How to get the local time in seconds?

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studiotutorialquestion
8 Posts 4 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.
  • Y Offline
    Y Offline
    Yonggoo
    wrote on last edited by
    #1

    How to get local time in seconds? I am using MS Visual Studio 2005. I could get UTC time in seconds below. Are they correct? Thanks! #include #include double Clock::getUtcSeconds()const // returns UTC time in second { time_t UtcTimeValue; return time(&UtcTimeValue); } string Clock::getUtcHMSTime()const { char buffer[10]; string strUtcTime; time_t UtcTimeValue; time(&UtcTimeValue); struct tm* UtcTime = gmtime(&UtcTimeValue); //convert the calender time to UTC time strUtcTime+=itoa(UtcTime->tm_hour, buffer,10); //convert to decimal number strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_min, buffer,10); strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_sec, buffer,10); return strUtcTime; }

    Yonggoo

    L M D 3 Replies Last reply
    0
    • Y Yonggoo

      How to get local time in seconds? I am using MS Visual Studio 2005. I could get UTC time in seconds below. Are they correct? Thanks! #include #include double Clock::getUtcSeconds()const // returns UTC time in second { time_t UtcTimeValue; return time(&UtcTimeValue); } string Clock::getUtcHMSTime()const { char buffer[10]; string strUtcTime; time_t UtcTimeValue; time(&UtcTimeValue); struct tm* UtcTime = gmtime(&UtcTimeValue); //convert the calender time to UTC time strUtcTime+=itoa(UtcTime->tm_hour, buffer,10); //convert to decimal number strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_min, buffer,10); strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_sec, buffer,10); return strUtcTime; }

      Yonggoo

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      http://msdn2.microsoft.com/en-us/library/ms724390.aspx[^]

      led mike

      Y 1 Reply Last reply
      0
      • L led mike

        http://msdn2.microsoft.com/en-us/library/ms724390.aspx[^]

        led mike

        Y Offline
        Y Offline
        Yonggoo
        wrote on last edited by
        #3

        We want to keep our system cross-platform. Any ANSI standard way to get local time in seconds?

        Yonggoo

        L Y 2 Replies Last reply
        0
        • Y Yonggoo

          We want to keep our system cross-platform. Any ANSI standard way to get local time in seconds?

          Yonggoo

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Yonggoo wrote:

          Any ANSI standard way to get local time in seconds?

          To bad you didn't include that requirement in your first post I might not have wasted my time giving you an answer that you can't use. X|

          led mike

          Y 1 Reply Last reply
          0
          • Y Yonggoo

            How to get local time in seconds? I am using MS Visual Studio 2005. I could get UTC time in seconds below. Are they correct? Thanks! #include #include double Clock::getUtcSeconds()const // returns UTC time in second { time_t UtcTimeValue; return time(&UtcTimeValue); } string Clock::getUtcHMSTime()const { char buffer[10]; string strUtcTime; time_t UtcTimeValue; time(&UtcTimeValue); struct tm* UtcTime = gmtime(&UtcTimeValue); //convert the calender time to UTC time strUtcTime+=itoa(UtcTime->tm_hour, buffer,10); //convert to decimal number strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_min, buffer,10); strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_sec, buffer,10); return strUtcTime; }

            Yonggoo

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            Yonggoo wrote:

            I could get UTC time in seconds below. Are they correct?

            Have you tried it? Good luck on January 18, 2038 :)

            "Great job, team. Head back to base for debriefing and cocktails." (Spottswoode "Team America")

            1 Reply Last reply
            0
            • Y Yonggoo

              How to get local time in seconds? I am using MS Visual Studio 2005. I could get UTC time in seconds below. Are they correct? Thanks! #include #include double Clock::getUtcSeconds()const // returns UTC time in second { time_t UtcTimeValue; return time(&UtcTimeValue); } string Clock::getUtcHMSTime()const { char buffer[10]; string strUtcTime; time_t UtcTimeValue; time(&UtcTimeValue); struct tm* UtcTime = gmtime(&UtcTimeValue); //convert the calender time to UTC time strUtcTime+=itoa(UtcTime->tm_hour, buffer,10); //convert to decimal number strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_min, buffer,10); strUtcTime+=" : "; strUtcTime+=itoa(UtcTime->tm_sec, buffer,10); return strUtcTime; }

              Yonggoo

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Yonggoo wrote:

              Are they correct?

              You are in the best position to determine that.

              Yonggoo wrote:

              strUtcTime+=" : ";

              You are making an assumption here that a colon is always the separator.


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              1 Reply Last reply
              0
              • Y Yonggoo

                We want to keep our system cross-platform. Any ANSI standard way to get local time in seconds?

                Yonggoo

                Y Offline
                Y Offline
                Yonggoo
                wrote on last edited by
                #7

                I have tried many ways. The following worked. double Clock::getLocalSeconds()const // The local time is hours behind. All else are the sams as UTC time. { time_t UtcTimeValue; time(&UtcTimeValue); //get the system time, which is UTC time struct tm* UtcHMS= gmtime(&UtcTimeValue); //convert it to struct tm*, which is UTC time int utcHours = UtcHMS->tm_hour; time_t LocalTimeValue; time(&LocalTimeValue); //get the system time, which is UTC time struct tm* LocalHMS = localtime(&LocalTimeValue); //convert it to struct tm*, which is the local time int localHours = LocalHMS->tm_hour; double diffHours =utcHours - localHours; return ((double)UtcTimeValue - diffHours*SECONDS_IN_HOUR); }

                Yonggoo

                1 Reply Last reply
                0
                • L led mike

                  Yonggoo wrote:

                  Any ANSI standard way to get local time in seconds?

                  To bad you didn't include that requirement in your first post I might not have wasted my time giving you an answer that you can't use. X|

                  led mike

                  Y Offline
                  Y Offline
                  Yonggoo
                  wrote on last edited by
                  #8

                  Sorry mike!

                  Yonggoo

                  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