// DateTime2.cpp
//
// Simulates the turning on of a pump at the top of each hour for five minutes.
// However, if the day is Saturday, the pump is turned on at the bottom of each
// hour for ten minutes.
//
// 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 log_to_file(struct tm *p_dt, int state);

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

#define ON !0
#define OFF 0

FILE *f;

int main(void)
{
     time_t current_seconds;
     struct tm *p_dt;
     
     char day_names[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
     int pump_state;                             
     
     pump_state = OFF;
     
     f = fopen("c:\\data.dat", "a+t");
     fclose(f);
                                
     while(1)
     {
             time(¤t_seconds);
             p_dt = localtime(¤t_seconds);
             display_wday(p_dt);
             display_date_time(p_dt);
             delay_ms(10000);
             
             if (p_dt->tm_wday != 6) // not Saturday
             {
                  if ((p_dt->tm_min >= 0) && (p_dt->tm_min < 5))
                  {
                      if (pump_state!= ON)
                      {
                          pump_state = ON;
                          printf("ON\n");
                          log_to_file(p_dt, pump_state);
                      }               
                  }   
                  else
                  {
                      if (pump_state != OFF)
                      {
                           pump_state = OFF;
                           printf("OFF\n");
                           log_to_file(p_dt, pump_state);
                       }                   
                  }                               
              }
              else // its Saturday
              {
                  if ((p_dt->tm_min >= 30 ) && (p_dt->tm_min < 40)) // ten minutes
                  {
                      if (pump_state != ON)
                      {
                          pump_state = ON;
                          printf("ON\n");
                          log_to_file(p_dt, pump_state);
                      }     
                  }                               
                  else
                  {
                      if (pump_state != OFF)
                      {
                           pump_state = OFF;
                           printf("OFF\n");
                           log_to_file(p_dt, pump_state);
                      }                            
                  }
              }
     }    
     return(0);
}     

void log_to_file(struct tm *p_dt, int state)
{
     f = fopen("c:\\data.dat", "a+t");
     fprintf(f, "%02d/%02d/%04d\t%02d:%02d:%02d\t%d\n", 
                    (p_dt->tm_mon)+1, p_dt->tm_mday, (p_dt->tm_year)+1900,
                     p_dt->tm_hour, p_dt->tm_min, p_dt->tm_sec, 
                     state);
     fclose(f);
}

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;
   endtime = millis + clock();
   while( endtime > clock() )        ;
}