time.h: time returns different times

This is a discussion on time.h: time returns different times within the C Programming forums, part of the General Programming Boards category; sorry for posting nearly directly but i doubt i would find the answer into so many posts returned by the ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    time.h: time returns different times

    sorry for posting nearly directly but i doubt i would find the answer into so many posts returned by the search.... so,

    linux date command on a shell return the date and time correctly... my following simple c program

    Code:
    int
    main ()
    {
      time_t *current;
      time(current);
      printf("%s", ctime(current));
      
      return 0;
    }
    always returns the following:
    Thu Jan 1 01:00:00 1970

    the following program:
    Code:
    int
    main ()
    {
      time_t *current;
      char dateStr[50];
      
      time(current);
      printf("%s", ctime(current));
      
      strftime(dateStr, sizeof(dateStr), "%d/%m/%Y %Z", localtime(current));
      printf("%s\n", dateStr);
      
      return 0;
    }
    always returns:
    Wed Apr 22 12:51:56 1970
    22/04/1970 BST

    can someone explain what is going on as i really have no clue why this is happening....

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    here is code which get the system time
    Code:
    #include<stdio.h>
    #include<time.h>
    
    int
    main ()
    {
      time_t current;
      time(&current);  
    printf("%s", ctime(&current));
      getchar();
      return 0;
    }
    my ouput
    Code:
    Tue May 24 09:59:17 2005
    s.s.harish

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    and second bit of code
    Code:
    #include<stdio.h>
    #include<time.h>
    
    int
    main ()
    {
      time_t current;
      char dateStr[50];
      
      time(&current);
      printf("%s", ctime(&current));
      
      strftime(dateStr, sizeof(dateStr), "%d/%m/%Y %Z", localtime(&current));
      printf("%s\n", dateStr);
      getchar();
      return 0;
    }
    s.s.harish

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    cheers, your code works just fine...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 08:11 AM
  2. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  3. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 05:46 PM
  4. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21