Getting System Time with C/C++

This question came about on the board recently and I thought it might be a good little snippet to give people an idea of how to access the time. You could use this method to pull out the hours, minutes, seconds, months, years, days etc all in ready to use integer format so we can use it with math functionality. Now the example will be using localtime to get the local time of the system, but you could use gmtime() to get the greenwhich time if you are working with something that may be of international use. We show you this little snippet and talk about it some more right here on the Programming Underground!

So how do we fetch the time in C or C++? Well, first it is best to let everyone know that there are many ways to manipulate the time. Since the time is a very common thing in our world, it stands to reason that C/C++ and their flexibility will have multiple ways of fetching it, manipulating it, and displaying it. So there is certainly no sure fire way of getting it for all applications. The solution below is just a little snippet I put together to offer you one very easy method which can get quick access to the parts of time that we are hunting for.

To use these functions we import the standard time.h header file. This file, as you can imagine, provides many of the functions used for getting and manipulating time. Some of the common functions include time(), localtime(), gmtime() and more. It also provides structures like the struct “tm” which holds member variables which present the parts of a time like minutes, seconds, hours, days etc. This little structure is the key to our little program. The one catch is that it takes a variable of type “time_t” to get the information into the struct. It is also the type that the function time() itself returns. Merging the idea of time_t and placing it in a struct of type “tm” and we have a recipe for getting the parts of time we need for calculations.

This is how it works…

#include <stdio.h>
#include <time.h>

int main ()
  // Create a raw time_t variable and a tm structure
  time_t ourRawtime;
  struct tm * timeInfo;

  // Get the current time and place it in time_t
  time (&ourRawtime);

  // Get the locatime from the time_t and put it into our structure timeinfo
  timeInfo = localtime(ourRawtime);

  // Now we have access to hours, minutes, seconds etc as member variables of all type int
  int hour = timeInfo->tm_hour;
  int min = timeInfo->tm_min;

  // Just print out the hours and minutes to show you
  printf("Hour is: %d and minutes are: %d\n", hour, min);
  return 0;

In the code above we setup two variables. One is a variable of type “time_t”, which we call “ourRawTime”, and will represent the raw time format of the current system. The other variable is a pointer to a struct of type “tm”, which we call timeInfo, and takes the raw time (with the help of the locatltime function) and allows us access to the different pieces of the time. localtime() takes a parameter of time_t and returns a pointer to a tm structure and from there we can use the pointer to access the tm structure’s member variables.

In our example we give the address of our time_t variable to the time() function, which fills it with the current time in a raw format, and then give that raw formatted time_t variable to our localtime function which puts it into a tm structure for us. Once we have it in this structure, we can access the member variables of the structure and pull out the hour and minutes. We then simply display the hours and minutes to the screen for the user to see.

Not too hard really and all the member variables of the tm structure are of type integer so we can use them in calculations with no problems and get access to the various pieces of data as already converted numbers.

Play around with the code a little and let me know what you think. I designed this snippet to be easily modified and easily pluggable into your own projects. Perhaps you can even throw it into a function which takes the time_t as a parameter and returns the tm structure. It is up to you. I hope you enjoy it and thanks again for reading. 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 18 years. He works for a hot application development company in Vancouver Canada which service some of the biggest telecoms in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.