Results 1 to 20 of 20

Thread: Current time

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    Current time

    Anyone knows how to get the current time into a variable?
    [p r a e t o r i a n]

  2. #2
    jim mcnamara
    Guest
    [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(&lt);
    printf("%s\n",asctime(ptr) );
    strcpy(tmp,asctime(ptr) );

    }

    tmp now has has something like:

    Wed Oct 11 12:20:20 2001

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    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!?
    [p r a e t o r i a n]

  5. #5
    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Thanks filburt!

    I'll try that code tomorrow, right now it's bedtime here in Europe - midnight! Well, see you later!
    [p r a e t o r i a n]

  7. #7
    jim mcnamara
    Guest
    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(&lt); 
    	printf("%s\n",asctime(ptr) ); 
    	strcpy(tmp,asctime(ptr) ); 
    	return 0;
    }

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    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(<);
    [p r a e t o r i a n]

  9. #9
    jim mcnamara
    Guest
    I see the problem - it's a forum problem it's turning
    ampersand lowercase L lowercase T into > I'll report the problem.

  10. #10
    Lively Member
    Join Date
    Jun 2001
    Location
    Banana Republic
    Posts
    115
    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(&lt);
        printf("%s\n",asctime(ptr) );
        strcpy(tmp,asctime(ptr) );
    
    }
    Marriage - is not a word, but a sentence.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    I tried your code thinktank and it works! Great! Thanks!

    Thanks to everybody that have helped!
    [p r a e t o r i a n]

  12. #12
    Megatron
    Guest
    Try this.
    Code:
    #include <iostream.h>
    
    int main()
    {
    	cout << __TIME__;
    	return 0;
    }

  13. #13
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    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

  14. #14
    DaoK
    Guest
    Megatron I though than you were kidding me lol but that code work for real!!! Nice job

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Cool code Megatron, didn't think there were such a function.....

    thanks!
    [p r a e t o r i a n]

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Ok, I understand Parksie.

    Then the previous solutions were much better.....
    [p r a e t o r i a n]

  17. #17
    jim mcnamara
    Guest
    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;
    }

  18. #18
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    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

  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  20. #20
    jim mcnamara
    Guest
    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
  •  



Click Here to Expand Forum to Full Width