-3

In c++:

  • How can I display the system current time (only time without the date) ?

  • How can I display current time + 3 minutes?

  • Can I do it using cout?

Which libraries or functions to use ?

Sahar Alsadah
  • 2,580
  • 3
  • 19
  • 25

1 Answers1

1

You can try this example (use c++11 clang 3.6):

#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>

int main()
{
    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
    std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));
    std::cout << std::put_time(std::localtime(&now_c), "%T") << std::endl;
    std::time_t later_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24) + std::chrono::minutes(3));
    std::cout << std::put_time(std::localtime(&later_c), "%T") << std::endl;

    return 0;
}

just use std::chrono.

pezy
  • 1,022
  • 2
  • 8
  • 27
  • 2
    `std::put_time` is poorly supported, to the extent that [GCC 4.9 doesn't have it](http://stackoverflow.com/a/14137287/560648) and neither does clang 3.5.0. So unless you're using, what, MSVS(?) this isn't going to be too helpful. – Lightness Races in Orbit Oct 17 '14 at 15:48
  • 1
    @LightnessRacesinOrbit Well, at least it's described as a standard function in [this reference](http://en.cppreference.com/w/cpp/io/manip/put_time) – πάντα ῥεῖ Oct 17 '14 at 15:50
  • 2
    @πάνταῥεῖ Yes, it's a C++11 feature. I just said that it is poorly supported, and I stand by that statement. It's great that cppreference.com is accurate to the letter of the standard, and I suppose that's helpful if you're going to start a career as a cppreference.com contributor... but, for all other C++ use, not so much. – Lightness Races in Orbit Oct 17 '14 at 15:51
  • 1
    I use Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn) and it seem to work well. – pezy Oct 17 '14 at 15:54