0

I have looked all over for ways to get the current time in C++ for a Console Application project, but every method I have found has been rejected by Visual Studio as incorrect or deprecated, even with #define tags to keep the program from deprecating the functions. What is the current method to get the current time in a Visual Studio C++ Win32 console application?

I.A.S.
  • 81
  • 2
  • 11
  • what did you try ? GetLocalTime should do it ... – willll Nov 10 '16 at 17:46
  • 2
    Please, list at least **every method I have found has been rejected by Visual Studio** – J. Piquard Nov 10 '16 at 17:47
  • [`std::chrono::system_clock::now()`](http://en.cppreference.com/w/cpp/chrono/system_clock/now) should do the trick. – πάντα ῥεῖ Nov 10 '16 at 17:49
  • Also be a great idea to edit the question to add in any display formatting requirements you have. πάνταῥεῖ's comment and answer provide the modern C++ solution to get a point in time (`std::chrono::time_point`), but if you want ta time and date string for the user to read, there is [a lot more work coming](http://stackoverflow.com/questions/12835577/how-to-convert-stdchronotime-point-to-calendar-datetime-string-with-fraction). – user4581301 Nov 10 '16 at 18:05

2 Answers2

0

You may use DateTime::Now

using namespace System;
using namespace System::Globalization;

void main()
{
   DateTime localDate = DateTime::Now;
   array<String^>^ cultureNames = { "en-US", "en-GB", "fr-FR",
                                    "de-DE", "ru-RU" };

   for each (String^ cultureName in cultureNames) {
      CultureInfo^ culture = gcnew CultureInfo(cultureName);
      Console::WriteLine("{0}: {1}", cultureName,
                         localDate.ToString(culture));
   }
}

For more information, have a look into MSDN

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
Dmitry
  • 276
  • 2
  • 4
  • May be, but not a portable solution unfortunately. – πάντα ῥεῖ Nov 10 '16 at 17:50
  • 3
    This solution is not C++. It's C++ with .Net Framework extensions. Not necessarily a bad thing since the stated target is Visual Studio, but users of this solution should be made aware. – user4581301 Nov 10 '16 at 17:56
  • Yes, agree. But if a person who asked the question use the not a portable solution, I think it can help him. if we should found a portable solution, we definetly should see into standard library, for example localtime() routine can help. But I suppose VS 2015 considers this one as a deprecated as well. – Dmitry Nov 10 '16 at 17:56
  • @user4581301 yep, exactly. but seems, it is not a problem for user of VS – Dmitry Nov 10 '16 at 17:58
0

Feel free to use this free, open-source modern C++ library:

#include "tz.h"
#include <iostream>

int
main()
{
    std::cout << date::make_zoned(date::current_zone(),
                                  std::chrono::system_clock::now()) << '\n';
}

See the installation section for details on how to install this on Windows. It is known to work with VS-2015.

The library is thread safe and uses no deprecated functionality. It also does a lot more things than just getting the current local time.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577