31 Jul, 2009, Bojack wrote in the 1st comment:
Votes: 0
Trying to figure out the correct line to print out system time in hours, mins and seconds.. ctime uses date too which i dont want. What im trying to do is take that time and add a week onto it then save it to a file for a code im writing.
31 Jul, 2009, Kline wrote in the 2nd comment:
Votes: 0
Maybe this will help you a little.

time_t cur_time, xmas_time;
struct tm tm_xmas, *tm_local;
int days;
char buffer[255];

time(&cur_time);
tm_local = localtime(&cur_time);

tm_xmas.tm_sec = 0;
tm_xmas.tm_min = 0;
tm_xmas.tm_hour = 0;
tm_xmas.tm_mday = 25;
tm_xmas.tm_mon = 11;
tm_xmas.tm_year = tm_local->tm_year;

if( tm_local->tm_mon >= 11 && tm_local->tm_mday >= 25 ) /* Christmas has passed this year, go to next year! */
tm_xmas.tm_year++;

xmas_time = mktime( &tm_xmas );
days = static_cast<int>(difftime( xmas_time, time( &cur_time ) ) / ( 3600 * 24 ));

if( days > 0 )
snprintf( buffer, 255, "$n utters the words '%i day%s to Christmas!'.", days, days == 1 ? "" : "s" );
else
snprintf( buffer, 255, "$n utters the words 'Christmas is here! Rejoice!'." );

act( buffer, ch, NULL, NULL, TO_ROOM );


This is also a very good website in regards to time (or any other C/C++ functions): http://www.cplusplus.com/reference/clibr...
31 Jul, 2009, Bojack wrote in the 3rd comment:
Votes: 0
Thanks for your post, your link helped me out.
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer, 80, "%X", timeinfo);
sendch (buffer, ch);

Which gave me this :
01:38:04 You have summoned Shenlong the dragon!

Now I can finally finish this.
02 Aug, 2009, Bojack wrote in the 4th comment:
Votes: 0
Ok im spent.. maybe someone else can help me with this.. taking the current time.. adding a week onto it then subtracting that and then show the remaining time left.
This is what I got.. the first part works fine.. its the second part.. ive tried using different methods and neither worked right.
void dragon_time (CHAR_DATA *ch)
{
struct tm * timeinfo;
time_t timer;
char buffer[80];

// Set each timestamp set in Central Time
time (&current_time);
timeinfo = localtime (&current_time);
strftime (buffer, 80, "Day %d at %I:%M:%S\n\r", timeinfo);
sendch (buffer, ch);
//Add 7 days to the current time, convert to number of seconds from the EPOCH
//then add 7 days times 24 hours times 60 minutes times 60 seconds to get the
//number of seconds in 7 days.
timer = mktime (timeinfo) + (7 * 24 * 60 * 60);
timeinfo = localtime (&timer);
strftime (buffer, 80, "Day %d at %I:%M:%S\n\r", timeinfo);
sendch (buffer, ch);

return;
}

void dragon_remaining (CHAR_DATA *ch)
{
// time_t timer, countdown;
// int newtime;
/*
struct tm *newtime;
struct tm *newcount;
struct tm *final;
char buffer[80];

time (&current_time);
newtime = localtime(&current_time);

newcount = newtime + (7 * 24 * 60 * 60);
timer = mktime(newcount);
//time (&countdown);
// countdown = difftime(timer, time (&current_time));
countdown = difftime (time(&current_time), timer );
time (&countdown);
final = localtime(&countdown);
strftime (buffer, 80, "Time Left: %d days, %IHrs:%MMins:%SSecs\n\r", final);
sendch(buffer, ch);
*/
struct timeval newtime, faketime;
char buffer[80];
char buf[MSL];
time_t current, settime, final;

gettimeofday (&newtime, NULL);
current = (time_t) newtime.tv_sec;

gettimeofday (&faketime, NULL);
settime = (time_t) faketime.tv_sec;

final = difftime (current, settime);

strcpy (buffer, ctime (&final));
sprintf (buf, "%s", buffer);
sendch (buf, ch);
}


