Thread: Time - not returning correct day

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Time - not returning correct day

    Code:
    #include <time.h>
    #include <stdio.h>
    
    main()
    {
            time_t t;
            struct tm* local_t = localtime(&t);
            if (local_t != NULL) {
            printf("Day of the month: %d\n", local_t->tm_mday);  
            printf("Month of the year: %d\n", local_t->tm_mon);
            printf("Day in the year: %d\n", local_t->tm_yday);
            }
    }
    This should print out as specified by the printf statements, yet I get

    6
    3
    95

    Does anyone know what the problem can be?

    Thanks
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    check this out.

  3. #3
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Ive looked at it, but I dont see the problem besides it being statically allocated.
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Im leaving work right now, I will take a look at it when I get home. Unless someone else helps out before then.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you had actually read the link given to you, you'd see the correct way to use the function. You never put any value into t, so in effect, it passes random crap on to the other function.

    In MSVC++, if I compile your code, the function returns NULL so your printf statements never execute.

    How about actually calling time first?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    If you would just read the info, its all there, with links to other interesting info about time.h. Here because I am nice is the way you want to do things.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
    
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      printf("Day of the month: %d\n", timeinfo->tm_mday);  
      printf("Month of the year: %d\n", (timeinfo->tm_mon) + 1);
      printf("Day in the year: %d\n", timeinfo->tm_yday);
                          //int tm_mon;    month (0 - 11 : 0 = January)
      return 0;
    }
    Last edited by stumon; 03-25-2003 at 09:26 PM.

  7. #7
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Thanks for the help!
    -MethodMan-

    Your Move:Life is a game, Play it; Life is a challenge, Meet it; Life is an opportunity, capture it.

    Homepage: http://www.freewebs.com/andy_moog/home.html

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by stumon
    If you would just read the info, its all there, with links to other interesting info about time.h. Here because I am nice is the way you want to do things.
    [---snip exact example in the first link---]
    If they didn't pay attention the first time through, what makes you think they will the second? It's nice to see someone make the effort to help, but you'd better slow down, you'll burn yourself out by the end of the week. Trust me on this, people never ever read links you give them. For example, there is a petition here to rename the FAQ to "NORT", or No One Reads This.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    ill sign it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merry Politically Correct Day
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-26-2005, 10:08 AM
  2. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM
  3. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  4. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM