Anyone knows how to get the current time into a variable?
Printable View
Anyone knows how to get the current time into a variable?
[code]
#include <time.h>
#include <stdio.h>
#include <string.h>
void main(void){
char tmp[80];
struct tm *ptr;
time_t lt;
lt = time(NULL);
ptr = localtime(<);
printf("%s\n",asctime(ptr) );
strcpy(tmp,asctime(ptr) );
}
tmp now has has something like:
Wed Oct 11 12:20:20 2001
If you are making a Win app, you can also use the WIn32 API
Quote:
GetLocalTime
The GetLocalTime function retrieves the current local date and time.
VOID GetLocalTime(
LPSYSTEMTIME lpSystemTime // address of system time structure
);
Quote:
SYSTEMTIME
The SYSTEMTIME structure represents a date and time using individual members for the month, day, year, weekday, hour, minute, second, and millisecond.
typedef struct _SYSTEMTIME { // st
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
Members
wYear
Specifies the current year.
wMonth
Specifies the current month; January = 1, February = 2, and so on.
wDayOfWeek
Specifies the current day of the week; Sunday = 0, Monday = 1, and so on.
wDay
Specifies the current day of the month.
wHour
Specifies the current hour.
wMinute
Specifies the current minute.
wSecond
Specifies the current second.
wMilliseconds
Specifies the current millisecond.
Remarks
It is not recommended that you add and subtract values from the SYSTEMTIME structure to obtain relative times. Instead, you should
Convert the SYSTEMTIME structure to a FILETIME structure.
Copy the resulting FILETIME structure to a LARGE_INTEGER structure.
Use normal 64-bit arithmetic on the LARGE_INTEGER value.
I'm sorry Jim, but I can't get your code to work.......
Vlatko: I've already read that, but I doesn't understand/know how to do(I've tried, but failed)....any suggestions would be appreciated!?
Here's a quote from the Borland C++ 5 help:
Code:Syntax
#include <dos.h>
void gettime(struct time *timep);
void settime(struct time *timep);
Description
Gets and sets the system time.
gettime fills in the time structure pointed to by timep with the system's current time.
settime sets the system time to the values in the time structure pointed to by timep.
The time structure is defined as follows:
struct time {
unsigned char ti_min; /* minutes */
unsigned char ti_hour; /* hours */
unsigned char ti_hund; /* hundredths of seconds */
unsigned char ti_sec; /* seconds */
};
Return Value
None.
Thanks filburt!
I'll try that code tomorrow, right now it's bedtime here in Europe - midnight! Well, see you later!
I just compiled & ran this as a console app in VC++, I made two typos omn the original.
If you ever saw me type, you'd come to a whole new realization of what typographic errors are. My elbows just don't do not fit onto the top of just one key.
Code:// removed stdafx.h
#include <time.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]){
char tmp[80];
struct tm *ptr;
time_t lt;
lt = time(NULL);
ptr = localtime(<);
printf("%s\n",asctime(ptr) );
strcpy(tmp,asctime(ptr) );
return 0;
}
Jim, I tried your code(cut-'n'-paste), but I couldn't get it to work...maybe I do something wrong when I create the project, which the code is going to be pasted in.
-Win32 console application
-c++ source file
something wrong with this?
It complains about:
ptr = localtime(<);
I see the problem - it's a forum problem it's turning
ampersand lowercase L lowercase T into > I'll report the problem.
You can post like this to avoid the bug.
&[b][/b]lt
Code:#include <time.h>
#include <stdio.h>
#include <string.h>
void main(void){
char tmp[80];
struct tm *ptr;
time_t lt;
lt = time(NULL);
ptr = localtime(<);
printf("%s\n",asctime(ptr) );
strcpy(tmp,asctime(ptr) );
}
I tried your code thinktank and it works! Great! Thanks!
Thanks to everybody that have helped!
Try this.
Code:#include <iostream.h>
int main()
{
cout << __TIME__;
return 0;
}
wow, i didn't know there was a time thing in iostream.h?
Megatron I though than you were kidding me lol but that code work for real!!! Nice job
Cool code Megatron, didn't think there were such a function.....
thanks!
Ok, I understand Parksie.
Then the previous solutions were much better.....
It's common practice to put __FILE__ and so on in a what string, so you can use ident on the .EXE
This is a way of providing version control of .EXE images
A what string starts with 4 characters: @(#) - this can't be found as a part of a COFF format file -- in other words, if it's there, it was put there by people, not compilers/linkers.
You can do this with PC image files as well:
PCident
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int main(int argc, char* argv[])
{
FILE *in;
char one,two;
char tmp[128];
int i=0;
char what[5];
memset(what,'\0',5);
in=fopen(argv[1],"r");
assert(in != NULL);
for (i=0;i<4;i++){
what[i]=fgetc(in);
}
while (!feof(in )) {
if(!strcmp(what,"@(#)") ) {
memset(tmp,'\0',sizeof(tmp) );
fgets(tmp,127,in);
printf("%s\n",tmp);
break;
}
one=fgetc(in);
for(i=3;i>(-1);i-- ){
two=what[i];
what[i]=one;
one=two;
}
}
fclose(in);
return 0;
}
so thats not from IOSTREAM? or is it dependent on the compiler?Quote:
Originally posted by parksie
Erm...hehe...
__TIME__ is the time that the preprocessor hit that line...so it's effectively compilation time, not the time the program was run :p
No. If it's in that format (__SOMETHING__) then it's usually compiler or preprocessor-supplied.Quote:
Originally posted by sail3005
so thats not from IOSTREAM? or is it dependent on the compiler?
In this case, I think __TIME__ is required by ANSI...anyone got the standard?
Yes - it is ansi C standard, so is __FILE__, __DATE__
__TIME__ format: 5 Apr 2002 19:14:20
which is also what you get with __DATE__. I'm sure MS wouldn't have the huevos to change this in MSVC++, but you never know... and I've never checked. They diddled up fpos() and bsearch(), IMO, so why not something else?