codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

81 / 87 topics
76<iostream> Reference77<fstream> Reference78<cmath> Reference79<string> Reference80<cstring> Reference81<ctime> Reference82<vector> Reference83<algorithm> Reference
Tutorials/C++ Programming/&lt;ctime&gt; Reference
⚡C++ Programming

&lt;ctime&gt; Reference

Updated 2026-05-12
30 min read

<ctime> Reference

The &lt;ctime&gt; library in C++ provides a set of functions to work with date and time. These functions are essential for applications that require tracking the passage of time, scheduling tasks, or logging events based on timestamps. Understanding how to use these functions can greatly enhance your ability to develop robust and efficient C++ programs.

Introduction

The &lt;ctime&gt; library includes several functions that allow you to manipulate dates and times. Some of the most commonly used functions are:

  • time: Retrieves the current time.
  • difftime: Calculates the difference between two points in time.
  • mktime: Converts a broken-down time structure into a calendar time.
  • localtime: Converts a calendar time to local time.
  • gmtime: Converts a calendar time to Coordinated Universal Time (UTC).
  • strftime: Formats a date and time according to locale-specific format strings.
  • clock: Retrieves the amount of processor time used by the program since its start.
  • CLOCKS_PER_SEC: A constant that represents the number of clock ticks per second.

These functions provide a comprehensive toolkit for handling time-related tasks in C++.

Core Content

1. Retrieving Current Time with time

The time function retrieves the current calendar time as a time_t object, which is often used to represent time in Unix-like systems.

time_example.cpp
1#include <ctime>
2#include <iostream>
3
4int main() {
5 std::time_t now = std::time(nullptr);
6 std::cout << "Current time: " << now << std::endl;
7 return 0;
8}
Output

3. Converting Broken-Down Time to Calendar Time with mktime

The mktime function converts a broken-down time structure (tm) into a calendar time (time_t). This is useful for setting specific dates and times.

mktime_example.cpp
1#include <ctime>
2#include <iostream>
3
4int main() {
5 std::tm t = {};
6 t.tm_year = 2023 - 1900; // Year since 1900
7 t.tm_mon = 11; // Month, 0-11
8 t.tm_mday = 25; // Day of the month, 1-31
9
10 std::time_t calendar_time = std::mktime(&t);
11 if (calendar_time == -1) {
12 std::cerr << "Invalid time" << std::endl;
13 } else {
14 std::cout << "Calendar time: " << calendar_time << std::endl;
15 }
16 return 0;
17}
Output

5. Converting Calendar Time to UTC with gmtime

The gmtime function converts a calendar time (time_t) into a broken-down time structure (tm) representing Coordinated Universal Time (UTC).

gmtime_example.cpp
1#include <ctime>
2#include <iostream>
3
4int main() {
5 std::time_t now = std::time(nullptr);
6 std::tm* utc_time = std::gmtime(&now);
7
8 if (utc_time == nullptr) {
9 std::cerr << "Invalid time" << std::endl;
10 } else {
11 std::cout << "UTC time: "
12 << utc_time->tm_year + 1900 << "-"
13 << utc_time->tm_mon + 1 << "-"
14 << utc_time->tm_mday << " "
15 << utc_time->tm_hour << ":"
16 << utc_time->tm_min << ":"
17 << utc_time->tm_sec << std::endl;
18 }
19 return 0;
20}
Output

7. Measuring Processor Time with clock

The clock function retrieves the amount of processor time used by the program since its start.

clock_example.cpp
1#include <ctime>
2#include <iostream>
3
4int main() {
5 std::clock_t start = std::clock();
6 // Simulate some operations
7 for (int i = 0; i < 1000000; ++i) {}
8 std::clock_t end = std::clock();
9
10 double cpu_time_used = static_cast<double>(end - start) / CLOCKS_PER_SEC;
11 std::cout << "CPU time used: " << cpu_time_used << " seconds" << std::endl;
12 return 0;
13}
Output

Practical Example

Let's create a complete program that demonstrates the use of several &lt;ctime&gt; functions to log an event with its timestamp.

practical_example.cpp
1#include <ctime>
2#include <iostream>
3#include <cstring>
4
5void logEvent(const char* message) {
6 std::time_t now = std::time(nullptr);
7 char buffer[80];
8 std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", std::localtime(&now));
9 std::cout << "[" << buffer << "] " << message << std::endl;
10}
11
12int main() {
13 logEvent("Program started");
14 // Simulate some operations
15 for (int i = 0; i < 1000000; ++i) {}
16 logEvent("Operations completed");
17 return 0;
18}
Output
[2023-1-1 12:34:56] Program started
[2023-1-1 12:34:57] Operations completed

Summary

FunctionDescription
timeRetrieves the current time as a time_t object.
difftimeCalculates the difference between two time_t objects.
mktimeConverts a broken-down time structure into a calendar time.
localtimeConverts a calendar time to local time.
gmtimeConverts a calendar time to Coordinated Universal Time (UTC).
strftimeFormats a date and time according to locale-specific format strings.
clockRetrieves the amount of processor time used by the program since its start.
CLOCKS_PER_SECRepresents the number of clock ticks per second.

What's Next?

In the next section, we will explore the &lt;vector&gt; library, which provides a dynamic array that can grow or shrink in size as needed. This will be particularly useful for managing collections of data efficiently.

Stay tuned for more C++ tutorials on codingstuff.io!


Previous<cstring> ReferenceNext <vector> Reference

Recommended Gear

<cstring> Reference<vector> Reference