question about using ctime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • progstuff
    New Member
    • Jan 2010
    • 3

    question about using ctime

    How do I use the library ctime to set the current time in: seconds, hours (24 hour), minutes into separate integers.

    And how do i do the same thing with dates setting the current year, day , month into separate integers.

    This is so i can use them to use the integers instead of user input and the user typing in the date themselves.
    In c++.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Read the ctime reference. There are only 9 functions so it should take you long to find the one you need.

    Comment

    • progstuff
      New Member
      • Jan 2010
      • 3

      #3
      I think i found it. struct tm right?
      Code:
      #include <stdio.h>
      #include <time.h>
      
      int main ()
      {
        time_t rawtime;
        struct tm * timeinfo;
      
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        printf ( "Current local time and date: %s", asctime (timeinfo) );
        
        return 0;
      }
      this is a dumb question...
      i dont see how to implement a "tm structure" as the return value

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Your code works fine and is probably as you intended
        Code:
        #include<iostream>//added
        #include <stdio.h>
        #include <time.h>
        using namespace std; //added
        
        int main ()
        {
          time_t rawtime;
          struct tm * timeinfo;
         
          time ( &rawtime );
          timeinfo = localtime ( &rawtime );
          printf ( "Current local time and date: %s", asctime (timeinfo) );
          cout<<"\n";
         
         char q;
         cout<<"Press any key to continue...";
         cin>>q;
         return 0;//these last 4 added
        }
        printed: Current local time and date: Thu Jan 14 13:14:01 2010
        Press any key to continue.

        Comment

        • progstuff
          New Member
          • Jan 2010
          • 3

          #5
          sorry that was example code from cplusplus reference... i dont think you read my first post...
          im trying to say, and i quote myself

          How do I use the library ctime to set the current time in: seconds, hours (24 hour), minutes into separate integers.

          And how do i do the same thing with dates setting the current year, day , month into separate integers.
          The example was the function i was going to use but i dont quite know how to use it...
          i need to refine that code to fit my needs hence the quote.

          i know no matter what function it is i need to use struct tm
          p.s. read the quote

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            My answer would probably be only convert to struct tm as required (i.e. locally to any given function), pass the time around as a time_t.

            Is this C or C++.

            If it is C++ you can always just return a struct tm. Note don't return the struct tm* pointer because that points to static data it would be overwritten on the next call to localtime (or gmtime). Return *timeinfo.

            In C returning structures is generally considered bad practice. Good C practice is to pass in a pointer to an already allocated block of data and let the called function fill it in. You could do that with struct tm.

            Comment

            Working...