Date & Time

This is a discussion on Date & Time within the C++ Programming forums, part of the General Programming Boards category; Hi, I needed to extract current machine date in my application. Can "_strdate" be used in Linux? Also, is there ...

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Date & Time

    Hi, I needed to extract current machine date in my application. Can "_strdate" be used in Linux? Also, is there any Time & Date library like MFC's CTime (but a free one)?

    Thanks in advance.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    What's wrong with the standard?
    Last edited by SlyMaelstrom; 08-15-2006 at 03:11 AM.
    Sent from my iPadŽ

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by SlyMaelstrom
    What's wrong with the standard?
    Well, I want a simpler approach of date & time. You know, like Visual Basic's time variabel. AFAIK, the ctime's output is in second. The reason is, I wanted to, for example, substract or add dates like this:
    Code:
     date date1, date2,date3;
     date3=date2-date1;
    without making my own code from ctime. AFAIK, it can be easily done in VB.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Code:
    /* from http://www.cplusplus.com/ref/ctime/asctime.html */
    
    /* asctime example */
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
    
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      printf ( "Current date and time are: %s", asctime (timeinfo) );
      
      return 0;
    }
    
    // Output:
    // Current date and time are: Sat May 20 15:21:51 2000
    It's really not all that much work. Anyway, you're not going to find a more standard approach than this, so I'd suggest you use it.
    Sent from my iPadŽ

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    He prob wants it in C++, you see. I don't think he actually did, Sly ... sorry.
    Last edited by twomers; 08-15-2006 at 03:41 AM.

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by SlyMaelstrom
    Code:
    /* from http://www.cplusplus.com/ref/ctime/asctime.html */
    
    /* asctime example */
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
    
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      printf ( "Current date and time are: %s", asctime (timeinfo) );
      
      return 0;
    }
    
    // Output:
    // Current date and time are: Sat May 20 15:21:51 2000
    It's really not all that much work. Anyway, you're not going to find a more standard approach than this, so I'd suggest you use it.
    Thanks. Seems like I skipped reading the link. Although it's not what I have in mind, but I guess that will do for now. Anyway, what I'm trying to do is making a similar type like VB's date. In VB, I can just insert, eg.:
    Code:
    dim date1,date2 as date
    date1 = '01/01/1990'
    date2 = '01/01/1991'
    date1=date2-date1

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Do the above, ok, but get two times a few seconds apart (use a Sleep function or something), then subtact the t_time's from eachother, and it'll tell you the time difference.

    Then modify that so that you can set the date, and it will create the 'rawtime' varible for you.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Is it really that difficult to convert the output to C++? As I said, that was taken from the website...
    ...
    ... fine...
    Code:
    #include <iostream>
    #include <ctime>
    
    int main ()
    {
      std::time_t rawtime;
      std::tm *timeinfo;
    
      std::time( &rawtime );
      timeinfo = std::localtime( &rawtime );
      std::cout << "Current date and time are: " << asctime(timeinfo);
      
      return 0;
    }
    
    // Output:
    // Current date and time are: Tue Aug 15 05:35:44 2006
    Happy now? Geez...

    [EDIT]Oh good... all seems well. Anyway, do as twomers says for getting the difference. If you really want a date type like VB's, it's not too hard to implement yourself.
    Last edited by SlyMaelstrom; 08-15-2006 at 03:39 AM.
    Sent from my iPadŽ

  9. #9
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by SlyMaelstrom
    Is it really that difficult to convert the output to C++? As I said, that was taken from the website...
    ...
    ... fine...
    Code:
    #include <iostream>
    #include <ctime>
    
    int main ()
    {
      std::time_t rawtime;
      std::tm *timeinfo;
    
      std::time( &rawtime );
      timeinfo = std::localtime( &rawtime );
      std::cout << "Current date and time are: " << asctime(timeinfo);
      
      return 0;
    }
    
    // Output:
    // Current date and time are: Tue Aug 15 05:35:44 2006
    Happy now? Geez...
    Well what I wanted is inputting string for a date. Like: '01/01/1990' and it will be converted to a time_t variable. And after looking through the website again I found the "mktime" function. So, maybe I'll parse the string with sscanf and inputted them to a tm struct. Then the tm struct wil be converted by mktime. One more question though, if I only inserted the tm_mday, tm_month, and tm_year, can it still be converted to time_t?

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Yes, you can do anything you like.

    And by the way, don't skip links. They are normally put there to help people who have questions.


    Here's a really simple timing class I made before, if you're interested.

    Code:
    class TimeIt 
    {
    
    private:
    	time_t Start;
    	time_t End;
    
    public:
    	TimeIt	( void );
    
    	void Clear	( void );
    
    	void StartTimer	( void );
    	void EndTimer	( void );
    
    	double GetTime	( void );
    
    };
    
    
    
    TimeIt::TimeIt( void )
    {
    	Clear();
    }
    
    void TimeIt::Clear	( void )
    {
    	Start = 0;
    	End   = 0;
    }
    
    
    
    void TimeIt::StartTimer ( void )
    {
    	Start = clock();
    }
    void TimeIt::EndTimer	( void )
    {
    	End = clock();
    }
    
    double TimeIt::GetTime( void )
    {
    	return (double)( End-Start )/1000;
    }
    edit - hint for modifying it. The above tells the number of seconds in an interval. How many seconds in a minute? How many minutes in an hour? How many hours in a day? How many days in a week? How many weeks in a month? How many months in a year? etc
    Last edited by twomers; 08-15-2006 at 03:51 AM.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM
  2. Getting time and date
    By winsonlee in forum C++ Programming
    Replies: 3
    Last Post: 08-18-2004, 11:31 PM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  4. Help: Display time and date
    By stansu in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2003, 02:27 PM
  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