Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,330 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,828 people online right now. Registration is fast and FREE... Join Now!




convert current time into seconds?

 
Reply to this topicStart new topic

convert current time into seconds?, calculating number of seconds from the current time?

chronoTrigger
1 May, 2008 - 09:45 AM
Post #1

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 43


My Contributions
does anyone know how can I get the total seconds based on the current time?

hour:minute into seconds
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Convert Current Time Into Seconds?
1 May, 2008 - 09:51 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
(hour * 3600) + (minutes * 60) = total number of seconds

smile.gif
User is offlineProfile CardPM
+Quote Post

lockdown
RE: Convert Current Time Into Seconds?
1 May, 2008 - 09:54 AM
Post #3

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
Ah beat me to it. Yeah thats what I would recommend.

This post has been edited by lockdown: 1 May, 2008 - 09:54 AM
User is offlineProfile CardPM
+Quote Post

chronoTrigger
RE: Convert Current Time Into Seconds?
1 May, 2008 - 09:55 AM
Post #4

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 43


My Contributions
QUOTE(Martyr2 @ 1 May, 2008 - 10:51 AM) *

(hour * 3600) + (minutes * 60) = total number of seconds

smile.gif


smile.gif thanks for the calculation method, but I meant, the C++ function itself that can access to current time and hopefully can access to both hour and minute so I can calculate into seconds like you have shown above. Sorry my question is kind of vague.

This post has been edited by chronoTrigger: 1 May, 2008 - 09:56 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Convert Current Time Into Seconds?
1 May, 2008 - 10:26 AM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
I knew it couldn't be that simple. Next time work on the vagueness a little. Precise questions deserve precise answers I always say. wink2.gif

cpp


#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;

int main ()
{
// Create a raw time_t variable and a tm structure
time_t rawtime;
struct tm * timeinfo;

// Get the current time and place it in time_t
time ( &rawtime );

// Get the locatime from the time_t and put it into our structure timeinfo
timeinfo = localtime ( &rawtime );

// Now we have access to hours, minutes, seconds etc as member variables of all type int
int hour = timeinfo->tm_hour;
int min = timeinfo->tm_min;

// Just print out the hours and minutes to show you
cout << "Hour is: " << hour << " and minutes are: " << min << endl;

return 0;
}


Just an example for you to play with. Enjoy!

"At DIC we be time mastering code ninjas... with that said, I say it is miller time!" decap.gif
User is offlineProfile CardPM
+Quote Post

chronoTrigger
RE: Convert Current Time Into Seconds?
1 May, 2008 - 10:30 AM
Post #6

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 43


My Contributions
QUOTE(Martyr2 @ 1 May, 2008 - 11:26 AM) *

I knew it couldn't be that simple. Next time work on the vagueness a little. Precise questions deserve precise answers I always say. wink2.gif

cpp


#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;

int main ()
{
// Create a raw time_t variable and a tm structure
time_t rawtime;
struct tm * timeinfo;

// Get the current time and place it in time_t
time ( &rawtime );

// Get the locatime from the time_t and put it into our structure timeinfo
timeinfo = localtime ( &rawtime );

// Now we have access to hours, minutes, seconds etc as member variables of all type int
int hour = timeinfo->tm_hour;
int min = timeinfo->tm_min;

// Just print out the hours and minutes to show you
cout << "Hour is: " << hour << " and minutes are: " << min << endl;

return 0;
}


Just an example for you to play with. Enjoy!

"At DIC we be time mastering code ninjas... with that said, I say it is miller time!" decap.gif


Thanks I will definitely play around with your given example.

User is offlineProfile CardPM
+Quote Post

chronoTrigger
RE: Convert Current Time Into Seconds?
7 May, 2008 - 02:31 PM
Post #7

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 43


My Contributions
I don't know why, but I included the header file like your sample code shown above, my program has error, so I'm using SYSTEMTIME instead, and I believe I can access to hour, minute, second. So far it works.

Thanks Martyr2.



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 07:51AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month