Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the seconds 2

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
hallo,

I would like to store the actual seconds into a an integer variable. I mean if now is 15:14:32, my integer variable should contain 32.

How can I do it?

thanks
 
Hi

Code:
#include <[url=http://cplusplus.com/reference/clibrary/ctime/]time.h[/url]>
#include <stdio.h>

[b]int[/b] main([b]void[/b])
{
  [url=http://cplusplus.com/reference/clibrary/ctime/time_t/]time_t[/url] now=[url=http://cplusplus.com/reference/clibrary/ctime/time/]time[/url](NULL);
  [b]int[/b] sec=now%60;
  printf([i]"Current seconds : %d\n"[/i],sec);
}
Code:
#include <[url=http://cplusplus.com/reference/clibrary/ctime/]time.h[/url]>
#include <stdio.h>

[b]int[/b] main([b]void[/b])
{
  [url=http://cplusplus.com/reference/clibrary/ctime/time_t/]time_t[/url] now=[url=http://cplusplus.com/reference/clibrary/ctime/time/]time[/url](NULL);
  struct [url=http://cplusplus.com/reference/clibrary/ctime/tm/]tm[/url] *nows=[url=http://cplusplus.com/reference/clibrary/ctime/localtime/]localtime[/url](&now);
  [b]int[/b] sec=nows->tm_sec;
  printf([i]"Current seconds : %d\n"[/i],sec);
}

Feherke.
 
It's not a standard-compliant solution. The C Standard never defines that time() returns a value in seconds.
That's (one of) a correct solution:
Code:
    time_t t = time(0);
    int seconds = gmtime(&t)->tm_sec;
 
Some correction to my post above: only the 1st feherke's solution was incorrect. Sorry ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top