HI, I am working a program for an Aquarium which contains controls for lights and a electronic fish feeder. I have found a way to get the system time but I do not have the desired result. [1] I want to be able to set the system time during run time and have the time run during run time ( my program shows the current time and I can't set the time during run time).
[2] I am trying to implement a timer that will check itself against the system clock within my fish timer and light timer functions. Help will be greatly appreciated, Thanks

#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
 
using namespace std;
 
class ControlPanel {
 
public:
ControlPanel();
~ControlPanel() {};
void SetFeedFish(bool);
bool GetFeedFish();
void SetLightSwitch(bool);
bool GetLightSwitch();
void Time();
void FishTimer();
void LightTimer();
 
private:
 
bool FeedFish;
bool LightSwitch;
 
};
 
ControlPanel::ControlPanel() 
{
 
FeedFish = false;
LightSwitch = false;
 
}
 
void ControlPanel::FishTimer()  // Not sure how to implement input 
{
 
}
 
 
/*
void ControlPanel::LightTimer() //Ditto
{
 
}
*/
 
 
void ControlPanel::Time()      // Returns Time of PC clock
{ 
 
 
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"Now is %I:%M:%S%p.",timeinfo);
puts (buffer);
 
 
}
 
void ControlPanel:SetFeedFish( bool AllFish) 
{
FeedFish = AllFish;
} 
 
 
 
bool ControlPanel::GetFeedFish()
{
return FeedFish;
}
 
void ControlPanel:SetLightSwitch(bool AllLights)    
{
LightSwitch = AllLights;
}
 
bool ControlPanel::GetLightSwitch() 
{
return LightSwitch;
}

win32 api SetSystemTime()

One problem with your program is that many computers automatically synchronize the system time with something else, such as a network computer or a satellite. In that case whatever you set the computer to will eventually be changed.

>> can't set the time during run time
why not? Its just a function that can be called anytime you want to.

>> can't set the time during run time
why not? Its just a function that can be called anytime you want to.

What if I created a function that has the parameters hours and minutes, and lets say I had a prompt saying: What is the hour etc. Whcih will then give the variable hour the new initialization. Then I called my SetTime function via a prompt menu. would that work, if not what options do I have to let the end user set the time.

Of course it will work -- just fill in the SYSTEMTIME structure with whatever the user entered and call SetSystemTime(). I would first call GetSystemTime() to get current day, month and year, then change the other member values to whatever you want before calling SetSytemTime(). Note that SetSystemTime() expects GMT, which depends on the time zone. There are other win32 api functions you can call to get the computer's time zone setting. If your time zone is CST (USA Central Standard Time), you have to subtract 6 from the local time that the user enters.

Call SetSystemTimeAdjustment() to disable auto periodic time adjustments from outside sources because it will screw up your program.

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.