Find the answer to your Linux question:
Results 1 to 5 of 5
Am running ctime to timestamp a repeating operation. Had trouble getting date correct so wrote this simple program to test my logic and formatting: #include <stdio.h> #include <time.h> // Gets ...
  1. #1
    Linux User
    Join Date
    Mar 2008
    Posts
    287

    ctimes different

    Am running ctime to timestamp a repeating operation. Had trouble getting date correct so wrote this simple program to test my logic and formatting:
    #include <stdio.h>
    #include <time.h>

    // Gets System Date and Time

    int main(void)
    {
    int min, sec;
    char str[26], da[3], hr[3], mn[3], DOW[4], MTH[4], SC[3], YEAR[5];
    time_t now;

    if( (now = time(NULL)) == (time_t) - 1 ) {
    puts("FAIL: Unable to get time");
    } else {
    printf("TEST ctime(&now):%s", ctime(&now));
    // FORMAT: 'DOW MTH DA HR:MN:SC YEAR'
    // Spaces ignored by conversion! Use %*1s to skip adjacent character
    sscanf(ctime(&now), "%*3s%*3s%2s%2s%*1s%2s", da, hr, mn);
    printf("TEST-1 strings: da=%s hr=%s mn=%s\n", da, hr, mn);
    }
    }
    AND it works! Values are for today at the correct time.
    Problem: when I use this in the code to extract da, hr, mn, ctime is returning a date of Nov 12, 1935 and when called repeatedly the minutes do not change.
    Yes, I have print statement with CTIME to validate this in the other program.
    The sscanf and printf lines were copied to the other program just to be sure.
    Any help ?

  2. #2
    Linux User
    Join Date
    Mar 2008
    Posts
    287
    Perhaps I need to simplify. Here is the program which worked for me but stripped down:
    #include <stdio.h>
    #include <time.h>

    // Gets System Date and Time
    int main(void)
    {
    time_t now;
    if( (now = time(NULL)) == (time_t) - 1 ) { //<---
    puts("FAIL: Unable to get time"); //<---
    } else { //<---
    printf("TEST ctime(&now):&#37;s", ctime(&now));
    } //<---
    }
    RESULT: TEST ctime(&now):Mon Oct 6 04:10:39 2008

    Googling I found another script which I show here stripped down:
    #include <stdio.h>
    #include <time.h>

    int main (void)
    {
    time_t eot;
    printf("Today's (ctime(&eot)) date is: %s\n", ctime(&eot));
    }
    RESULT: Today's (ctime(&eot)) date is: Tue Sep 15 12:35:28 1931

    When the 1st program gets the //<--- lines removed it also returns an inaccurate date.
    Can someone tell me why? Note the 1st program does (should?) not execute the 1st half of the "if" when the "if" is used.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You have a variable of type time_t, which can hold a date/time.

    When you remove the lines containing //<--- from your program, you no longer call time(). That's the function that fills the variable with the current time.

    If you don't fill that variable with something, it's going have any old value in it, and ctime() will convert to printable format what that value is.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Linux User
    Join Date
    Mar 2008
    Posts
    287
    Thanks Bill,
    Yes, after "retiring", I came up with that solution too. Ran a test and proved it this am. Again thanks.
    I just went back to the ctime man page and still don't see this line of code explained.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Now I'm the puzzled one. :)

    Which line of code exactly do you have a question about, and what's the question?
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...