[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to read the system time and date in dos (with the time ticking).

Posted on 2004-11-06
4
Medium Priority
?
508 Views
Last Modified: 2008-01-16
Dear experts, professionals, and gurus

How to read the system time and date in dos?? Because i'm doing a library information system for my school assignment for MS-DOS and i want to have the time and date displayed (with the time ticking).

For your informaiton, i'm using bloodshed dev C++ 4.9.9.0 compiler, so I hope that the solutions given to me is compilable with that compiler.

Thanks.
0
Comment
Question by:qweweng
4 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 300 total points
ID: 12517446
Windows has Windows-specific functions, but if you want to use the standard functions, something like this would work:

#include <ctime>
...
void PrintTime()
{
  time_t now = time(NULL); // Get the time
  struct tm *time_struct = localtime(&now); // Convert it to a struct that's easy to use
  char time_string[100] = {0}; // Create a variable to hold the time string
  strftime(time_string, 100, "%m/%d/%Y %H:%M:%S", time_struct); // Format the string
  cout << time_string << endl; // Output the string
}
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12517461
Example

/* MKTIME.C: The example takes a number of days
 * as input and returns the time, the current
 * date, and the specified number of days.
 */

#include <time.h>
#include <stdio.h>

void main( void )
{
   struct tm when;
   time_t now, result;
   int    days;

   time( &now );
   when = *localtime( &now );
   printf( "Current time is %s\n", asctime( &when ) );
   printf( "How many days to look ahead: " );
   scanf( "%d", &days );

   when.tm_mday = when.tm_mday + days;
   if( (result = mktime( &when )) != (time_t)-1 )
      printf( "In %d days the time will be %s\n",
              days, asctime( &when ) );
   else
      perror( "mktime failed" );
}


Output

Current time is Tue May 03 12:45:47 1994

How many days to look ahead: 29
In 29 days the time will be Wed Jun 01 12:45:47 1994



You could also use strftime to convert a date/time to a string value.


Regards, Alex
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12517491
I suppose you could do something like this:

#include <ctime>
#include <iostream>
#include <windows.h>
#include <process.h>

using namespace std;

unsigned __stdcall ThreadFunc(void* pParam)
{      
      HANDLE hConsole = GetStdHandle (STD_OUTPUT_HANDLE); // Get a handle to standard output
      COORD pos = {0, 0};      
      while (true) {
            SetConsoleCursorPosition (hConsole, pos);
            time_t curr = time(0);
            cout << ctime(&curr) << endl;
            Sleep(1000);
      }
}

int main()
{    
      HANDLE hThread;

      hThread = (HANDLE)_beginthreadex(NULL, NULL, &ThreadFunc, NULL, 0, 0);

      while (true) {
            Sleep(INFINITE);      
      }

      CloseHandle(hThread);
}

You need to be running a secondary thread in which you update the time.  Here I have set the thread to update the time once per second, and have the time output on the top line of the console (0,0).  

HTH
0
 

Expert Comment

by:Tirumaleshwar
ID: 12521616
Hai,
     char szDate[128];
     SYSTEMTIME st;
     GetLocalTime(& st);
     sprintf(szDate, "%02d:%02d:%02d %4d/%02d/%02d", st.wHour, st.wMinute, st.wSecond,  st.wYear, st.wMonth, st.wDay);

Tiru
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question