Hi, i've started to use time.h and i'm needing help with something.. basically, i want to be able to return the specified number of seconds in a week/day/hour/minute/second format very similar to that of mIRC's $duration().

I've done it before but I can't remember what I did.. i know how to get the ctime C++

time_t seconds;
  seconds = time (NULL);
  printf ("%d seconds since January 1, 1970\n", seconds);

It would be the same thing as doing this in mIRC

//echo -a $ctime

So, I know how to get the ctime and calculate it down to how many seconds i have.. but i don't know how to convert it to a week/day/hour/minute/second format.. like this in mIRC

//echo -a $duration($calc($ctime - 1139796581))

which returns 1hr 46mins 24secs for me.

So I have the number of seconds from ctime 6384 - but i need to convert those seconds to 1hr 46mins 24secs.

Any help would be appreciated, i've been trying to figure it out all day with different combinations of asctime, time, localtime, etc.. and i can't figure it out.

#include <iostream>
#include <ctime>

int main()
	time_t time_now;
	struct tm *tm_now = localtime(&time_now);
	std::cout << tm_now->tm_hour << " " 
		<< tm_now->tm_isdst << " " 
		<< tm_now->tm_mday << " " 
		<< tm_now->tm_min << " " 
		<< tm_now->tm_mon << " " 
		<<tm_now->tm_sec << " " 
		<< tm_now->tm_wday << " " 
		<< tm_now->tm_yday << " "
		<< tm_now->tm_year + 1900 
		<< std::endl;
	return 0;

Thanks for that piece of code WolfPack, now i need to figure out how i should calculate it..

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.