Thread: date, time, and baning my head against the wall

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    date, time, and baning my head against the wall

    I've copies and pasted code from all over the place for the simple task of getting the system date and time, but not a single piece of code actually does what it's supposed to.

    Code:
    caibbor@helios ~ $ cat one.cpp 
    
    #include <time.h>
    
    void main() {
    char sdate[9];
    char stime[9];
    _strdate( sdate );
    _strtime( stime );
    }
    
    caibbor@helios ~ $ g++ one.cpp 
    one.cpp:4:11: error: '::main' must return 'int'
    one.cpp: In function 'int main()':
    one.cpp:7:17: error: '_strdate' was not declared in this scope
    one.cpp:8:17: error: '_strtime' was not declared in this scope
    caibbor@helios ~ $ cat two.cpp 
    //Date
    #include <iostream>
    #include <ctime>
     
    int main ()
    {
      char date [10];
      _strdate(date);
      std::cout<<"Current Date:"<<date;
      return 0;
    }
    
    
    caibbor@helios ~ $ g++ two.cpp 
    two.cpp: In function 'int main()':
    two.cpp:8:16: error: '_strdate' was not declared in this scope
    caibbor@helios ~ $ cat three.cpp 
    //Time
    #include <iostream>
    #include <ctime>
     
    int main ()
    {
      char time [10];
      _strtime(time);
      std::cout<<"Current Time:"<<time;
      return 0;
    } 
    
    
    caibbor@helios ~ $ g++ three.cpp 
    three.cpp: In function 'int main()':
    three.cpp:8:16: error: '_strtime' was not declared in this scope
    caibbor@helios ~ $

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose if "_strtime" or "_strdate" existed at all, you'd be somewhere. Since they don't....

    You can look up time.h/ctime if you want to see what's available there. You probably want ctime + time.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would think that this one is a no-brainer:
    Code:
    one.cpp:4:11: error: '::main' must return 'int'
    Funnily enough this actually means that main must be declared as returning int, not void.
    You got it right two out of three times.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Are you looking for the time in any particular format? Because C++ includes a number of time and date functions in the <ctime> header file, inherited from C's time.h header. Here's a simple example.
    Code:
    #include <iostream>
    #include <ctime>
    
    int main() {
        time_t t;
        std::time(&t);
        std::cout << std::ctime(&t);
        return 0;
    }
    If you want more control start reading about <ctime>. A good place to start is "man ctime" (which brings you the man page for ctime, localtime, asctime, etc.).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed