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!
<It is a small world theme music... but after all the little dolls play GTA 4>
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...
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. :)
If you want more blog entries like this, check out the official blog over on The Coders Lexicon. There you will find more code, more guides and more resources for programmers of all skill levels!
<It is a small world theme music... but after all the little dolls play GTA 4>
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. :)
If you want more blog entries like this, check out the official blog over on The Coders Lexicon. There you will find more code, more guides and more resources for programmers of all skill levels!
2 Comments On This Entry
Page 1 of 1
mikeblas
03 May 2008 - 06:18 PM
This code gets the local time, not the system time. They may be different, or might be the same.
In Windows, the system time is reachable with the GetSystemTime() API. The time returned by time() from the C Runtime libraries in Windows might be different because the user can configure a separate time zone for the C Runtime. The application might also set a different locale, influencing the timezone, or by setting the time zone directly with a call to tzset()
In Windows, the system time is reachable with the GetSystemTime() API. The time returned by time() from the C Runtime libraries in Windows might be different because the user can configure a separate time zone for the C Runtime. The application might also set a different locale, influencing the timezone, or by setting the time zone directly with a call to tzset()
Page 1 of 1
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
- announcement
- APIs
- Basics
- Best Practices
- BLOB
- Book Reviews
- Bots
- C#
- CSS
- Deep Underground
- Deep Underground (misc)
- design
- desktop programming
- Drawing
- Forms
- Games
- General Discussion
- Hack the Planet!! (Just for fun category)
- HTML
- HTML forms
- images
- Interviews
- Java
- JavaScript
- jquery
- menus
- PHP
- Python
- Ruby
- Search theory
- Security
- Theory
- Tips and Tricks
- VB.NET
- Web API
- web development
My Blog Links
Recent Entries
Recent Comments
Search My Blog
12 user(s) viewing
12 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



2 Comments









|