|
-
August 8th, 2007, 08:24 PM
#1
Hi getting system time in C++
Hi all,
How do i get the system time in C++?
thnks!
-
August 8th, 2007, 08:32 PM
#2
Re: Hi getting system time in C++
Code:
# define TIME_SIZE 40
const struct tm *tm;
size_t len;
time_t now;
char *s;
now = time ( NULL );
tm = localtime ( &now );
s = new char[TIME_SIZE];
len = strftime ( s, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
-
August 8th, 2007, 08:38 PM
#3
Re: Hi getting system time in C++
You can make use of strftime.
Code:
#include <iostream>
#include <ctime>
#include <cerrno>
int main()
{
//Find the current time
time_t curtime = time(0);
//convert it to tm
tm now=*localtime(&curtime);
//BUFSIZ is standard macro that expands to a integer constant expression
//that is greater then or equal to 256. It is the size of the stream buffer
//used by setbuf()
char dest[BUFSIZ]={0};
//Format string determines the conversion specification's behaviour
const char format[]="%A, %B %d %Y. The time is %X";
//strftime - converts date and time to a string
if (strftime(dest, sizeof(dest)-1, format, &now)>0)
std::cout<<dest<<std::endl;
else
std::cerr<<"strftime failed. Errno code: "<<errno<<std::endl;
}
You can make use of _strtime but it's non-portable and may not work on every system.
Code:
#include <iostream>
#include <ctime>
int main ()
{
char time [10];
_strtime(time);
std::cout<<"Current Time:"<<time;
return 0;
}
Last edited by sunnypalsingh; August 8th, 2007 at 08:40 PM.
Appreciate others by rating good posts
"Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett
-
August 8th, 2007, 11:37 PM
#4
Re: Hi getting system time in C++
On windows Using Win32 API we can make it very simple.
Code:
#include<iostream>
#include<windows.h>
int main(){
SYSTEMTIME st;
GetLocalTime(&st);
std::cout<<st.wHour<<":"<<st.wMinute<<":"<< st.wSecond;
return 0;
}
You can find details for all other related functions here
-
August 9th, 2007, 01:03 AM
#5
Re: Hi getting system time in C++
You can use GetSystemTime. Here is the list of all Windows time functions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|