Open In App

std::get_time() Function in C++

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

SpecifierDescription
%aAbbreviated weekday (e.g., Mon, Tue)
%AFull weekday (e.g., Monday, Tuesday)
%bAbbreviated month (e.g., Jan, Feb)
%BFull month (e.g., January, February)
%cDay of the month with weekday (e.g., 02 Mon) (Less common)
%CCount of the weekday within the year (range 00 to 53)
%dDay of the month, 2 digits (range 00 to 31)
%DDay of the year, 3 digits (range 000 to 366)
%FEquivalent to %Y-%m-%d (ymd’s canonical format)
%gISO week date year without the century (range 00 to 99)
%GISO week date year including the century
%jEquivalent to %D
%mMonth as a number (01 to 12)
%MMinute (00 to 59)
%qQuarter (01 to 04) (Less common)
%sSeconds since January 1, 1970 (for timestamps)
%uDay of the week (number, Sunday = 1)
%UWeek count, day of week is Sun (range 00 to 53)
%VISO week count, day of week is Mon (range 01 to 53)
%wDay of the week (number, Sunday = 0)
%WWeek count, day of week is Mon (range 00 to 53)
%yYear without a century (range 00 to 99)
%YYear including the century
%ZZone 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;
}

Output
Year: 2000
Month: 1
Day: 1
Hour: 0
Minute: 0
Second: 0

Important Points About get_time()

  1. 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.
  2. The format string can contain conversion specifiers, whitespace characters, and ordinary characters (except %). Each ordinary character should match one character in the input stream.
  3. 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
  4. The format specifiers are the same as that of POSIX function strptime().



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads