Hi everyone,

i just want to know about "time.h" or "ctime"... I browsed through many sites, but I couldn't understand anything.... I need some help... from the basics in "time.h" or "ctime"... From declaring a time variable and initialising a date to a time variable, to the end... Please help me...

time() returns the current system date/time in seconds since about 1 Jan 1970 (epoch time)

localtime() will return a tm structure based on the time_t value that was returned by time() or other similar function.

ctime() returns a character array based on the contents of struct tm that is passed to ctime()

Briefly

time_t now = time(0);
struct tm* tm = localtime(&now);
char* s = ctime(tm);
Comments
Short & sweet

very limited knowledge of this but this using ctime appears to give a rough time the code took to run.

double time=clock();
//program
cout<<clock()-time<<" ms"; or cout<<(clock()-time)/1000<<" seconds.";

Edited 5 Years Ago by frogboy77: n/a

very limited knowledge of this but this using ctime appears to give a rough time the code took to run.

If you don't know what a function does then you should look it up. ctime() has nothing to do with how long the code has run.

The code you posted called clock(), not ctime(). Yes, clock() does that, but that's now what you said.

clock() returns clock_t object, which may or may not be double.

Edited 5 Years Ago by Ancient Dragon: n/a

very limited knowledge of this but this using ctime appears to give a rough time the code took to run.

double time=clock();
//program
cout<<clock()-time<<" ms"; or cout<<(clock()-time)/1000<<" seconds.";

This is more conventional:

#include <iostream>
#include <ctime>

int main()
{
    std::clock_t start = std::clock();

    // ...

    std::cout<< ((double)std::clock() - start) / CLOCKS_PER_SEC <<'\n';
}

While the assumption that clock_t will be meaningful when cast to double is not portable, I have yet to see an implementation where this construct fails. However, keep in mind that there's no portable way to achieve more than second granularity with timing in standard C++; any solution would require a non-portable library or assumption about the implementation.

If you're okay with second granularity, the difftime function can be used in a portable manner:

#include <iostream>
#include <ctime>

int main()
{
    std::time_t start = std::time(0);

    // ...

    std::cout<< std::difftime(std::time(0), start) <<'\n';
}

In all honesty, I use the non-portable construct when doing ad hoc timing simply because there's low risk of it failing, and it works well without using non-standard libraries. YMMV.

okay.. I get vaguely all the points you guys said... Now if have to get a date, what should i use???

Why do we give time_t now= time(0);

what happens if i change 0 with 1 or any other number??

The parameter to time() is a pointer to a time_t object, or NULL. Since NULL is often declareed as 0 you can call time() is one of three ways

time_t now;

time(&now);
// or
now = time(NULL);
// or
now = time(0);

Any of the above three are acceptable.

okay.. I get vaguely all the points you guys said... Now if have to get a date, what should i use???

struct tm contains both the date and the time. See my first post of example how to get that structure for current system date and time.

You mean you want to enter a date -- in that case none of the functions in time.h will help with that. Just call getline() and do all date validations youself (there are no functions that will do it for you.)

#include <string>
#include <iostream>
using std::string;
using std::cin;
using std::cout;

int main()
{
   string date;
   cout << "Enter a date\n";
   getline(cin,date);
   // now validate the date is in correct format
}

Edited 5 Years Ago by Ancient Dragon: n/a

>i need to get some date not the current date...
Then you'll probably want to populate a tm object with the relevant values and use mktime to normalize it into something valid. From there you can convert the time_t value back to a tm object for consumption. For example, the following program will print the date one week from now:

#include <iostream>
#include <ctime>

int main()
{
    std::time_t t = std::time(0);
    std::tm *date = std::localtime(&t);

    date->tm_mday += 7;

    t = std::mktime(date);
    date = std::localtime(&t);

    std::cout<< std::asctime(date) <<'\n';
}
This article has been dead for over six months. Start a new discussion instead.