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++ CTime to .NET DateTime

C++ CTime to .NET DateTime

Scheduled Pinned Locked Moved C#
c++csharphelp
11 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.
  • C CSharpDavid

    I have an app that logs data and serialises a CTime to an archive ie CTime time achive << time I cannot for the life of me get .NET to read it from the file. I can get the same value from the file , its just that the code below produces a date somewhere in the 1600's DateTime time=DataTime.FromBinary(theFileValue); I can load the value in a C++ MFC program and get the correct result HELP!!!! Thanks .Netter

    K Offline
    K Offline
    KrIstOfK
    wrote on last edited by
    #2

    did you try to provide an IFormatProvider? This is an option which you can define how it's formatted.

    C 1 Reply Last reply
    0
    • K KrIstOfK

      did you try to provide an IFormatProvider? This is an option which you can define how it's formatted.

      C Offline
      C Offline
      CSharpDavid
      wrote on last edited by
      #3

      its not the format , the date is totally incorrect, infact the date it reports is 12:01:53 AM on 1/1/0001. Ive tried all the .From options, none work.

      K 1 Reply Last reply
      0
      • C CSharpDavid

        its not the format , the date is totally incorrect, infact the date it reports is 12:01:53 AM on 1/1/0001. Ive tried all the .From options, none work.

        K Offline
        K Offline
        KrIstOfK
        wrote on last edited by
        #4

        and Convert.ToDateTime() you can provide as good as everything, maybe that will help

        C 1 Reply Last reply
        0
        • K KrIstOfK

          and Convert.ToDateTime() you can provide as good as everything, maybe that will help

          C Offline
          C Offline
          CSharpDavid
          wrote on last edited by
          #5

          in fact it just throws and invalid cast exception (as the documentation says)

          K 1 Reply Last reply
          0
          • C CSharpDavid

            in fact it just throws and invalid cast exception (as the documentation says)

            K Offline
            K Offline
            KrIstOfK
            wrote on last edited by
            #6

            Then i think you can use the same solution as i've just given down here:

            		string strDate = "20060502";
            		DateTime dtDate = DateTime.ParseExact( strDate, "yyyyMMdd", CultureInfo.InvariantCulture );
            		strDate = dtDate.ToString();
            

            Where y stands vor years M for months d for days h for hours m for minutes s for seconds Should this work?

            C 1 Reply Last reply
            0
            • K KrIstOfK

              Then i think you can use the same solution as i've just given down here:

              		string strDate = "20060502";
              		DateTime dtDate = DateTime.ParseExact( strDate, "yyyyMMdd", CultureInfo.InvariantCulture );
              		strDate = dtDate.ToString();
              

              Where y stands vor years M for months d for days h for hours m for minutes s for seconds Should this work?

              C Offline
              C Offline
              CSharpDavid
              wrote on last edited by
              #7

              The value is saved in the file as an int32, not a string, the problem is that the .net datetime doesnt interpret this value the same way MFC does with CTime.

              B 1 Reply Last reply
              0
              • C CSharpDavid

                The value is saved in the file as an int32, not a string, the problem is that the .net datetime doesnt interpret this value the same way MFC does with CTime.

                B Offline
                B Offline
                BambooMoon
                wrote on last edited by
                #8

                You say the value is saved in the file as an int32. Is it the same as time_t (which is 32 bits)? If so, the C# DateTime is obtained as follows, where the variable "timet" contains your 32 bit time_t value: DateTime.FromFileTime(10000000 * (long)timet + 116444736000000000)

                C 1 Reply Last reply
                0
                • B BambooMoon

                  You say the value is saved in the file as an int32. Is it the same as time_t (which is 32 bits)? If so, the C# DateTime is obtained as follows, where the variable "timet" contains your 32 bit time_t value: DateTime.FromFileTime(10000000 * (long)timet + 116444736000000000)

                  C Offline
                  C Offline
                  CSharpDavid
                  wrote on last edited by
                  #9

                  i can't check it at the moment but that sounds about right. So thank you very much. Where did you fid that strange bit of information. Can't think I would have come up with that.

                  B 1 Reply Last reply
                  0
                  • C CSharpDavid

                    i can't check it at the moment but that sounds about right. So thank you very much. Where did you fid that strange bit of information. Can't think I would have come up with that.

                    B Offline
                    B Offline
                    BambooMoon
                    wrote on last edited by
                    #10

                    >> Where did you find that strange bit of information It is not so strange if you analyze it. Those aren't "magical" numbers in that equation: DateTime.FromFileTime(10000000 * (long)timet + 116444736000000000) You can see from the method name that the quantity in parentheses is a FileTime. You already know that time_t is seconds since 1/1/1970. A FileTime is a long representing 100 nanosecond intervals since 1/1/1601. So all you need to do is multiply the time_t by 1e7 to get 100 nanosecond intervals, and then add the number of 100 nanosecond intervals between 1/1/1601 and 1/1/1970, which is easily calculated. Perfectly logical, no? Hahaha.

                    C 1 Reply Last reply
                    0
                    • B BambooMoon

                      >> Where did you find that strange bit of information It is not so strange if you analyze it. Those aren't "magical" numbers in that equation: DateTime.FromFileTime(10000000 * (long)timet + 116444736000000000) You can see from the method name that the quantity in parentheses is a FileTime. You already know that time_t is seconds since 1/1/1970. A FileTime is a long representing 100 nanosecond intervals since 1/1/1601. So all you need to do is multiply the time_t by 1e7 to get 100 nanosecond intervals, and then add the number of 100 nanosecond intervals between 1/1/1601 and 1/1/1970, which is easily calculated. Perfectly logical, no? Hahaha.

                      C Offline
                      C Offline
                      CSharpDavid
                      wrote on last edited by
                      #11

                      Went to work , tested and thats it.Thank you

                      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