Hi, I needed to extract current machine date in my application. Can "_strdate" be used in Linux? Also, is there any Time & Date library like MFC's CTime (but a free one)?
Thanks in advance.
Hi, I needed to extract current machine date in my application. Can "_strdate" be used in Linux? Also, is there any Time & Date library like MFC's CTime (but a free one)?
Thanks in advance.
What's wrong with the standard?Code:#include <ctime> // http://www.cplusplus.com/ref/ctime/
Last edited by SlyMaelstrom; 08-15-2006 at 03:11 AM.
Sent from my iPadŽ
Well, I want a simpler approach of date & time. You know, like Visual Basic's time variabel. AFAIK, the ctime's output is in second. The reason is, I wanted to, for example, substract or add dates like this:Originally Posted by SlyMaelstrom
without making my own code from ctime. AFAIK, it can be easily done in VB.Code:date date1, date2,date3; date3=date2-date1;
It's really not all that much work. Anyway, you're not going to find a more standard approach than this, so I'd suggest you use it.Code:/* from http://www.cplusplus.com/ref/ctime/asctime.html */ /* asctime example */ #include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Current date and time are: %s", asctime (timeinfo) ); return 0; } // Output: // Current date and time are: Sat May 20 15:21:51 2000
Sent from my iPadŽ
He prob wants it in C++, you see. I don't think he actually did, Sly ... sorry.
Last edited by twomers; 08-15-2006 at 03:41 AM.
Thanks. Seems like I skipped reading the link. Although it's not what I have in mind, but I guess that will do for now. Anyway, what I'm trying to do is making a similar type like VB's date. In VB, I can just insert, eg.:Originally Posted by SlyMaelstrom
Code:dim date1,date2 as date date1 = '01/01/1990' date2 = '01/01/1991' date1=date2-date1
Do the above, ok, but get two times a few seconds apart (use a Sleep function or something), then subtact the t_time's from eachother, and it'll tell you the time difference.
Then modify that so that you can set the date, and it will create the 'rawtime' varible for you.
Is it really that difficult to convert the output to C++? As I said, that was taken from the website...
...
... fine...Happy now? Geez...Code:#include <iostream> #include <ctime> int main () { std::time_t rawtime; std::tm *timeinfo; std::time( &rawtime ); timeinfo = std::localtime( &rawtime ); std::cout << "Current date and time are: " << asctime(timeinfo); return 0; } // Output: // Current date and time are: Tue Aug 15 05:35:44 2006
[EDIT]Oh good... all seems well. Anyway, do as twomers says for getting the difference. If you really want a date type like VB's, it's not too hard to implement yourself.
Last edited by SlyMaelstrom; 08-15-2006 at 03:39 AM.
Sent from my iPadŽ
Well what I wanted is inputting string for a date. Like: '01/01/1990' and it will be converted to a time_t variable. And after looking through the website again I found the "mktime" function. So, maybe I'll parse the string with sscanf and inputted them to a tm struct. Then the tm struct wil be converted by mktime. One more question though, if I only inserted the tm_mday, tm_month, and tm_year, can it still be converted to time_t?Originally Posted by SlyMaelstrom
Yes, you can do anything you like.
And by the way, don't skip links. They are normally put there to help people who have questions.
Here's a really simple timing class I made before, if you're interested.
edit - hint for modifying it. The above tells the number of seconds in an interval. How many seconds in a minute? How many minutes in an hour? How many hours in a day? How many days in a week? How many weeks in a month? How many months in a year? etcCode:class TimeIt { private: time_t Start; time_t End; public: TimeIt ( void ); void Clear ( void ); void StartTimer ( void ); void EndTimer ( void ); double GetTime ( void ); }; TimeIt::TimeIt( void ) { Clear(); } void TimeIt::Clear ( void ) { Start = 0; End = 0; } void TimeIt::StartTimer ( void ) { Start = clock(); } void TimeIt::EndTimer ( void ) { End = clock(); } double TimeIt::GetTime( void ) { return (double)( End-Start )/1000; }
Last edited by twomers; 08-15-2006 at 03:51 AM.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law