Increase the date / time syntax time to get a value compatible with .NET.

I would like to use C ++ / Boost to parse timelines such as 1980.12.06 21:12:04.232 , and get a ticks value that matches the number of ticks (used to initialize the .NET System.DateTime ). How can i do this?

Update: I need to use C ++; I cannot use C ++ / CLI for this.

+7
source share
2 answers
  • at .Net Start date 01/01/01 00:00:00
  • in boost ptime starts from 1400.01.01 00.00.00

// C ++ code

 #include <boost/date_time/posix_time/posix_time.hpp> int main(int argc, char* argv[]) { using namespace boost::posix_time; using namespace boost::gregorian; //C# offset till 1400.01.01 00:00:00 uint64_t netEpochOffset = 441481536000000000LL; ptime ptimeEpoch(date(1400,1,1), time_duration(0,0,0)); //note: using different format than yours, you'll need to parse the time in a different way ptime time = from_iso_string("19801206T211204,232"); time_duration td = time - netEpoch; uint64_t nano = td.total_microseconds() * 10LL; std::cout <<"net ticks = " <<nano + netEpochOffset; return 0; } 

// outputs 624805819242320000

in c # to check

 static void Main(string[] args) { DateTime date = new DateTime(1400,1,1); Console.WriteLine(date.Ticks); DateTime date2 = new DateTime(624805819242320000L); //C++ output Console.WriteLine(date2); /*output * 441481536000000000 * 6/12/1980 21:12:04 * */ return; } 
+4
source

.Net "Ticks" is at 100 nanosecond intervals.

ticksType: System.Int64 Date and time, expressed as 100-nanosecond intervals, from January 1, 0001, at 00: 00: 00.000 in the Gregorian calendar

Thus, you need a tick counter of a known era (for example, the Unix era), the number of days between the era and the desired date / time, as well as the time of day ( the total_nanoseconds accessor can help). Then you can easily calculate the equivalent .Net counter, with simple addition and multiplication.

You may have problems with the date range provided.

0
source

All Articles