|
-
Oct 10th, 2001, 02:11 PM
#1
Thread Starter
Addicted Member
Current time
Anyone knows how to get the current time into a variable?
-
Oct 10th, 2001, 02:23 PM
#2
[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
-
Oct 10th, 2001, 03:02 PM
#3
Frenzied Member
If you are making a Win app, you can also use the WIn32 API
GetLocalTime
The GetLocalTime function retrieves the current local date and time.
VOID GetLocalTime(
LPSYSTEMTIME lpSystemTime // address of system time structure
);
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.
-
Oct 10th, 2001, 04:46 PM
#4
Thread Starter
Addicted Member
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!?
-
Oct 10th, 2001, 04:49 PM
#5
Member
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.
-
Oct 10th, 2001, 05:07 PM
#6
Thread Starter
Addicted Member
Thanks filburt!
I'll try that code tomorrow, right now it's bedtime here in Europe - midnight! Well, see you later!
-
Oct 10th, 2001, 10:54 PM
#7
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;
}
-
Oct 11th, 2001, 11:44 AM
#8
Thread Starter
Addicted Member
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(<);
-
Oct 11th, 2001, 12:41 PM
#9
I see the problem - it's a forum problem it's turning
ampersand lowercase L lowercase T into > I'll report the problem.
-
Oct 11th, 2001, 04:42 PM
#10
Lively Member
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) );
}
Marriage - is not a word, but a sentence.
-
Oct 12th, 2001, 09:20 AM
#11
Thread Starter
Addicted Member
I tried your code thinktank and it works! Great! Thanks!
Thanks to everybody that have helped!
-
Oct 12th, 2001, 02:33 PM
#12
Try this.
Code:
#include <iostream.h>
int main()
{
cout << __TIME__;
return 0;
}
-
Oct 12th, 2001, 02:37 PM
#13
PowerPoster
wow, i didn't know there was a time thing in iostream.h?
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Oct 12th, 2001, 04:29 PM
#14
Megatron I though than you were kidding me lol but that code work for real!!! Nice job
-
Oct 13th, 2001, 04:04 AM
#15
Thread Starter
Addicted Member
Cool code Megatron, didn't think there were such a function.....
thanks!
-
Oct 13th, 2001, 04:07 PM
#16
Thread Starter
Addicted Member
Ok, I understand Parksie.
Then the previous solutions were much better.....
-
Oct 13th, 2001, 04:35 PM
#17
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;
}
-
Oct 13th, 2001, 04:38 PM
#18
PowerPoster
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
so thats not from IOSTREAM? or is it dependent on the compiler?
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Oct 13th, 2001, 04:42 PM
#19
Monday Morning Lunatic
Originally posted by sail3005
so thats not from IOSTREAM? or is it dependent on the compiler?
No. If it's in that format (__SOMETHING__) then it's usually compiler or preprocessor-supplied.
In this case, I think __TIME__ is required by ANSI...anyone got the standard?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 13th, 2001, 04:53 PM
#20
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?
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
|