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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. UTC Time from a C program

UTC Time from a C program

Scheduled Pinned Locked Moved C#
sysadminquestiondiscussionlearning
5 Posts 3 Posters 1 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
    mcljava
    wrote on last edited by
    #1

    So.... I get UTC time from a C server in a 32-bit unsigned integer (aka long). Let's say for sake of discussion the UTC time is in the following variable: uint utcTime; And let's say I also have: DateTime locTime; And of course, there seems to be no combination of DateTime() methods that will address my simple need. Any ideas? Mike Luster

    J 1 Reply Last reply
    0
    • M mcljava

      So.... I get UTC time from a C server in a 32-bit unsigned integer (aka long). Let's say for sake of discussion the UTC time is in the following variable: uint utcTime; And let's say I also have: DateTime locTime; And of course, there seems to be no combination of DateTime() methods that will address my simple need. Any ideas? Mike Luster

      J Offline
      J Offline
      Jon Sagara
      wrote on last edited by
      #2

      I'm not quite sure what you're asking. Are you trying to turn your uint into a DateTime? Edit: Boy, MSDN[^] is sure helpful: Convert.ToDateTime Method (UInt32): Return Value This conversion is not supported. No value is returned. Jon Sagara Look at him. He runs like a Welshman. Doesn't he run like a Welshman? Doesn't he? I think he runs like a Welshman. Sagara.org | Blog | My Articles -- modified at 21:02 Wednesday 22nd February, 2006

      M 1 Reply Last reply
      0
      • J Jon Sagara

        I'm not quite sure what you're asking. Are you trying to turn your uint into a DateTime? Edit: Boy, MSDN[^] is sure helpful: Convert.ToDateTime Method (UInt32): Return Value This conversion is not supported. No value is returned. Jon Sagara Look at him. He runs like a Welshman. Doesn't he run like a Welshman? Doesn't he? I think he runs like a Welshman. Sagara.org | Blog | My Articles -- modified at 21:02 Wednesday 22nd February, 2006

        M Offline
        M Offline
        mcljava
        wrote on last edited by
        #3

        Yea, but the Uint32 that holds the time contains UTC formatted time from a C based program sent over ethernet. I pull it out of a packet, decode and now I need to be able to convert it to DateTime to be able to use it the C# app. Thanks

        G 1 Reply Last reply
        0
        • M mcljava

          Yea, but the Uint32 that holds the time contains UTC formatted time from a C based program sent over ethernet. I pull it out of a packet, decode and now I need to be able to convert it to DateTime to be able to use it the C# app. Thanks

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          I believe the Uint32 value is the number of seconds since January 1, 1970 (most likely). Thus, to get the correct DateTime, you must initialize the your DateTime variable to January 1, 1970 and add the Uint32 seconds to it. -- modified at 23:33 Wednesday 22nd February, 2006

          M 1 Reply Last reply
          0
          • G George L Jackson

            I believe the Uint32 value is the number of seconds since January 1, 1970 (most likely). Thus, to get the correct DateTime, you must initialize the your DateTime variable to January 1, 1970 and add the Uint32 seconds to it. -- modified at 23:33 Wednesday 22nd February, 2006

            M Offline
            M Offline
            mcljava
            wrote on last edited by
            #5

            Thanks for the tip George. Here's what I have encountered: uint uval // for this example assume it contains the UTC time, a value like 1140713262 DateTime dt = new DateTime( 1970, 1, 1 ); dt.AddSeconds( uval ); Console.WriteLine( "Date is {0}", dt.ToLocalTime( ) ); Here is a real result to illustrate: 2/23/2006 11:49:15 AM: Raw POSIX Date-Time: 1140713262, Conversion: 12/31/1969 7:00:00 PM I printed out the 2K/POSIX value, followed by the conversion to local time. I was thinking the conversion should have yielded a date time along the lines of the message preamble 2/23/2006 11:49:15 AM give or take a few seconds in terms of synchronization w/ the server time. However it was off by just a few years! I was curious to what was happening so I changed the dt.ToLocalTime() to dt.ToUniversalTime() and it yields 1/1/1970 5:00:00 AM. Since the DateTime is initialized to DateTime( 1970, 1, 1 ) this begs the question of just what is dt.AddSeconds( Convert.ToDouble( uval ) ) adding? Interestingly if I print out dt without the ToLocalTime() conversion it yields 1/1/1970 12:00 AM. That means the seconds added should be 18000 to result in 1/1/1970 5:00:00 AM. This is why I am a little suspect of the 2K/POSIX value, it doesn’t make sense to me to added a 10 digit value and only yield a 5 hour delta since the 1970 epoch. Now I have quadruple checked the packet decoding logic and I can tell you that all fields before and those after in a variable length packet are all correct and at the appropriate offset. Thus I have revisited the decoding and it appears to be OK in every respect less the date time situation. At this juncture I am wondering if the 2K flavor of UTC requires any special consideration. Or is there something I am just plain missing? I am stumped. Final FYI – according to the documentation from the Server vendor, the date-time is stored as followed: TIME 4 Bytes A date/time, expressed as the number of seconds since Midnight January 1, 1970 Coordinated Universal Time (UTC). So at least I know I am playing with the right deck, :laugh: Mike Luster

            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