1

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?

Kayla
  • 223
  • 2
  • 14
  • `newday will never be reset back to false`? Then set it to false... Seriously I can't understand what you are trying to do. – Yves Mar 04 '22 at 01:58
  • A Boolean stores two distinct values: true and false. You want to store a date that something happened (in order to check whether the current date is different). A date is never going to fit inside a Boolean, due to the simple fact that there are more than two days. – Silvio Mayolo Mar 04 '22 at 02:00
  • I'd turn the time into a [`tm` struct](https://en.cppreference.com/w/c/chrono/tm) and see if the `tm_yday` member has changed since the last time I looked. Wind up with something like `if (newtime.tm_yday != last_day) { last_day = newtime.tm_yday; /* do stuff */ }` – user4581301 Mar 04 '22 at 02:01
  • If you go this route make sure you operate on a monotonic clock so that time can't jump backward and fool the test. – user4581301 Mar 04 '22 at 02:03
  • @Yves The problem is when do I reset it back to false, that's what I'm confused about .. – Kayla Mar 04 '22 at 02:05
  • Mind you, that's the C programmer in me talking. Howard Hinnant (and friends) probably have a neat trick buried in [the Date library](https://howardhinnant.github.io/date/date.html). Built into C++, more or less, as of C++20. – user4581301 Mar 04 '22 at 02:05
  • @SilvioMayolo I'm not trying to save a date in a boolean, I just need to be able to perform a single operation once its past 11:59 the problem is, every minute past 11:59 is well..past 11:59.. – Kayla Mar 04 '22 at 02:06
  • @user4581301 That does seem like I could go down that solution. So I'd store the last day and just compare it to the new day? – Kayla Mar 04 '22 at 02:06
  • Yes. and if the `/* do stuff */` sets the `newday` flag, an `else` would be used to clear the flag. But definitely [look into `chrono`](https://en.cppreference.com/w/cpp/chrono) it's generally more tractable than the old C library. – user4581301 Mar 04 '22 at 02:10
  • @user4581301 But what about when it's the first day of a new month it wouldn't be greater than the last day would it? I could check if month and day is greater but then what about a new year, would I have to check if day is greater than yesterday and month is greater than last month and year is greater than last year? – Kayla Mar 04 '22 at 02:12
  • @Kayla `if (newday == true) { newday = false; }`? – Yves Mar 04 '22 at 02:13
  • @Yves maybe I'm just not explaining the problem clearly enough. But it's more to do with checking for a new day the boolean was just so the operation wouldn't be repeated – Kayla Mar 04 '22 at 02:16
  • Here's a question answered by Mr Hinnant himself [outlining how to pick off if a timestamp is in the weekend](https://stackoverflow.com/questions/52776999/c-chrono-determine-whether-day-is-a-weekend). If it can do that so easily, detecting a new day shouldn't be hard. [Here's another question that covers a number of manipulation tricks](https://stackoverflow.com/questions/15957805/extract-year-month-day-etc-from-stdchronotime-point-in-c) – user4581301 Mar 04 '22 at 02:17
  • Don't use a bool, use the day of the week (for example 0=Sun, 6=Sat). Then, if today is Tuesday (day 2) and it is after 11:59 you can increment the day number to 3 and do your processing. If you get to 4am and someone connects and it is still day 2 and you haven't done it yet so then you can increment the day number to 3 and do your processing. If it is already incremented you know the processing has already been done. That way it will work as long as someone connects at least once every 24 hours. – Jerry Jeremiah Mar 04 '22 at 02:18
  • @JerryJeremiah I like the day of the week idea. But that's also assuming someone will connect at least once in those 24 hours. If they don't , would the whole thing break? – Kayla Mar 04 '22 at 02:21
  • If you want robust code, you'll find a way to check daily whether someone logs in or not. If you can be certain that time only goes forward you don't care if it's greater or less than, just that it's not equal. If not, you have to get smarter. If the new day is less than the last day, check to see if it plus the period (week works well here because the period is always 7) would be ne next day. If it is, you're in the next day. If not, time probably went backward on you and you could ignore the update or get really crafty. Nonmonotonic time is a . – user4581301 Mar 04 '22 at 02:26
  • Well, it won't break but your new day processing won't happen that day - do you need the new day thing to happen at least once per day or only on days that people connect? It does rely on someone connecting at least every 6 days. If someone connects at noon on Sunday and the day is 0 then if they connect next Saturday at 11:59 the day will still be 0 and the scheme will think that the new day processing has already been done - but it hasn't. To get around that use the Julian date instead of the day of week because then it is day of year instead of day of week. . – Jerry Jeremiah Mar 04 '22 at 02:28
  • @JerryJeremiah What if the current day is say Monday which is day 1, then no one connects to the server for an entire week, until the next monday. It's day 1 again, now if I check if current day of week is greater than the previous, the code will break because 1 is not greater than 1.. But the only way I can get around that is if I also check week, then I also check month, then I also check year – Kayla Mar 04 '22 at 02:30
  • Lol I feel like an idiot because this seems like it should be easy to solve but I'm just not gettin git – Kayla Mar 04 '22 at 02:32
  • @user4581301 I think I might have a solution. The first time the server runs, I store the current time in miliseconds in a file. Then I can use that file for all my time based calculations? So if the server restarts, I just read time from file, get current milisecond time, subtract, convert to hours, and judge days based on how many hours? So rather than using 11:59 as a new day, use whatever time the server first launched as a new day time – Kayla Mar 04 '22 at 02:43

2 Answers2

-1

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

  • This is fine if you're assuming the server will never shut off. There are times where a machine needs to be restarted or mantanence/updates needs to happen. And will throw off the cycle – Kayla Mar 04 '22 at 03:57
  • you can also run it locally, why waste money on servers? – The Inventor Mar 04 '22 at 03:59
  • actually it would be more reliable and no money wasted method to do it locally, since it will take the user's local time and run independently over there, if the device is off then they cant use the device itself, and can use it only when its on – The Inventor Mar 04 '22 at 04:03
  • The server isn't just for checking a new day.. it's doing a lot more work internally, that's just one part of it.. – Kayla Mar 04 '22 at 04:05
  • then you can split this part of checking a newday, to be local – The Inventor Mar 04 '22 at 04:09
  • If its local then the client can manipulate it, and the server relies on it. It's critical that this is calculated by the server not the client. – Kayla Mar 04 '22 at 04:13
  • whats the issue if they manipulate it? – The Inventor Mar 04 '22 at 04:22
-3

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.

gogogo
  • 1
  • 2
  • 1
    you also could try to use signal and slot to check it at a certain time – gogogo Mar 04 '22 at 02:03
  • I could create a new thread but I was just thinking it's a bad idea to waste resource on a thread that's going to be idle majority of the time because I would only need to execute the thread once per minute or so just to check – Kayla Mar 04 '22 at 02:07
  • It could be worse than you think Kayla. If you have multiple threads operating on `newday` you pick up overhead protecting it from simultaneous access. – user4581301 Mar 04 '22 at 02:15
  • Have you considered the consequences of creating threads every minute? If you ensure there is no problem, you can do that, but what is the reason why you create threads every minute and how to create threads every minute. But usually using one thread is not a waste of time, because you don't have a lot of calculation. You only need to sleep for 60 seconds, check the system time or Internet time, and then execute other code – gogogo Mar 04 '22 at 02:18
  • But creating a new thread doesn't solve my problem.. Even if I did and kept it idle for 60 seconds and check time I stil would need to validate if its a new day. The thread isn't what I'm having an issue with it's more so how do I check for a new day – Kayla Mar 04 '22 at 02:20
  • please tellme, do you use c++, i want give you codes about – gogogo Mar 04 '22 at 02:25
  • is there no system api? – gogogo Mar 04 '22 at 02:27
  • Yes I'm writing this in C++ – Kayla Mar 04 '22 at 02:28
  • use time api to get time string( y / m / d / h / m / s ), check time, if day string equal to 00:00:xx? if i meet this question, i will use api to compare string to get my answer... is there another idea you have? ToT – gogogo Mar 04 '22 at 02:45