How to get current time and date in C++?

后端 未结 24 1479
刺人心
刺人心 2020-11-22 06:55

Is there a cross-platform way to get the current date and time in C++?

相关标签:
24条回答
  • 2020-11-22 07:31

    localtime_s() version:

    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
      time_t current_time;
      struct tm  local_time;
    
      time ( &current_time );
      localtime_s(&local_time, &current_time);
    
      int Year   = local_time.tm_year + 1900;
      int Month  = local_time.tm_mon + 1;
      int Day    = local_time.tm_mday;
    
      int Hour   = local_time.tm_hour;
      int Min    = local_time.tm_min;
      int Sec    = local_time.tm_sec;
    
      return 0;
    } 
    
    0 讨论(0)
  • 2020-11-22 07:34

    C++ shares its date/time functions with C. The tm structure is probably the easiest for a C++ programmer to work with - the following prints today's date:

    #include <ctime>
    #include <iostream>
    
    int main() {
        std::time_t t = std::time(0);   // get time now
        std::tm* now = std::localtime(&t);
        std::cout << (now->tm_year + 1900) << '-' 
             << (now->tm_mon + 1) << '-'
             <<  now->tm_mday
             << "\n";
    }
    
    0 讨论(0)
  • 2020-11-22 07:35

    the C++ standard library does not provide a proper date type. C++ inherits the structs and functions for date and time manipulation from C, along with a couple of date/time input and output functions that take into account localization.

    // Current date/time based on current system
    time_t now = time(0);
    
    // Convert now to tm struct for local timezone
    tm* localtm = localtime(&now);
    cout << "The local date and time is: " << asctime(localtm) << endl;
    
    // Convert now to tm struct for UTC
    tm* gmtm = gmtime(&now);
    if (gmtm != NULL) {
    cout << "The UTC date and time is: " << asctime(gmtm) << endl;
    }
    else {
    cerr << "Failed to get the UTC date and time" << endl;
    return EXIT_FAILURE;
    }
    
    0 讨论(0)
  • 2020-11-22 07:35

    New answer for an old question:

    The question does not specify in what timezone. There are two reasonable possibilities:

    1. In UTC.
    2. In the computer's local timezone.

    For 1, you can use this date library and the following program:

    #include "date.h"
    #include <iostream>
    
    int
    main()
    {
        using namespace date;
        using namespace std::chrono;
        std::cout << system_clock::now() << '\n';
    }
    

    Which just output for me:

    2015-08-18 22:08:18.944211
    

    The date library essentially just adds a streaming operator for std::chrono::system_clock::time_point. It also adds a lot of other nice functionality, but that is not used in this simple program.

    If you prefer 2 (the local time), there is a timezone library that builds on top of the date library. Both of these libraries are open source and cross platform, assuming the compiler supports C++11 or C++14.

    #include "tz.h"
    #include <iostream>
    
    int
    main()
    {
        using namespace date;
        using namespace std::chrono;
        auto local = make_zoned(current_zone(), system_clock::now());
        std::cout << local << '\n';
    }
    

    Which for me just output:

    2015-08-18 18:08:18.944211 EDT
    

    The result type from make_zoned is a date::zoned_time which is a pairing of a date::time_zone and a std::chrono::system_clock::time_point. This pair represents a local time, but can also represent UTC, depending on how you query it.

    With the above output, you can see that my computer is currently in a timezone with a UTC offset of -4h, and an abbreviation of EDT.

    If some other timezone is desired, that can also be accomplished. For example to find the current time in Sydney , Australia just change the construction of the variable local to:

    auto local = make_zoned("Australia/Sydney", system_clock::now());
    

    And the output changes to:

    2015-08-19 08:08:18.944211 AEST
    

    Update for C++20

    This library is now largely adopted for C++20. The namespace date is gone and everything is in namespace std::chrono now. And use zoned_time in place of make_time. Drop the headers "date.h" and "tz.h" and just use <chrono>.

    As I write this, partial implementations are just beginning to emerge on some platforms.

    0 讨论(0)
  • 2020-11-22 07:36

    You could use boost:

    #include <boost/date_time/gregorian/gregorian.hpp>
    #include <iostream>
    using namespace boost::gregorian;
    
    int main()
    {
        date d = day_clock::universal_day();
        std::cout << d.day() << " " << d.month() << " " << d.year();
    }
    
    0 讨论(0)
  • 2020-11-22 07:39

    std::ctime

    Why was ctime only mentioned in the comments so far?

    #include <ctime>
    #include <iostream>
     
    int main()
    {
        std::time_t result = std::time(nullptr);
        std::cout << std::ctime(&result);
    }
    

    Output

    Tue Dec 27 17:21:29 2011

    0 讨论(0)
提交回复
热议问题