This is the output:
Day 02 at 03:38:58
Day 09 at 03:38:58
Wed Dec 24 18:00:00 1969
If I use the commented out method I just get Day 02 at 03:38:58, at one point reversing things I got Day 13 at 09:blah blah.. does anyone know the correct way to do this?
02 Aug, 2009, David Haley wrote in the 5th comment:
Votes: 0
What exactly are you trying to do?

"taking the current time.. adding a week onto it then subtracting that and then show the remaining time left. "

It sounds like you're doing…

cur time + 1 week - 1 week

and then you want to see what's left… so I'm guessing that the above sentence needs some elaboration.

When showing code, it would probably be helpful to not show code you've commented out… it would make it easier to see what you're actually using. Anyhow, I'm not sure what the code you're pasting is supposed to be doing, either.
02 Aug, 2009, kiasyn wrote in the 6th comment:
Votes: 0
I think he wants to take the current time, add a week, and then display a countdown to that time.
02 Aug, 2009, Kline wrote in the 7th comment:
Votes: 0
Look at the code I initially posted: you can break a tm struct down into pieces fairly easily. tm_struct.tm_mday += 7. Then just call difftime().
02 Aug, 2009, Bojack wrote in the 8th comment:
Votes: 0
Correct kiasyn, take aug 2 10:30:05 add 7 days then give the remaining time left to that date everytime its called. Right now i just gotta make it work then ill actually set it in a file later.
02 Aug, 2009, Bojack wrote in the 9th comment:
Votes: 0
Kline the problem with the code you posted is you are creating a new structure which is a specific day, mine is always changing by what time it is now. So you have to add a week onto what time it is now, that code doesnt work like that.
02 Aug, 2009, Kline wrote in the 10th comment:
Votes: 0
You can't fill it with localtime first and then increment it?
02 Aug, 2009, Bojack wrote in the 11th comment:
Votes: 0
No cuz you'll get incompatible types in assignment error.
void dragon_remaining (CHAR_DATA *ch)
{
time_t current, newtime, countdown;
struct tm local;
char buffer[80], buf[MSL];

time (&current);
local = localtime(&current);

local.tm_sec += 60;
local.tm_min += 60;
local.tm_hour += 24;
local.tm_mday += 7;

newtime = mktime(&local); <— errors here

countdown = difftime(newtime, time(&current_time));
strcpy (buffer, ctime (&countdown));
sprintf (buf, "%s", buffer);
sendch (buf, ch);
}

void dragon_remaining (CHAR_DATA *ch)
{
time_t current, countdown, ntime;
struct tm new, newtime;
struct tm *local;
char buffer[80], buf[MSL];

time (&current);
local = localtime(&current);

new.tm_sec += 60;
new.tm_min += 60;
new.tm_hour += 24;
new.tm_mday += 7;
ntime = mktime(&new);

newtime = mktime(local + new);

countdown = difftime(newtime, time(&current_time));


If you try to use the + symbol in mktime for it, you'll get invalid operands to binary +. Thats my problem
03 Aug, 2009, kiasyn wrote in the 12th comment:
Votes: 0
start_time = time(0) + (86400 * 7); // now + a week

// time passes…

// …

now_time = time(0);

diff = start_time-now_time; // seconds left;

// logic to display time left here, ie
// below logic is completely wrong, but gives the idea
minutes = diff/60;
hours = diff/60;
days = diff/86400;
04 Aug, 2009, Bojack wrote in the 13th comment:
Votes: 0
I just realized what I was doing wrong.. strftime spits out what time it is, not a countdown format.. what im trying to go for is countdown numbers like what clock() uses.
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}

int main ()
{
int n;
printf ("Starting countdown…\n");
for (n=10; n>0; n–)
{
printf ("%d\n",n);
wait (1);
}
printf ("FIRE!!!\n");
return 0;
}
Output:


Starting countdown…
10
9
8
7
6
5
4
3
2
1
04 Aug, 2009, Bojack wrote in the 14th comment:
Votes: 0
Also here's part of the code.. I got to do each if statement now broken down by seconds.
struct tm * timeinfo;
time_t timer, countdown;
char buffer[80];
int down, current, timeleft;

// Set each timestamp set in Central Time
time (&current_time);
current = time(&current_time);
countdown = 604800 + time(&current_time);

down = countdown;
timeleft = (difftime(down, current) / (3600 * 24));

sprintf (buffer, "%d", timeleft);
sendch (buffer, ch);
0.0/14