Is there a cross-platform way to get the current date and time in C++?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- How do you make an installer for your python progr
- Selecting only the first few characters in a strin
- Libgdx - Check if a key is being held down?
- What exactly do pointers store? (C++)
you could use C++ 11 time class:
out put:
localtime_s() version:
There's always the
__TIMESTAMP__
preprocessor macro.example: Sun Apr 13 11:28:08 2014
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.
New answer for an old question:
The question does not specify in what timezone. There are two reasonable possibilities:
For 1, you can use this date library and the following program:
Which just output for me:
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.
Which for me just output:
The result type from
make_zoned
is adate::zoned_time
which is a pairing of adate::time_zone
and astd::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:And the output changes to: