I'm using an Adafruit Feather HUZZAH ESP8266 controller for a wordclock, which is a birthday present and should also be a marriage proposal.
Last time I programmed was years ago in school, so I'm rather noobish to that.
My plan is to make a simple condition if the desired time is reached (its only once), then light up some LEDs (marry me and her name) in heartform.
To not be overwritten a minute later i think an infinite while loop would be best. Push Reset and the clock gets time again over NTP.
So I searched for correct unix timestamp and tried things like
#include <iostream>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
int main(void)
{
while (time()==1461337750)
or
time_t t;
t = time(NULL); //oder time(&t);
if (t==1461335820)
}
but it didn't show anything.
I don't know what is the right way and need help, please! I searched the Inet for hours but couldn't find a similar task or I thought it wasn't.
Related
Basically, I want to create a program that will check the month, the day and the year and will execute code if both the month and the day criteria is met.
For example, let's say the date was July 8th, 2016.
Let's say I had some code that simply wanted the program to output "Hello world!" on this date.
I would want this code to execute on July 8, 2016 and no other date. How would I go about this?
To run your program at a certain time, you have to rely on external tools such as cron or the Windows task scheduler. A program cannot run itself if it's not already running :-)
If your code is running and you just want it to delay action until some specific time, that's what all the stuff in the ctime header is for.
You can use time() and localtime() to get your local time into a struct tm, then examine the fields to check if some specific time is current. If so, do your action. If not, loop around and try again (with a suitable delay if needed).
By way of example, here's a program that outputs the time but only on five-second boundaries:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main() {
time_t now;
struct tm *tstr;
// Ensure first one is printed.
int lastSec = -99;
// Loop until time call fails, hopefully forever.
while ((now = time(0)) != (time_t)-1) {
// Get the local time into a structire.
tstr = localtime(&now);
// Print, store seconds if changed and multiple of five.
if ((lastSec != tstr->tm_sec) && ((tstr->tm_sec % 5) == 0)) {
cout << asctime(tstr);
lastSec = tstr->tm_sec;
}
}
return 0;
}
I would use std::this_thread::sleep_until(time_to_execute); where time_to_execute is a std::chrono::system_clock::time_point.
Now the question becomes: How do you set the system_clock::time_point to the correct value?
Here is a free, open-source library for easily setting a system_clock::time_point to a specific date. Using it would look like:
using namespace date;
std::this_thread::sleep_until(sys_days{jul/8/2016});
This would trigger at 2016-07-08 00:00:00 UTC. If you would rather trigger based on your local time, or some arbitrary time zone, here is a companion library to accomplish that.
You can also drop down to the C API and set a std::tm's field values, convert that to a time_t and then convert that to a system_clock::time_point. It is uglier, more error prone, and doesn't require a 3rd party library.
So I need to do some actions if the software has not been opened on the same day my plan is to store the number of days since the epoch for today's date in my database. Then when the software opens it will pull this number of days since the last epoch from the DB and then check that with today's number of days since the last epoch. Thus I will know if this is the same day or not.
I'm very inexperienced with C++ and I've been looking at the DateTime functions from "stdafx.h" however I've not been able to get the number of days since the last epoch.
So my goal in doing this would be to clear a database if this is the first time the software has been opened today. If it has already been opened today, then just continue on and do nothing.
I do not know how to go about this in C++. If it was python or C# or java I would have no trouble with this. So how would I go about this in C++?
Using the <chrono> standard header, it's quite easy to get the number of days since the clock's epoch.
For example:
#include <chrono>
#include <iostream>
int main() {
using days = std::chrono::duration<int, std::ratio<24 * 60 * 60>>;
auto days_since_epoch = std::chrono::duration_cast<days>(std::chrono::system_clock::now().time_since_epoch());
std::cout << days_since_epoch.count() << "\n";
}
I think the implementation is free to use whatever epoch, but if you only care about the difference in the number of days of successive calls, you should be fine.
Note that since we're essentially rounding down to the beginning of they day,
if your user opens your application once at 23:59 and then again one minute later, you'll perceive a calendar day to have passed.
+1 to melak47's answer. I just wanted to add (and this wouldn't fit in a comment), that his answer assumes the day changes in the UTC timezone, which might just be fine. This is a very good and efficient answer for many applications, including stackoverflow itself!
But in case you want the day change to happen at local midnight, here is a library that can help you do that, while still sticking with the nice <chrono> facilities. Indeed this is just a very minor tweak to melak47's answer:
#include "tz.h"
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::chrono;
using namespace date;
auto tp = system_clock::now();
auto lp = current_zone()->to_local(tp);
auto days_since_epoch = duration_cast<days>(lp.time_since_epoch());
std::cout << days_since_epoch.count() << "\n";
}
It will give exactly the same answer as melak47's answer, except when the machine is set to a local timezone that differs from UTC, and UTC is currently already into the next day, or still on the previous day, compared to the local timezone.
For my program I am making pac-man. Every time the pac-man eats a dot I want to play the munching sound. I already know how to play the sound but it causes lag too much lag. The lag is about 0.5 seconds but overall it slows down my game a bunch. Any suggestions?
#include <iostream>
#include <windows.h>
#pragma comment (lib , "winmm.lib") // Used for sound
using namespace std;
int main()
{
PlaySound(TEXT("Sounds\\pacman chomp.wav"), NULL, SND_FILENAME|SND_ASYNC);
system("pause");
}
If you want real "twitch time" audio you should probably look at DirectSound, that's what it was created for. The waveOutXXX APIs are better than PlaySound, but DirectSound is better still.
I'm working on a project where I'm to time the creation of fork() and pthread_create(). We're supposed to time how long it takes to do each of these tasks by using system calls to create a personalized timer class. For assistance, the professor has told us to check man -k time.
I'm new to both using system calls and using man pages for documentation, so I'm entirely lost. So far the code I'm trying to get working is this:
#include "Timer.h"
#include <iostream>
#include <signal.h>
#include <ctime>
#include <time.h>
using namespace std;
Timer::Timer() {
timer_t * tid;
timer_create(CLOCK_REALTIME, NULL, tid);
int time = timer_gettime(tid, 0);
cout<<time;
}
When compiling, eclipse tosses me these errors:
undefined reference to 'timer_create', line 20
undefined reference to 'timer_gettime', line 21
The internet has pointed me in the direction of including the -lrt library when compiling, but I can't find anything that says how to do that, suggesting that perhaps it's entirely wrong.
So, am I on the right path? If I am, how am I supposed to get this code working?
Edit:
I got the clock() based timer working. The downside is that we're timing fork(), which isn't registering as taking any time at all when timing it. Any ideas?
You can use this
time_t start,end;
start=clock();//predefined function in c
//after the user defined function does its work
end=clock();
t=(end-start)/CLOCKS_PER_SEC;
I was just toying around with making a basic chess engine. I was able to get a lot of advice from http://web.archive.org/web/20070704121716/http://www.brucemo.com/compchess/programming/alphabeta.htm, but the real site is down and not all the pages are archived. (Anyone know where to find a full version of Bruce's site?)
But now to the real question: how do I communicate with XBoard? I understand it is via stdin and stdout, but I've been having problems in code. Basically, to get started, I just want to
receive input from XBoard and print it to the console/screen
Give a move of hard-coded input to XBoard and have it make the move
program utility functions and have a random chess ai which chooses random moves.
After that, I can start implementing real things like alpha-beta searching.
I am stuck on the first two things right now. Here is some code I have tried to write/borrowed.
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define STR_BUFF 256
using namespace std;
int main (int argc, const char * argv[])
{
char input[STR_BUFF];
char output[STR_BUFF];
while(true){
fflush(stdout);
// read input
if (!fgets(input, STR_BUFF, stdin)){
printf("terminated");
return 0;;
}
printf("%s", input);
}
return 0;
}
I am just getting back into C after about 6 months break and this is the first project that I have used stdin/stdout pipelines to communicate with another program (minus a few basic programs) so I would appreciate any help and any explanations. I know programming a chess engine is a herculean task, but I have already programmed the rules of chess before and what I can find of Bruce's site is absolutely amazing.
You are doing it almost right: get a command from XBoard with fgets, then report a move with printf and fflush. (One thing is wrong, though: you don't need to 'print the command to the console/screen'; you are not communicating with the console/screen; you only read commands from XBoard and send moves back to XBoard).
Probably, it would be easier to start with some existing code. Try to read sources for GNU Chess. Or download sources for any other chess engine, supporting XBoard protocol.
And here is other question with lots of information on chess engine programming: "What are some good resources for writing a chess engine?".
I think that you're looking for pipe(), included in unistd.h. Take a look at Can popen() make bidirectional pipes like pipe() + fork()? for notes on implementation.