Time and Date Display

#include<DAN.h> 0 Tallied Votes 178 Views Share

This program uses snipets from a couple of other programs that I put together in order to make a program that displays the time and date and refreshes the time and date every second. I commented practically every line to explain, as far as I know, what it does. If you know of an easier way to do any of the functions in this program or a more in depth description of what each line is really doing, please comment this and I will fix or change what ever I have to and I'll gladly add your name as a contibutor to this program. ^_^

// This program consists of some program snipets that were pieced together by
// #include<DAN.h>. The result of this compilation is a program that displays
// the time and date and refreshes every second.
#include <time.h>   // This includes the time.h header file for getting the 
                    // current time and for waiting
#include <iostream> // This includes the iostream.h header file for input and
                    // output

using namespace std; // Defines the namespace being used

void wait ( int seconds ) // The function used to wait
{
     clock_t endwait;                                // All of this just tells 
     endwait = clock () + seconds * CLOCKS_PER_SEC ; // the program to wait 
     while (clock() < endwait) {}                    // however many seconds 
                                                     // are in the () after
                                                     // wait in the main part
                                                     // of the program
}

int main ()
{
     bool on = true; // this bool is just used to repeat the time display 
                     // after the wait and clear screen
     while (on != false) // repeats whats in the loop
     {
          time_t rawtime;                     // This all just gets
          struct tm * timeinfo;               // and the time and 

          time ( &rawtime );                  // date to be displayed
          timeinfo = localtime ( &rawtime );  // in the next line
          cout << "Current local time and date: " << asctime (timeinfo); 
          // the line above displays the time and date
    
          wait (1);      // waits 1 second
          system("cls"); // clears the screen for the next display
     }
return 0;
}

Dani AI

Generated

Good simple demo from @#include&lt;DAN.h&gt;, but a few practical fixes will make it more portable, efficient and correct.

That mysterious header that asked about isn't part of the standard library — it's either a local/custom file or just a placeholder. If you don't have a real file named that, remove the include. If it is a local header, include it with quotes (for example, #include "MyHeader.h") and keep third‑party headers with angle brackets.

The main problem hit (seconds running oddly fast) comes from the busy-wait that uses clock(). That loop spins the CPU and measures processor time, not wall clock time. Prefer sleeping to yield the CPU; in modern C++ use std::this_thread so the program doesn't burn 100% CPU:

#include <chrono>
#include <thread>

std::this_thread::sleep_for(std::chrono::seconds(1));

For nicer, safer time formatting, avoid asctime/localtime directly (they use static storage and are not thread-safe). Copy the tm and use std::put_time or std::strftime to control output:

#include <iomanip>
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);               // copy for safety
std::cout << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << '\n';

Finally, is right that clearing differs by platform, but calling system() is brittle and a security risk. For simple terminal apps use an ANSI escape sequence (many terminals support it) or a proper library (ncurses, PDCurses, Qt/SFML for GUIs). Example (ANSI):

std::cout << "\x1B[2J\x1B[H";

Other small tips: avoid a global using namespace std; in larger projects, provide a clean exit condition instead of an endless loop, and compile with C++11 or later to use the modern facilities shown above.

Samlouel 0 Newbie Poster

can you explain whats the function Of #include <DAN.h>?
this kind of syntax are not in my computer library, how can i achieve it?
can i download this syntax header?

mohamed yousef 0 Newbie Poster

first i'd like to thank you vey this powerful code you have made but
iam tryinh\g to do this by using the structure this is required from me to do the time by structures and i face a problem taht the counting of seconds is very fast what can i do to make delay
and also ther is some thing if i want to use the function window() for good interface what should i include for the header file
:( :) ;)

stephen84s 550 Nearly a Posting Virtuoso Featured Poster

To get this code to work on Linux, just replace the line system("cls"); with system("clear") .
BTW gotta say a very nice and useful piece of code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.