Thread: Time/Date getting access to?

  1. #1
    DirtyHarry
    Guest

    Post Time/Date getting access to?

    void tyme(void)
    {
    time_t tim = time(NULL);
    printf("\n\n\t\t Time: %s", ctime(&tim));
    }

    Hi want to be able to take the date or time out so as to append it to a transaction as it occurs in my code. Use the above to display time and date, but unsure how to actually break the time into components, eg min,hour - day,month,year. Can I put these into variables. Anyway not sure how to access them. Looked thru. lots of books with all sorts of details but none showing implementation.
    Thanks,
    H.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
       time_t now;
       if ( time(&now) != (time_t)(-1) )
       {
          struct tm *today = localtime(&now);
          if ( today != NULL )
          {
             const char Iso8601[] = "%Y-%m-%dT%X";
             char buffer[20];
             if ( strftime(buffer, sizeof(buffer), Iso8601, today) > 0 )
             {
                puts(buffer);
             }
          }
       }
       return(0);
    }
    
    /* sample output 
    2003-04-16T15:01:06
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of access labels in class definition
    By Mario F. in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2006, 07:13 AM
  2. ww hosting and SSH access
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-07-2005, 08:49 AM
  3. Replies: 3
    Last Post: 09-22-2003, 09:48 PM
  4. Direct disk access in DOS
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-26-2002, 02:52 PM