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#
  4. C# time to C++ time_t

C# time to C++ time_t

Scheduled Pinned Locked Moved C#
csharpc++question
5 Posts 3 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.
  • G Offline
    G Offline
    goodpilot
    wrote on last edited by
    #1

    I am sending binary data between C# and C++ successfully. However, I would like to convert the C++ time_t, which is a 32 bit long in C++ to a C# DateTime. Does anyone have a solution off hand for converting time_t to a C# DateTime value and from a C# DateTime to a C++ time_t?

    H 1 Reply Last reply
    0
    • G goodpilot

      I am sending binary data between C# and C++ successfully. However, I would like to convert the C++ time_t, which is a 32 bit long in C++ to a C# DateTime. Does anyone have a solution off hand for converting time_t to a C# DateTime value and from a C# DateTime to a C++ time_t?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Since time_t is just an unsigned 32-bit integer, you can use uint (alias for UInt32), but I recommend you don't depending on how you use it because unsigned types in .NET are not CLS-compliant (i.e., less portable code). Whenever you need to pass it to unmanaged code or get it back, you can use the MarshalAsAttribute with UnmanagedType.U4. For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64. The value is still the same, it's just how it's interpreted. The marshaling I mentioned above will take care of this. The biggest hurdle has to do with the epoch (start time) of each. time_t is the number of seconds that have ellapsed since 12:00 midnight, 1-1-1970. A DateTime in .NET represents the number of ticks (100 nanoseconds) since 12:00 midnight, 1-1-0001 AD. This definitely becomes a hurdle as I'm sure you can see. One easy way to convert between the two is to create a DateTime that starts at the epoch of a time_t, and then add the number of seconds represented by time_t:

      DateTime dt = DateTime.Parse("1970-01-01T00:00:00");
      dt.AddSeconds(time_t); // From wherever it comes

      If you do this a lot, you might consider storing the return from DateTime.Parse as a read-only field of your class (the readonly keyword lets the field be set in the static or instance constructor and can never be set again, which is good since you can't use a const for a non-constant value like this).

      Microsoft MVP, Visual C# My Articles

      G B 2 Replies Last reply
      0
      • H Heath Stewart

        Since time_t is just an unsigned 32-bit integer, you can use uint (alias for UInt32), but I recommend you don't depending on how you use it because unsigned types in .NET are not CLS-compliant (i.e., less portable code). Whenever you need to pass it to unmanaged code or get it back, you can use the MarshalAsAttribute with UnmanagedType.U4. For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64. The value is still the same, it's just how it's interpreted. The marshaling I mentioned above will take care of this. The biggest hurdle has to do with the epoch (start time) of each. time_t is the number of seconds that have ellapsed since 12:00 midnight, 1-1-1970. A DateTime in .NET represents the number of ticks (100 nanoseconds) since 12:00 midnight, 1-1-0001 AD. This definitely becomes a hurdle as I'm sure you can see. One easy way to convert between the two is to create a DateTime that starts at the epoch of a time_t, and then add the number of seconds represented by time_t:

        DateTime dt = DateTime.Parse("1970-01-01T00:00:00");
        dt.AddSeconds(time_t); // From wherever it comes

        If you do this a lot, you might consider storing the return from DateTime.Parse as a read-only field of your class (the readonly keyword lets the field be set in the static or instance constructor and can never be set again, which is good since you can't use a const for a non-constant value like this).

        Microsoft MVP, Visual C# My Articles

        G Offline
        G Offline
        goodpilot
        wrote on last edited by
        #3

        Two lines of code! Thank you much.

        1 Reply Last reply
        0
        • H Heath Stewart

          Since time_t is just an unsigned 32-bit integer, you can use uint (alias for UInt32), but I recommend you don't depending on how you use it because unsigned types in .NET are not CLS-compliant (i.e., less portable code). Whenever you need to pass it to unmanaged code or get it back, you can use the MarshalAsAttribute with UnmanagedType.U4. For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64. The value is still the same, it's just how it's interpreted. The marshaling I mentioned above will take care of this. The biggest hurdle has to do with the epoch (start time) of each. time_t is the number of seconds that have ellapsed since 12:00 midnight, 1-1-1970. A DateTime in .NET represents the number of ticks (100 nanoseconds) since 12:00 midnight, 1-1-0001 AD. This definitely becomes a hurdle as I'm sure you can see. One easy way to convert between the two is to create a DateTime that starts at the epoch of a time_t, and then add the number of seconds represented by time_t:

          DateTime dt = DateTime.Parse("1970-01-01T00:00:00");
          dt.AddSeconds(time_t); // From wherever it comes

          If you do this a lot, you might consider storing the return from DateTime.Parse as a read-only field of your class (the readonly keyword lets the field be set in the static or instance constructor and can never be set again, which is good since you can't use a const for a non-constant value like this).

          Microsoft MVP, Visual C# My Articles

          B Offline
          B Offline
          Bryan White
          wrote on last edited by
          #4

          "For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64" I think that you'll find that 0xffff is 65535 for Int32 as well as in UInt16, UInt32, Int64 & UInt64 but -1 in Int16. 0xffffffff is -1 in Int32 & 4,294,967,295 in UInt32, Int64 & UInt64. Regards Brewman

          H 1 Reply Last reply
          0
          • B Bryan White

            "For instance, 0xffff is -1 for an Int32 while it is 65535 for a UInt64" I think that you'll find that 0xffff is 65535 for Int32 as well as in UInt16, UInt32, Int64 & UInt64 but -1 in Int16. 0xffffffff is -1 in Int32 & 4,294,967,295 in UInt32, Int64 & UInt64. Regards Brewman

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            Oops, that last part of the quote was actually supposed to be UInt32, but you're right - I obviously couldn't perform basic math that day. :-O

            Microsoft MVP, Visual C# My Articles

            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