std::get_time() Function in C++
Last Updated :
21 Mar, 2024
In C++, the function std::get_time() is a standard library function that is used to parse the given input as date and time value as specified in the format string that is passed as the argument. It stores that parsed time in the object of the tm type.
std::get_time() is defined inside <ctime> header in C++.
Syntax of get_time()
get_time(tm* var, const CharT* format);
Parameters of get_time()
This function accepts two parameters:
- var: The variable to store the parsed data/time.
- format: This is the string that specifies the format of the date and time that will be entered as input.
Format String for get_time() in C++
The format string for the get_time() has its own set of rules for parsing the date/time data from the given input. These rules states different specifiers to parse the date/time in different format. The following table list some of the common specifiers:
Specifier | Description |
---|
%a | Abbreviated weekday (e.g., Mon, Tue) |
---|
%A | Full weekday (e.g., Monday, Tuesday) |
---|
%b | Abbreviated month (e.g., Jan, Feb) |
---|
%B | Full month (e.g., January, February) |
---|
%c | Day of the month with weekday (e.g., 02 Mon) (Less common) |
---|
%C | Count of the weekday within the year (range 00 to 53) |
---|
%d | Day of the month, 2 digits (range 00 to 31) |
---|
%D | Day of the year, 3 digits (range 000 to 366) |
---|
%F | Equivalent to %Y-%m-%d (ymd’s canonical format) |
---|
%g | ISO week date year without the century (range 00 to 99) |
---|
%G | ISO week date year including the century |
---|
%j | Equivalent to %D |
---|
%m | Month as a number (01 to 12) |
---|
%M | Minute (00 to 59) |
---|
%q | Quarter (01 to 04) (Less common) |
---|
%s | Seconds since January 1, 1970 (for timestamps) |
---|
%u | Day of the week (number, Sunday = 1) |
---|
%U | Week count, day of week is Sun (range 00 to 53) |
---|
%V | ISO week count, day of week is Mon (range 01 to 53) |
---|
%w | Day of the week (number, Sunday = 0) |
---|
%W | Week count, day of week is Mon (range 00 to 53) |
---|
%y | Year without a century (range 00 to 99) |
---|
%Y | Year including the century |
---|
%Z | Zone offset in hours and minutes (HH:MM) with a preceding sign (+ for offsets east of UTC, – for offsets west of UTC) |
---|
Please refer to the documentation for more specifiers.
Return Value of get_time()
This function does not return any value after its execution. It just stores the input data into the given argument.
Example of get_time() in C++
The below program demonstrates how to use the get_time() function in C++:
C++
// C++ program to demonstrate
// std::get_time() function in C++
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
// Initialize a string stream
istringstream ss("01-01-2000 00:00:00");
// Declare a tm struct
tm time;
// Parse the date and time using get_time()
ss >> get_time(&time, "%d-%m-%Y %H:%M:%S");
// Print the date and time
cout << "Year: " << time.tm_year + 1900
<< "\nMonth: " << time.tm_mon + 1
<< "\nDay: " << time.tm_mday
<< "\nHour: " << time.tm_hour
<< "\nMinute: " << time.tm_min
<< "\nSecond: " << time.tm_sec;
return 0;
}
OutputYear: 2000
Month: 1
Day: 1
Hour: 0
Minute: 0
Second: 0
Important Points About get_time()
- It is used to parse the given input as date and time value as specified in the format string that is passed as the argument.
- The format string can contain conversion specifiers, whitespace characters, and ordinary characters (except %). Each ordinary character should match one character in the input stream.
- Each conversion specification starts with a % character, optionally followed by an E or O modifier (which is ignored if not supported by the locale), and then followed by a character that determines the behavior of the specifier
- The format specifiers are the same as that of POSIX function
strptime()
.
Share your thoughts in the comments
Please Login to comment...