-2

How can I get the current time, in C++, without C libraries?

C has <ctime>. I am trying to avoid C libraries, for no good reason.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
tensor
  • 41
  • 2
  • 10
  • 1
    You can look into chrono library – Thomas Sablik Jun 08 '20 at 09:35
  • [This](https://stackoverflow.com/a/27856440/13534897) answer answers how to get a time interval, but not how to get the _current_ time. – tensor Jun 08 '20 at 09:35
  • @ThomasSablik I have been looking into [chrono](https://en.cppreference.com/w/cpp/header/chrono), but can't find what I'm looking for. – tensor Jun 08 '20 at 09:37
  • 2
    [std::chrono::system_clock::now](https://en.cppreference.com/w/cpp/chrono/system_clock/now) Returns a time point representing the current point in time. – Thomas Sablik Jun 08 '20 at 09:37
  • @ThomasSablik And how do I use it? I managed to use it to get a difference and print it out. Printing current time has been unsuccessful. – tensor Jun 08 '20 at 09:43
  • 1
    Your initial question was _"How can I get the current time, in C++, without C libraries?"_ `std::chrono::system_clock::now` returns the current time point. How to print it, is a different question. – Thomas Sablik Jun 08 '20 at 09:46
  • 1
    Do not edit questions in ways that change them substantially and invalidate existing answers. If you have another question, you can ask it in a new post. – Eric Postpischil Jun 08 '20 at 10:51
  • 1
    Is additionally printing the current time substantially different? I would have thought it to be a minor addition. – tensor Jun 08 '20 at 11:09
  • @EricPostpischil In what way did OP's edit "invalidate existing answers?". As far as I can see the edit *expanded* the question but did not modify the original question at all. – JBentley Jun 08 '20 at 11:15
  • 1
    @JBentley: An answer that is complete and deserves a vote up for one question is incomplete and may not deserve a vote up for two questions. Once a person posts a good and complete answer, and the post is changed to ask a second question, other users may come along, see the incomplete answer, and vote it down for failing to answer the questions. Thus, a person who made a genuine and useful effort to answer a question can undeservedly accrue negative reputation. – Eric Postpischil Jun 08 '20 at 11:58
  • @JBentley: Additionally, people who search for the second question, find a matching post in the search results, and view it in a desire to see the answer will be disappointed because the posted answer does not answer their question. – Eric Postpischil Jun 08 '20 at 12:01
  • @EricPostpischil You are assuming that new and complete answers will not be added – tensor Jun 08 '20 at 12:10
  • @tensor: No, I am not. Since you do not specifically indicate which part of my statements you are referring to, perhaps it is the statement that people will be disappointed. Perhaps on some occasions, they will not be completely disappointed in the set of answers, if other authors have added more. But they will nonetheless be disappointed in the earlier answer that does not answer the question they searched for. – Eric Postpischil Jun 08 '20 at 12:15
  • @EricPostpischil All of those are valid and reasonable points, but none of them *invalidate* the answer; they merely turn them into partial answers (which are perfectly fine for posts which ask mutiple questions). In any case that isn't what has happened here. OP posted a question, got an answer which didn't address what he was *really* asking, so he edited to make it clearer. I don't think it is unreasonable that someone who asks "how do I get the current time?" expects an answer that he can *actually use in a meaningful way*, and being able to print that time is a solid indicator of that. – JBentley Jun 08 '20 at 13:23
  • @EricPostpischil Indeed, taken in this context, the accepted answer doesn't answer the question at all. OP asked how to get the current time and referred to to hint at what he is looking for. The answer returns a "time point" which is not the same thing and requires further manipulation (not provided in the answer) to get an actual time. To tell OP that he must post a separate question to get a useable answer is unnecessarily pedantic in this case. I agree with you in the more general case - I just think applying that approach here is unnecessarily harsh on the OP. – JBentley Jun 08 '20 at 13:31
  • Combined with the fact that OP has received 3 downvotes with zero feedback in spite of his question (edits aside) being well written and on-topic for the site, we've not treated OP very well IMO. – JBentley Jun 08 '20 at 13:33
  • Thank you for your feedback and support. Is there anything I can do to improve the question? – tensor Jun 08 '20 at 13:49
  • Also, when testing `system_clock::now()` it can be converted to `time_t` with `system_clock::to_time_t(t)`, which can easily be printed and worked with; but `time_t` is defined in header `ctime` (curiously, `time_t` is available, even though I haven't included `ctime`). – tensor Jun 08 '20 at 13:52
  • `chrono` implicitly includes `ctime` in most implementations. – Thomas Sablik Jun 09 '20 at 11:22

3 Answers3

3

You can use std::chrono::system_clock::now() from chrono library. It returns a time point representing the current point in time.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
0

system_clock::now() returns a time_point object representing the current time.

The time point object can be converted to time_t with system_clock::to_time_t(), which can be printed and manipulated further.

#include <iostream>
#include <chrono>

int main() {
    std::chrono::time_point t = std::chrono::system_clock::now();
    time_t t_time_t = std::chrono::system_clock::to_time_t(t);
    std::cout << t_time_t << '\n';
}

Here time_t is imported from <chrono>, even though it is a C type.

tensor
  • 41
  • 2
  • 10
-1

You can use time.h to get the time in seconds as below,

include <time.h>

time_t timeSec;
time(&timeSec);

println("Current time in seconds : \t%lld\n", (long long)timeSec);