-
November 17th, 2004, 04:19 PM
#1
Member
Getting Windows system time in C++
As the title says, how do you get the system time in C++ on Windows? And putting it into a variable would be the same as any other typecasting thing, right?
Tell me if you think I\'m spamming or doing something stupid, please.
-
November 17th, 2004, 06:53 PM
#2
GetSystemTime
or
GetLocalTime
these are both API's which can be used to get the time. they are different so i suggest you take a read through those links to determine which one suits your purpose
-
November 18th, 2004, 04:32 AM
#3
You could always do a system call. And get the output from that.
-
November 18th, 2004, 04:39 AM
#4
-
November 18th, 2004, 04:59 AM
#5
I wasn't asking how to, I was saying that she/he could.
-
November 18th, 2004, 07:29 AM
#6
that's true, but it will be a lot harder to calculate with and the API's are a lot more powerful.
this doesn't work under windows, since it WILL display the time, but at the same time asks you for the new time
-
November 18th, 2004, 09:06 AM
#7
Hi
I am wondering about that question. Doing a google using "Windows system time in C++"
provides you with the links you need - fully documented. One interesting ingredient here is
the remark about using the API's by White Scorpion.
system-call
You can read the system time without being prompted for a new time using
system("time /T"); but on depending what you want to do with the time, this is
a bad option.
high resolution
If you wish to do high-resolution time measurements there is a possiblity
to use the system clock. Around 10 years ago, this provided you with
a resolution of 1/4096 secs per tic (or so...). Nowadays I don't know. Somewhere at
home, I even might have a code of mine using this ( in Pascal/ASM, as far as I remember).
time.h, timeb.h
Here is a an excerpt that is slightly modified by myself of Times.C by the MSDN Library
(publicly available [1])
Code:
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
// MSDN Library TIMES.C
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}
I hope this answers your question fully and you can use 1-1 the examples.
Cheers!
[1] http://msdn.microsoft.com/library/de..._.wcsftime.asp
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)
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
|
|