Broad Network


Current Processor Time in C++

Date and Time in C++ Simplified - Part 3

Forward: In the previous part of the series, we dealt with time typed into the computer by the user. In this part of the series, we deal with time read from the computer’s processor.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 3 of my series, Date and Time in C++, Simplified. In the previous part of the series, we dealt with time typed into the computer by the user. In this part, we deal with time read from the computer’s processor. If you set the time in your computer as local time, then the time read is local time. If you set it as GMT time, then the time read is GMT (UTC) time.

The time Function
There is a function in the ctime header called the time function. Its syntax is:

        time_t time(time_t *timer);

If the argument to this function is NULL, then the current time (microprocessor) of the computer is returned as a time_t object. The following code displays the current computer time:

#include <iostream>
#include <ctime>
using namespace std;

int main()
    {
        time_t currTime = time(NULL);
        time_t *currTimePtr = &currTime;
        cout << ctime(currTimePtr);

        return 0;
    }

Read and try the above code. Recall from the previous part of this series, that the ctime function converts a time_t* argument into a string (see last but one statement above).

If you want the equivalent struct tm object from the time_t object returned by the time function, then you can use the gmtime function.

That is what I have prepared for the Current Processor Time in C++. A rather short but important article! We continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message