Home > Blockchain >  Checking if new day
Checking if new day

Time:03-04

I'm not quite sure how to describe the problem programmatically but I have a server and I don't want to run a separate thread/loop to check for a new day but essentially any time a user connects to the server it runs a check to see if it's a new day meaning past 11:59pm on local time. And if so, the server should perform a single operation but only that one time until a new day starts again. And I'm not sure how to go about doing this. My first issue would be how would I check if it's past 11:59 on any given day. And my second question is how do I make sure whatever operation I'm performing is only performed once until the next day?

I thought about just keeping a variable look bool and operating on that

pseudo

if(time>11:59){
 if(!newday){
  //do single operation 
  newday = true;
 }
}

But this won't work simply because every minute past 11:59 will be greater than and newday will never be reset back to false..

I don't know if I'm overthinking a very simple concept or I just don't understand what I'm doing. Should I not use a specific time to check whether a new day has begun? Should I run on a 24 hour basis rather than 12?

CodePudding user response:

You can probably just check their local time in 24 hour format and subtract their current time from 24 hours like eg. 24-16.12, convert the value into milliseconds then use the sleep() function on linux(unistd.h) or Sleep() function on windows(windows.h) and pass the value you get from subtracting to the sleep()/Sleep() function's parameter. Then you know that its a new day, do what you want right now, so then next line make an infinite while loop(dont worry, since sleep() is in the next line there is no risk), but in the first line put sleep(86160000) or Sleep(86160000) since 86160000 milliseconds = 23 hours and 58 mins(1 day precisely) and in the following lines, do what you want to do when its a newday. If you encounter any errors post it in the comment

CodePudding user response:

you need one new thread,it doesn't take up a lot of resources. using a 24 hour basis to record,but you can show 12h basis.

  • Related