c++ - std chrono 1 hour wrong when converting with ctime to char* -


I am trying to display the difference between high resolution time points in a string like 00:00:00. My problem is that when I print it, the time is 1 hour.

  #include & lt; Chrono & gt; # Include & lt; Ctime & gt; # Include & lt; Iostream & gt; #include & lt; Thread & gt; using namespace std; Using Namespace Cronos; Int main (int argc, char ** argv) {auto start = high_resolution_clock :: now (); This_thread :: sleep_for (seconds (1)); Auto stop = high_resolution_clock :: now (); Auto Result = Stop - Start; Time_t t = period_cast   

This prints

  Thursday 1 January 01:00:01 1970   

When I expect

However, the following print will be printed to:

  Thursday 1 January 00:00 1970   

Still print the following 1:

  cout & Lt; & Lt; (Int) period_cast & lt; Seconds & gt; (result). Count () & lt; & Lt; Endl;   

Perhaps there is something to do with the time zone in it? Any help is appreciated!

After discussions in comments that happened after you, I came up with the following:

  size_tmktimstamp (four * output, size_ty size) {milliseconds ms = period_cast & lt; Milliseconds & gt; (System_clock :: now (). Time_since_epoch ()); Seconds s = period_cast & lt; Seconds & gt; (M / s); Time_t ctime = (time_t) sount (); TM components; Size_t return_value = 0; If (! Localtime_r (& amp; ctime, & amp; component)) 0; If ((return_value = strftime (Output, Size, "% F% T: XXX", and Components)) == 0) Return 0; int ms_d1 = ms.count () / 100% 10, ms_d2 = ms.count () / 10% 10, ms_d3 = ms.count ()% 10; Output [Return_View - 3] = MS_D1 + '0'; Output [Return_View - 2] = MS_D2 + '0'; Output [Return_View - 1] = MS_D3 + '0'; Return return_value; }   

In short, you give it a character buffer and its size, and it writes it a timestamp (if the buffer is too small, then it returns zero, otherwise this total number If you want this timestamp in the form of std :: string , you can use the std :: string slice constructor, including zero terminator of characters in it Can:

  four buffers [64]; String timestamp (buffer, mktimstamp (buffer, size buffer));   

The string constructor will be read to zero terminator if MkTimestamp is successful, and if it fails, it will generate an empty string. You can probably consolidate this step in MkTimestamp if you wish.

This is different from your first attempt as follows:

  • Since you are trying to create a timestamp, you can use relative to complete, instead of time Are there.
  • Since we want to use full time, we should use the system_clock because it is the only clock that actually returns the time_point value Which can be meaningfully converted into time_t values ​​which will accept the old C family function. (Sometimes, these two watches are the same era, or the same watch, but not always.)
  • I chose a different time format. You can change it if you want it will not be difficult, I highly recommend that you keep millisecond placeholder in the format string, so you do not need to test any length until the success of strftime is.

    Credit where credit has been given (other stackoverphorph answers which are referred to when writing this reply)

    Basic Answer:

    (Note that the issue you describe does not happen.) Also, you can also go to #include & lt; Thread & gt;

    In addition, this indicates that its argument is expected to be a calendar date. You are interrupting this one second, which is more or less meaningless than itself. You are probably right that the time zone interpretation problem is showing this one hour off.

    I can not really give a better answer without much information about what you are trying to do.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -