Hi all,

#include<ctime>
#include<string>
 
int main()
{
         string snapshot_time = "10:00:00"
         int interval = 60;
         string end_time = convert_hour(snapshot_time,interval);
         cout << end_time << endl;
}
 
string convert_hour(string &snapshot_time, int interval)
{
char current_time[BUFSIZ] = { '\0' } ;
struct tm tm;
strptime(snapshot_time.c_str(), "%H:%M:%S", &tm);
tm.tm_sec = tm.tm_sec + interval;
time_t t = mktime(&tm);
strftime( current_time, BUFSIZ, "%H:%M:%S", &tm) ;
return current_time;
}

I expect the output of my program to be 10:01:00 but it displays as 10:00:60.

Where am I going wrong? Thanks a lot in advance.

Do you wanna convert a given epoch time to a string?

what compiler are you using? I tried to compile it with three diffeent compilers and none of them knew about strptime().

<email snipped>
all,
hi dani
i am sanjay pleseas send the problam in c lang. my email.sit
<email snipped>

#include<ctime>
#include<string>
 
int main()
{
         string snapshot_time = "10:00:00"
         int interval = 60;
         string end_time = convert_hour(snapshot_time,interval);
         cout << end_time << endl;
}
 
string convert_hour(string &snapshot_time, int interval)
{
char current_time[BUFSIZ] = { '\0' } ;
struct tm tm;
strptime(snapshot_time.c_str(), "%H:%M:%S", &tm);
tm.tm_sec = tm.tm_sec + interval;
time_t t = mktime(&tm);
strftime( current_time, BUFSIZ, "%H:%M:%S", &tm) ;
return current_time;
}

I expect the output of my program to be 10:01:00 but it displays as 10:00:60.

Where am I going wrong? Thanks a lot in advance.

sanjay: what is the purpose of your post? did you forget to post some comments ?

hi dani
i am sanjay pleseas send the problam in c lang. my email.sit

The program is already C language. :rolleyes:

Given the use of the STL string class and the iostream objects I'd say the program is using the C++ language and the C language time header file since templates and iostream objects cannot be used in C.

I'm not sure the STL string header includes the iostream header, though it may.

I don't think it is a wise idea to give a variable the same name as a struct/class name.

The C/C++ languages leave a lot of the work, like bounds checking, garbage collection, etc, to the programmer. Given the input of the program, the output of the program, and the behaviour of difftime, I suspect that this is one of those times, too. That is, it may well be up to the programmer to convert seconds to minutes, minutes to hours, etc, if appropriate before passing the information to strftime() to process into a differrent format. A quick review of information at cppreference and Cplusplus and on Google didn't really state what the behavior of the time functions was in this regard.

But then again, maybe I am blowing smoke in the wind.

But then again, maybe I am blowing smoke in the wind.

Yeah, it's somewhat smoky in here... :D

what compiler are you using? I tried to compile it with three diffeent compilers and none of them knew about strptime().

It seems to be the reverse of strftime() in *nix strftime() does not normalize the data in the buffer. You have 60, it prints 60.

Since you put time_t t = mktime(&tm); in the code and never use it, I assume you wanted to reconvert the calendar time into the tm structure again.

This technique didn't work for me, though. mktime() returned an invalid time. Be sure you check it before proceeding.

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.