// DateTime1.cpp
//
// Illustrates the use of the time() and localtime() functions.
// 
// Fetches the current number of seconds since Jan 1, 1970 off the system clock
// using the time() function.  This quantity is then passed to the localtime()
// function and the day of the week, the date and the time are then displayed.
//
// The function continually loops.
//
// copyright, Peter H. Anderson, Oct 29, '09

#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <string.h>

void delay_ms(clock_t millis);

void display_wday(struct tm *p);
void display_date_time(struct tm *p);

int main(void)
{
     time_t current_seconds;  // long
     struct tm *p_dt;
                                     
     while(1)
     {
             time(¤t_seconds);
             p_dt = localtime(¤t_seconds);                         
             display_wday(p_dt);
             display_date_time(p_dt);
             delay_ms(1000);
     }
     return(0);
}     

void display_wday(struct tm *p)
{
     const char day_names[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
     printf("%s ", day_names[p->tm_wday]);
}

void display_date_time(struct tm *p)
{     
     printf("%02d/%02d/%04d\t", 
                      (p->tm_mon)+1, p->tm_mday, (p->tm_year)+1900);
     printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);
}

void delay_ms(clock_t millis)
{
   clock_t endtime; // long
   endtime = millis + clock();
   while( endtime > clock() )        ;
   
}  

#ifdef YYY
// struct tm is defined in time.h
struct tm
{
   int   tm_sec;      /* Seconds: 0-59 (K&R says 0-61?) */
   int   tm_min;      /* Minutes: 0-59 */
   int   tm_hour;   /* Hours since midnight: 0-23 */
   int   tm_mday;   /* Day of the month: 1-31 */
   int   tm_mon;      /* Months *since* january: 0-11 */
   int   tm_year;   /* Years since 1900 */
   int   tm_wday;   /* Days since Sunday (0-6) */
   int   tm_yday;   /* Days since Jan. 1: 0-365 */
   int   tm_isdst;   /* +1 Daylight Savings Time, 0 No DST,
             * -1 don't know */
};
#endif