I have following situation: I'm writing two programs, the first one starts with windows and writes the current system time in a text document. The code for this looks like this: auto start = std::chrono::system_clock::now(); std::time_t start_time = std::chrono::system_clock::to_time_t(start);  ofstream Write (&quot;Time.txt&quot;); Write &lt;&lt; std::ctime(&amp;start_time);  The date format looks like this: Thu Jul 16 14:19:53 2020. My second program autostarts with the shutdown. It should build the difference between the current system time and the starting time and write the difference in a different text document. #include &lt;iostream&gt; #include &lt;ctime&gt; #include &lt;string&gt; #include &lt;cmath&gt; #include &lt;fstream&gt; #include &lt;chrono&gt; #include &lt;windows.h&gt;  using namespace std;  double k; //Get leftover time from txt doc// int s;    //Seconds// int m;    //Minutes//    int h;    //Hours// int d;    //Days// long int z;    //Var containing new leftover time//   int main() {      ifstream Time (&quot;Time.txt&quot;);         Time &gt;&gt; start;         //Get starting time//       ifstream Check (&quot;Test.txt&quot;);         Check &gt;&gt; d; //Reading days from txt doc to d//         Check &gt;&gt; h; //Reading hours from txt doc to h//         Check &gt;&gt; m; //Reading minutes from txt doc to m//         Check &gt;&gt; s; //Reading seconds from txt doc to s//      k = (s + 60*m + 3600*h + 24*3600*d);      auto end = std::chrono::system_clock::now();     //Get current time ending the program//      std::chrono::duration&lt;double&gt; elapsed_seconds = end-start;      //Calculate difference between start and end//      z = (k-round(elapsed_seconds.count()));      //Calculate time difference in seconds//       //Converting seconds into days, hours, minutes, seconds//     d = z / (24*3600);     h = (z / 3600) % 24;     m = (z / 60) % 60;     s = z % 60;     //End of converting//      ofstream Write (&quot;Test.txt&quot;);         Write &lt;&lt; d &lt;&lt; &quot;\n&quot; &lt;&lt; h &lt;&lt; &quot;\n&quot; &lt;&lt; m &lt;&lt; &quot;\n&quot; &lt;&lt; s;           //Write all values back to txt doc//      }  The problem I have is that I don't have an idea on how to get the starting time from the Time.txt in a format I can use in this chrono operation std::chrono::duration&lt;double&gt; elapsed_seconds = end-start. I've tried to set it up like in the startup program std::time_t start_time = std::chrono::system_clock::to_time_t(start); ifstream Time (&quot;Time.txt&quot;); Time &gt;&gt; start;  The result: a ridiculous negative number in the Test.txt which is obviously wrong considering I didn't mess with the system time. A string variable didn't help me much either. It gets the job done copying the txt line to the string variable but afterwards I didn't manage to transform it into the right chrono variable. I hope someone has an idea on how to do this. I'm thanking you already for your answers.
120848
How do I convert a date string, formatted as "MM-DD-YY HH:MM:SS", to a time_t value in either C or C++?
18594
What is the module/method used to get the current time?
2899
I need a 'good' way to initialize the pseudo-random number generator in C++. I've found  that states:     In order to generate random-like   numbers, srand is usually initialized   to some distinctive value, like those   related with the execution time. For   example, the value returned by the   function time (declared in header   ctime) is different each second, which   is distinctive enough for most   randoming needs.   Unixtime isn't distinctive enough for my application. What's a better way to initialize this? Bonus points if it's portable, but the code will primarily be running on Linux hosts.  I was thinking of doing some pid/unixtime math to get an int, or possibly reading data from /dev/urandom.  Thanks!  EDIT  Yes, I am actually starting my application multiple times a second and I've run into collisions.
86757
If I have a native C++ windows program (i.e. the entry point is WinMain) how do I view output from console functions like std::cout?
179
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really compelling backwards-compatible reason for it, particularly because of the regional settings issue with using date. See @npocmaka's     What's a Windows command line statement(s) I can use to get the current datetime in a format that I can put into a filename?  I want to have a .bat file that zips up a directory into an archive with the current date and time as part of the name, for example, Code_2008-10-14_2257.zip. Is there any easy way I can do this, independent of the regional settings of the machine?  I don't really mind about the date format, ideally it'd be yyyy-mm-dd, but anything simple is fine.  So far I've got this, which on my machine gives me Tue_10_14_2008_230050_91:  rem Get the datetime in a format that can go in a filename. set _my_datetime=%date%_%time% set _my_datetime=%_my_datetime: =_% set _my_datetime=%_my_datetime::=% set _my_datetime=%_my_datetime:/=_% set _my_datetime=%_my_datetime:.=_%  rem Now use the timestamp by in a new ZIP file name. "d:\Program Files\7-Zip\7z.exe" a -r Code_%_my_datetime%.zip Code   I can live with this, but it seems a bit clunky. Ideally it'd be briefer and have the format mentioned earlier.  I'm using Windows Server 2003 and Windows&nbsp;XP Professional. I don't want to install additional utilities to achieve this (although I realise there are some that will do nice date formatting).
51678
Today I was installing Windows 7 on the same computer I have Ubuntu. For some reason, after installing Windows 7 and going back to Ubuntu the time went from the correct hour at that moment, 12:30PM to 7:30AM. This happened after installing Windows 7. For what I understand the BIOS has a time set, Ubuntu has the same time that is shown in the BIOS when installing it. At least up to this point is what I have seen. Now after installing Windows 7 (And believe me this is only for testing the performance of Windows 7 against Ubuntu in which case Windows 7 is falling behind a lot on the same hardware) the time changes in Ubuntu to the wrong one that is set in Windows 7.  I actually do not change the time in Windows 7 since this is one of the checks I am doing to see if the operating system can correctly grab the time. Windows 7 fails. Ubuntu does not BUT Windows 7 actually changes the time for Ubuntu if I boot with it afterwards.  So my question is, why/how does time change in Ubuntu after installing Windows.  NOTE - I have set Ubuntu time to "manual" instead of grabbing it from the Internet just to test out this.
50684
I'm starting out in Visual C++ and I'd like to know how to keep the console window.  For instance this would be a typical "hello world" application:   int _tmain(int argc, _TCHAR* argv[]) {     cout &lt;&lt; "Hello World";     return 0; }   What's the line I'm missing?
115062
I noticed that the clock of my Windows 10 Desktop has been set back one hour every day I boot the system for the first time. The three possible auto-settings afaik for changing the time automatically (automatic setting of time, timezone and summer/winter offset) are off. The timezone has been set correctly during installation and I never touched it. I adjusted the time every morning for the past couple of days.  For some time I had all auto-settings on (or just the adjust time and summer/winter automatically with the timezone CET/Berlin set manually without auto-update) which didn't change the time to the correct value over days and multiple reboots.  I'm running Windows 10 in a DualBoot with Ubuntu 18.10 which has the same settings regarding time (auto-update), timezone (CET/Berlin without auto-update) and summer/winter offset (auto-update) and it works fine there.
68227
I want a screen shot to be saved automatically to a location I specify, as soon as I press the Prt Scr button on my keyboard, or the Alt+Prt Scr combo. I also want the date and time to be used as file name for each new screen shot. And I want the files to be saved as PNG images.  Is there a Windows program that would allow me to do just this? I don't want any other fancy features like editing and making notes, etc. just a plain simple program that will either automatically save the screen shots to a pre-set location or prompt me where I want to save it. I don't want to go through MS Paint to save the shots.  I guess what I'm after is a little bit of Linux behavior of Print Screen, but only for Windows. And I additionally want the files to be named by the date and time they were taken.  Update:  Meanwhile, I have found one called Greenshot. But I don't know how to get the file name pattern right.  This is the default pattern.  ${capturetime:d"yyyy-MM-dd HH_mm_ss"}-${title}   Can someone explain this? Why do they have the title capturetime and the quotation marks? It produces file names like this.  2013-01-26 15_24_28-Greenshot.png   And this is what it says about the file patterns in the program description.  ${YYYY} year, 4 digits ${MM} month, 2 digits ${DD} day, 2 digits ${hh} hour, 2 digits ${mm} minute, 2 digits ${ss} second, 2 digits ${NUM} incremental number, 6 digits ${title} Window title ${user} Windows user ${domain} Windows domain ${hostname} PC name   How do I get files that include year, month and day, separated by a dash, followed by hour, minute and second? I want to include a time for uniqueness of the file name.
53092
This should be a fairly trivial problem. I'm trying to open an ofstream using a std::string (or std::wstring) and having problems getting this to work without a messy conversion.  std::string path = ".../file.txt";  ofstream output;  output.open(path);   Ideally I don't want to have to convert this by hand or involve c-style char pointers if there's a nicer way of doing this?
82090
int i = 4; string text = "Player "; cout &lt;&lt; (text + i);   I'd like it to print Player 4.  The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes?
113186
Is there any way to take advantage of the file creation flags in the Win32 API such as FILE_FLAG_DELETE_ON_CLOSE or FILE_FLAG_WRITE_THROUGH as described here  , but then force that handle into a std::ofstream?  The interface to ofstream is obviously platform independent; I'd like to force some platform dependent settings in 'under the hood' as it were.
112148
I'm reading Malcolm Clark's A Plain TeX Primer. On page 111 there's an exercise to define a macro that prints the current time. I've reproduced Clark's answer below:  \newcount\minleft \newcount\timehour \def\thetime{\timehour=\time   \divide\timehour by60 %gives 24 hour part of clock   \minleft=\timehour   \multiply\minleft by-60 %   \advance\minleft by\time    %minutes after hour   \ifnum\time&gt;720\advance\timehour by-12\fi\relax   \number\timehour:\ifnum\minleft&lt;10 %                    0\fi\relax\number\minleft   \ifnum\time&gt;720~p.m.\else~a.m.\fi}   My problem is that I don't know what \relax is doing in lines 8 and 10 (after each \fi). When I use this macro, it prints the time as it's supposed to:  8:51 p.m.  If I delete the \relax in line 8, I get the time but with the hour missing:  :51 p.m.  If I delete the \relax in line 10 (and restore the \relax in line 8), the macro works again like it did the first time:  8:51 p.m.  I naively thought that the \fi that ends the \ifnum block ends the processing of whatever's going on in the \ifnum ... \fi, and that it wouldn't matter whether \relax is there or not, in either place. Clearly that's not the case, but I can't figure out what's happening in line 8, and also why the \relax doesn't seem to be necessary in line 10.  In a different context the author does explain that \relax is a "do nothing" command that (in this other context) prevents TeX from treating the expression before the \relax and after the \relax parts of the same expression. I get that. But concerning \relax after an \fi, he says:  "The \relax is probably not required, but it's always nice to have one in: perhaps an example of 'safe TeX'."  This might be good advice, but it is not enlightening.  I found the following questions about \relax on tex.se, but I don't think they answer my question (or at least I'm not smart enough to figure it out):          So, what is \relax actually doing in this macro?
72988
Can anyone tell me the difference between break and continue statements?
43435
I have two files, say file1.tex and file2.tex. file1.tex is the first of a series of files, and it begins with counters (say chapter 1 for instance, although my counters are not used for chapters). Then at the end of file1, the counter for chapters is at say 7 ; I want to carry the value of the counter to file2.tex so that I can use a counter which will have the value 7 in file2.tex. In this way I could possibly carry on the counter in file2.tex to a counter in some file3.tex and so on.   I assume I would need to compile file1.tex first, and then file2.tex, and then file3.tex... of course.  Is that possible? And if so, how? I couldn't think of anything.
34094
I'm designing a website for a client that has some irregular trading hours. Some days they remain open until 2am, others they close at 10pm. They want these hours displayed on their landing page, so visitors can see what time they open &amp; close at a glance. The trouble is I have to work with an API that returns integers only. Based on these integers and the current time, I can display a currently open or currently closed flag accordingly. As it stands currently, this isn't possible. I can't use custom text anywhere, only integers (24 hour time). I'm having some difficulty working out how to display the time. Currently the client displays their hours like this;  Mon 8am - 2am Tue 8am - 2am Wed 8am - 10pm Thu 8am - 2am Fri 8am - 10pm  However I don't think this is correct, and looks quite confusing (what is 8am - 2am?) shouldn't it be;  Mon 8am - 12am &amp; 12am - 2am Tue 8am - 12am &amp; 12am - 2am Wed 8am - 10pm Thu 8am - 12am &amp; 12am - 2am Fri 8am - 10pm  Admittedly, this also looks quite confusing! Any ideas how I can achieve this in the most user friendly way possible, without using custom text?
51244
I'm looking for an easy-to-read way to format time and date. The time and date will be displayed in a piece of software, and will be used to tell when an event occurred. For example,      "A temperature spike occurred at [t&amp;d]".    Shorter versions of the time and date will be displayed where appropriate. They will use the format YYYY/MM/DD HH:MM:SS UTC±HH:MM.  Given the software user might be in a different timezone, I feel that using natural language (and saying an event occurred 'Yesterday' or 'Last week') is not ideal. I would like to know what other formats of time and date are standard. Currently I hypothesize that the two formats will be:    "Commonwealth" format: DAY, DD MON YYYY at HH:MM:SS (UTC±HH:MM) eg.      Tue, 30th July 2013 at 15:42 (UTC+13)  US format: DAY, MON DD YYYY at HH:MM:SS (UTC±HH:MM) eg.      Tue, July 30th 2013 at 15:42 (UTC+13)    DAY is a 3-letter abbreviation for the day  MON is a 3-letter abbreviation for the month unless the month has 4 letters in which case, just use that  The year is optional and will default to the current year if not specified    Are my date formats correct and good natural language? Or are there better ones?  Thanks in advance!