Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.

Similar presentations


Presentation on theme: "1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member."— Presentation transcript:

1 1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member functions CSE 20232 Lecture 23 – Time & inline functions

2 2 Reading, Etc. Weekly Reading: Sections: 10.1-10.8 11.1-11.14 Later this week Complex Numbers Mandelbrot and Julia Sets

3 3 UNIX date command Returns a formatted string indicating current date and time Default Format: DayName MonthName Day hh:mm:ss TZ Year Examples: Fri Dec 23 10:10:42 EST 1988 Tue Oct 24 23:08:30 EDT 2006

4 4 Using date in a program Use system command to run the date command and redirect its output into a file Open file and use extraction operator to read parts of formatted date Close and remove the file

5 5 Using date in a program ifstream inf; string dayOfWk, Month, Day, hhmmss, Z, Year; // run date command, save output in file system("date > xyztodayxyz.dat"); // use an odd name // open file and extract the date and time // format example: Fri Dec 23 10:10:42 EST 1988 inf.open("xyztodayxyz.dat"); inf >> dayOfWk >> Month >> Day >> hhmmss >> Z >> Year; inf.close(); // remove the file system("rm xyztodayxyz.dat");

6 6 Example (myclock.cpp) Determines date and launches itself over again in a separate window to display the date If argc is 1, system("date > xyztodayxyz.dat"); system(“xterm –e myclock xyztodayxyz.dat &”); exit(0); If argc is > 1, // open file and display date

7 7 myclock.cpp code // myclock.cpp -- JHS 2006 Notre Dame #include using namespace std; int main(int argc, char *argv[]) { if (argc == 1) { // save date in file, restart program inside xterm // with filename for date info, and exit system("date > xyztodayxyz.dat"); system("xterm -e myclock xyztodayxyz.dat &"); exit(0); } // some variables so we can read date info from file ifstream inf; string dayOfWk, Month, Day, hhmmss, Z, Year;

8 8 myclock.cpp code // date format example: Fri Dec 23 10:10:42 EST 1988 // read date information & dispose of file inf.open("xyztodayxyz.dat"); inf >> dayOfWk >> Month >> Day >> hhmmss >> Z >> Year; inf.close(); system("rm xyztodayxyz.dat"); // display date information (in the xterm) cout << dayOfWk << endl; cout << Month << endl; cout << Day << endl; cout << hhmmss << endl; cout << Z << endl; cout << Year << endl; // wait a bit, then quit system("sleep 5"); return 0; }

9 9 Time and ctime functions Defined in the header Both use the time_t data type The time(time_t *t) function has a pointer (or address) to a time_t structure as its parameter It sets t to point to the number of seconds since Jan 1, 1970 The char *ctime(time_t *t) function converts the time pointed to by t into local time as a string

10 10 Using time and ctime // some character arrays (C-style strings), etc char hhmmss[32], date_time[64]; char dayName[4], monthName[4], day[3], year[5]; int hour, minute, second; // a time_t variable named ltime time_t ltime; time (&ltime); // pass address of ltime to the function // now convert the ltime seconds to local time as a string // NOTE: sprintf is like printf and printw, but prints into a // char array sprintf(date_time,"%s", ctime(&ltime)); // now we can use sscanf to extract the parts out of the date sscanf(date_time,"%s %s %s %s %s", dayName, monthName, day, hhmmss, year); // get strings sscanf(hhmmss,"%d:%d:%d", &hour, &minute, &second); // get numbers

11 11 Inline functions We can declare functions “inline” to … Allow the compiler to optimize use of the function Using code substitution rather than functino calls and returns Note: the compiler has the final say about whether the function is implemented as a function or is used to perform inline code substitution

12 12 Inline example // declaration of inline function (note use of “inline”) inline int plus5 (int x) { return x + 5; } // original call on line 120 of code a = b + plus5(c); // the compiler will compile code AS IF the following was // the actual line 120 of code. That is, it substitutes // the body of the function in place of the function call // but uses the name of the actual parameter a = b + (c + 5);

13 13 When does the compiler NOT substitute If the inline function is too complex, the compiler will usually generate a normal function call So keep inline functions simple Just a few lines Straightforward algorithms Etc

14 14 Inline member functions in a C++ class Inline member functions are implemented within the class declaration In the header file Without the “inline” indicator Constructors, destructors and simple accessor functions are often done this way

15 15 Inline member function Example // point.h header file class point { public: // example inline member functions point(int x=0, int y=0) : xPosit(x), yPosit(y) { } int getX() const { return xPosit; } // regular member functions implemented elsewhere point operator+ (point p); point moveTo(int x, int y);...... private: xPosit, yPosit; };

16 16 Implementing a ticking clock inline void tick (int &hr, int &min, int &sec) { usleep(1000000); // wait a second sec++; // increment seconds if (sec == 60) { sec = 0; // reset seconds min++; // increment minutes if (min == 60) { min = 0; // reset minutes hr++; // increment hours if (hr == 24) // this is a 24 hour clock { hr = 0; // reset hours }

17 17 Put it all together See examples myclock.cpp Uses date command and temp file Shows details of current time in xterm myclock2.cpp Uses time and ctime functions Shows ticking clock in xterm using curses I/O

18 18 Current program Questions about … Images Files Make Makefiles, rules & targets …


Download ppt "1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member."

Similar presentations


Ads by Google