Difference between current system time and time from user input in c
-
How to find different between current system time and user given time in c (only C not in c++) program. Is there any method that I get only time (without date) as output (not as char as time_t type).
-
How to find different between current system time and user given time in c (only C not in c++) program. Is there any method that I get only time (without date) as output (not as char as time_t type).
You could calculate the number of seconds from midnight for current time and user given time. Subtract the two and convert back to hours, minutes seconds:
time_t now;
time (&now); //missed this
struct tm *systime = localtime(&now);
struct tm user_time = {56, 34, 12}; // user = 12:34:56
int sys_sec = systime->tm_hour*3600 + systime->tm_min*60 + stystime->tm_sec;
int user_sec = user_time.tm_hour*3600 + user_time.tm_min*60 + user_time.tm_sec;
int diff = sys_sec - user_sec;Of course you have to figure out what to do when time wraps around at midnight but that's your problem :). I'm just showing how you can shoot yourself in the foot
Mircea
-
You could calculate the number of seconds from midnight for current time and user given time. Subtract the two and convert back to hours, minutes seconds:
time_t now;
time (&now); //missed this
struct tm *systime = localtime(&now);
struct tm user_time = {56, 34, 12}; // user = 12:34:56
int sys_sec = systime->tm_hour*3600 + systime->tm_min*60 + stystime->tm_sec;
int user_sec = user_time.tm_hour*3600 + user_time.tm_min*60 + user_time.tm_sec;
int diff = sys_sec - user_sec;Of course you have to figure out what to do when time wraps around at midnight but that's your problem :). I'm just showing how you can shoot yourself in the foot
Mircea
Mircea Neacsu wrote:
struct tm *systime = localtime(&now);
I've never understood declaring a variable as
struct*
.struct
is a keyword used to define a UDF. It's not a type in itself.The difficult we do right away... ...the impossible takes slightly longer.
-
Mircea Neacsu wrote:
struct tm *systime = localtime(&now);
I've never understood declaring a variable as
struct*
.struct
is a keyword used to define a UDF. It's not a type in itself.The difficult we do right away... ...the impossible takes slightly longer.
tm
is a regular structure and localtime (as well as gmtime) return a pointer to an internal buffer. Quoting from cplusplus.com:[^]Quote:
A pointer to a tm structure with its members filled with the values that correspond to the local time representation of timer. The returned value points to an internal object whose validity or value may be altered by any subsequent call to gmtime or localtime.
Now that I look again at the code I see that I missed the call to
time(&now)
. Oops!Mircea
-
Mircea Neacsu wrote:
struct tm *systime = localtime(&now);
I've never understood declaring a variable as
struct*
.struct
is a keyword used to define a UDF. It's not a type in itself.The difficult we do right away... ...the impossible takes slightly longer.
-
It is simply a pointer to a structure that gets filled in by the system call. What is wrong with that?
It bothers me because it would be akin to saying:
class *Foo = new Foo();
Is that legal C++? EDIT: Oh wait, I didn't notice that there is a struct name followed by a variable name. I see now.
The difficult we do right away... ...the impossible takes slightly longer.
-
How to find different between current system time and user given time in c (only C not in c++) program. Is there any method that I get only time (without date) as output (not as char as time_t type).
You can use the [difftime()](http://www.cplusplus.com/reference/ctime/difftime/) function to subtract two time_t values. If you have the time in a struct tm, you can convert it to time_t using the [mktime()](http://www.cplusplus.com/reference/ctime/mktime/) function. The [strftime()](http://www.cplusplus.com/reference/ctime/strftime/) function formats a struct tm to text format. These APIs are all available in C90, but some options were introduced as of C99. difftime() should incorporate leap-seconds for past dates, but I don't know if actual implementations do so. Note that issues such as daylight-saving time, time zones, etc. are left to the programmer. Ignoring these is a common way for programmers to shoot themselves in the foot. :)
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.
-
It bothers me because it would be akin to saying:
class *Foo = new Foo();
Is that legal C++? EDIT: Oh wait, I didn't notice that there is a struct name followed by a variable name. I see now.
The difficult we do right away... ...the impossible takes slightly longer.
-
Richard Andrew x64 wrote:
Is that legal C++?
Yes, and necessary. Pointers must actually point to something, or they are just empty space.
You misunderstood what I was demonstrating. I was demonstrating declaring a variable of type "class*". Is it possible to declare a variable of type "class"? Is there such a type? I don't think that's legal in C++. I haven't tried it though.
The difficult we do right away... ...the impossible takes slightly longer.
-
You misunderstood what I was demonstrating. I was demonstrating declaring a variable of type "class*". Is it possible to declare a variable of type "class"? Is there such a type? I don't think that's legal in C++. I haven't tried it though.
The difficult we do right away... ...the impossible takes slightly longer.
Richard Andrew x64 wrote:
Is it possible to declare a variable of type "class"?
No, because
class
is an abstract concept, not a type in the same sense thatint
is. The actual type will be the name used in the class definition. For example consider the following:class Foo
{
public:
Foo() { std::cout << "Constructing a Foo object" << std::endl; }
};int main(int argc, char** argv)
{
Foo aFoo;
Foo* pFoo = new Foo();
}A new object
aFoo
is created and stored on the local stack. A second new object is instantiated on the heap and its address passed back to be stored in the pointerpFoo
. The type of both objects isFoo
notclass
. Does that make sense?