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...

Recommended Answers

All 16 Replies

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);
commented: Short & sweet +16

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.";

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.

ok. any explanation why it seems to work?

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.

thanks. so do i need to #include <ctime> to use clock()?

of course you do. Your compiler will most likely produce errors or warnings if you don't.

thanks for the reply.
still very new at this so hope my questions don't seem too dumb.

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.

okay... i need to get some date not the current date...

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
}

>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';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.