How To Use C++ to Convert a UNIX Timestamp to Date and Time

Published Categorized as C/C++, Command Line, Programming
How To Make A UNIX Timestamp Human Readable Using C++

Looking for a quick and easy tutorial that explains how to convert a UNIX timestamp to a human readable date and time using some easy C++ code?

How to convert Unix Timestamp to Da...

In this tutorial we will cover some easy methods for converting UNIX timestamp to human readable datetime with free sample code included.

Did you know that UNIX has been counting seconds since the 1st of January 1970?!

Table of Contents

  1. Prerequisite
  2. What Exactly is a UNIX Timestamp and Epoch Time?
  3. What Is The Current UNIX Epoch Time?
  4. How To Convert UNIX Epoch Time to a Human Readable Datetime Using C++
  5. How Do You Convert a UNIX timestamp in Milliseconds to a Human Readable Date?

Prerequisite

This tutorial will assume that you already have the ability to compile C++ code.

If you have not yet compiled any C++ code, you can check out my tutorials for Linux or Mac OS to get you up and running. For Windows users I would recommend getting setup with WSL first and then following the Linux tutorial.

To demonstrate the conversion we only need to execute the code within a terminal window. You can either use your prefered IDE or write the code directly in a text editor and compile it with G++.

What Exactly is a UNIX Timestamp and Epoch Time?

An epoch is a date and time that a computer uses to calculate system time.

The majority of computers calculate time as a number that represents the seconds subtracted from a given arbitrary date and time.

For instance, the Unix epoch, or the time since Thursday, January 1, 1970, 00:00:00 UTC, is used by Unix and POSIX to calculate the passage of time.

It is easier to store the time in computer memory as a single number of seconds counted since a fixed date such as the Unix epoch.

However this number makes no sense to most humans and must be converted into a readable date and time.

There are several methods that we can use to achieve this. In this article we will look at 3 quick methods that are commonly useful.

What Is The Current UNIX Timestamp?

Just as there is always a unique date and time at any given moment, there is also a unique UNIX timestamp at any given moment.

As previously mentioned the UNIX timestamp is the number of seconds that has elapsed since Thursday, January 1, 1970, 00:00:00 UTC.

There are several useful online tools that can tell us the current UNIX timestamp at any given moment.

For example at this exact time of writing, the UNIX timestamp is 1665566381.

This converts to Wed Oct 12 2022 09:19:41 GMT+0000.

To see the current UNIX timestamp, simply head over to unixtimestamp.com.

How To Convert UNIX Epoch Time to a Human Readable Datetime Using C++

The following code is an example of a simple converter that can accept a UNIX timestamp and output a human readable date.

#include <time.h>
#include <iostream>

int main() {
	
	time_t datetime;
  
	std::cout << "Enter datetime value: ";
	std::cin >> datetime;
	
    std::cout << "The time is " << ctime(&datetime);
  
}

So, how does this code work?

On the first line we include a preprocessor directive that includes time.h, a header file that defines variables and functions for manipulating time.

We will also include iostream so that we can read and write from the console.

#include <time.h>
#include <iostream>

Next we create the datetime variable of the type time_t, which contains an integer value that represents the number of seconds since the UNIX epoch.

time_t datetime;

The next line of code prints a prompt in the console requesting that the user enters a UNIX timestamp.

std::cout << "Enter datetime value: ";

The user inputted data is then stored in the datetime variable.

std::cin >> datetime;

The final line prints the UNIX timestamp as a human readable date and time using the ctime function to convert the UNIX timestamp.

std::cout << "The time is " << ctime(&datetime);

How Do You Convert a UNIX timestamp in Milliseconds to a Human Readable Date?

The code above converts a seconds UNIX timestamp into a human readable date, but what if the timestamp also has milliseconds?

If the timestamp that you are trying to convert has an additional three numbers on the end to represent milliseconds, you can make a minor amendment to the code above:

#include <time.h>
#include <iostream>

int main() {
	
	time_t datetime_ms;
  
	std::cout << "Enter datetime value: ";
	std::cin >> datetime_ms;
  
  	time_t datetime = datetime_ms / 1000;
	
    std::cout << "The time is " << ctime(&datetime);
  
}

We simply create an additional variable datetime_ms to store the UNIX timestamp with milliseconds.

	std::cout << "Enter datetime value: ";
	std::cin >> datetime_ms;
  
  	time_t datetime = datetime_ms / 1000;

We then divide datetime_ms by 1000 to remove the milliseconds and store the result in our normal datetime variable. This will be then displayed as a valid human readable date.

Conclusion

Referring to the time in only seconds would certainly be confusing for us humans!

However it makes perfect sense for a computer to store the time as a single positive integer value and seconds or milliseconds are the ideal choice of unit.

Thankfully as we have seen in this tutorial, it is pretty easy to convert a computer timestamp into a human readable date with a simple piece of C++ code.

Thanks for visiting and whilst you are here, why not check out some more C++ tutorials!

Leave a comment

Your email address will not be published. Required fields are marked *