Thread: Getting current time issue

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Getting current time issue

    Hello, I'd just got my first post in this forum. And this place seems like has a lot of resources . I just have this problem of mine with my project that I'm supposed to pass by this tuesday and I've been trying to fix this for already a week and have already tried a lot of things.
    The code is here:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    main()
    {
    time_t start_time,end_time;
    char* start,*end;
    printf("Press enter to get the date today\n");
    system("PAUSE");
    start_time = time(NULL);
    start = ctime(&start_time);
    printf("%s\n",start);
    printf("Enter again to get the latest date\n");
    system("PAUSE");
    end_time = time(end_time);
    end = ctime(&end_time);
    printf("%s\n",end);
    printf("And the time a while ago is %s\n",start);
    system("PAUSE");
    }
    I'm having a problem about storing the time information to a variable.My project is about payroll and I have a time-in and time-out of workers there. However, whenever I print their time-ins and time-outs in the display of workers, the time on time-in and time-out are always the same even though I got them from different time. I hope you will answer my question. Thanks!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's what I get when I compile your code:
    Code:
    $ make time
    gcc -Wall -Werror -ggdb3 -std=c99 -pedantic time.c -o time -lm -lpthread -lefence
    time.c:4:1: error: return type defaults to ‘int’ [-Werror]
    time.c: In function ‘main’:
    time.c:15:5: error: passing argument 1 of ‘time’ makes pointer from integer without a cast [-Werror]
    /usr/include/time.h:186:15: note: expected ‘time_t *’ but argument is of type ‘time_t’
    cc1: all warnings being treated as errors
    make: *** [time] Error 1
    Declare main to explicitly return an int, then return one at the bottom:
    Code:
    int main(void)
    {
        ...
        return 0;
    }
    Then, notice the difference between these two:
    Code:
    start_time = time(NULL);
    end_time = time(end_time);
    Why are you passing end_time to time()? The time() function expects a pointer to time_t, but end_time is just a plain time_t. Why not pas NULL to get the current time?

    Now, the root of your problem is that ctime returns a pointer to the buffer containing the time string. It does not create a new buffer each time you call it, it uses a single, static buffer. That means that start and end point to the same exact memory. The second time you call ctime, you change the contents, so start and end both point to the end time string. Either make a copy yourself, using strcpy, before you call ctime again, or use the re-entrant version ctime_r.
    Last edited by anduril462; 12-06-2012 at 11:47 AM. Reason: fixed a typo

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Your compiler should give you an error for line 15, because the time function takes a pointer type argument.

    But your use of the time() function also attempts to assign to end_time twice simultaneously.

    Try

    Code:
    end_time = time(NULL);

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Ah! Why didn't I ever think of making a copy of the string itself to prevent overwriting! Now problem solved! Thanks! However, I would just like to clarify some things. I don't know why you guys get errors but I don't? not even a warning.I'm using code blocks as my compiler and also have dev c++ which I don't usually use. And for that ctime_r, I can't use it. compiler said "unidentified reference to 'ctime_r' " what I did is this :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    int main()
    {
    typedef char string [128];
    time_t start_time,end_time;
    char* start,*end;
    string store[5];
    printf("Press enter to get the date today\n");
    system("PAUSE");
    start_time = time(NULL);
    ctime_r(&start_time,store[1]);
    printf("%s - %d\n",start,start_time);
    printf("Enter again to get the latest date\n");
    system("PAUSE");
    end_time = time(NULL);
    ctime_r(&end_time,store[2]);
    printf("%s - %d\n",end,end_time);
    printf("And the time a while ago is %s - %d\n",start,start_time);
    system("PAUSE");
    return 0;
    }
    And also with the "int main" and "return 0". We were also taught to use that on our program.However, I found out that nothing happens to the program if I don't put it. I'm just wondering what is that for? It's just that I might have already be missing some concepts about that.

    And also for the "end_time = time(end_time)". That's my bad. I forgot to change it back to the original code. Like I said a while ago, I was already been messing with that code for a long time. And for gemere, changing the argument of the time to NULL didn't do the trick.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code::Blocks is technically just an IDE, not a compiler. The warnings you get, and whether ctime_r is available will depend on the implementation you use (compiler + standard library).

    As for "int main", take a look here: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com. Basically, the "main" version is the old way (C89), but it's still tolerated. The new way (C99) requires "int main". Technically, in C99 the return 0; is optional at the end, but I always recommend it.

    The general rule I follow is: always be explicit. If you are explicit in what you are doing, there is no need to guess what the computer will do, and there is less room for error. I even prefer
    Code:
    int main(void)
    Where the void makes it clear that I do not intend to use any command line parameters.

    The only reason to do just "main()" would be to save a few keystrokes when typing, but that is so tiny as to not really matter in the long run.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Ooh. So that's the reason for that. I'll now try to put into practice what you had said. Thanks again! It's good to be in this place

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The document says "allows a return code to be passed to the invoker". So yes, if you omit return 0 it is implied. By putting it in makes it explicit to the next programmer who might be called on to maintain your code. Also, you could return different integers - but this is useful if your program is executed by another program which is looking for some status / error code from you. ‘zero’ is the standard value for “OK”. Other integers could mean some error condition that your program and its caller agrees on to interpret correctly.

    The function exit(n) can be used anywhere in a program if you need to exit immediately – even from within a function. It’s like executing the ‘return n’ at the bottom of main.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    btw apologies to anduril - the post timings didn't seem to reflect it, but I did
    check that the original post hadn't been answered to when I pressed the 'reply'
    button, only to discover it had when the page updated.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by gemera View Post
    btw apologies to anduril - the post timings didn't seem to reflect it, but I did
    check that the original post hadn't been answered to when I pressed the 'reply'
    button, only to discover it had when the page updated.
    No worries, and no need to apologize. That sort of thing happens all the time around here. Heck, it happened to me just last night.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  2. Converting Zulu Time to Current Time
    By Caldus in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 08:54 PM
  3. current time
    By lithium in forum C Programming
    Replies: 3
    Last Post: 01-26-2003, 05:42 PM
  4. How can I get the current time?
    By CompiledMonkey in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 07:12 AM
  5. current time
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-21-2002, 09:48 AM