- Home /
variable increase based on passing time using server
Hey guys,
Need some help.
Here's what I have:
My server is set, accessing and splitting the string is all good for date and time. Here's my problem.
I am successfully saving the end time of the app as well as the new start time. I'm trying to find the difference between the two times to decide how much to reward the payer.
Here's the problem, no matter how I try the math I will always have the problem that at midnight the number will go negative. here's an example
at 12:05:59 we close, cool
at 01:05:34 we start (it actually reads 13:05:34) still good since the second number is obviously larger than the first.
The problem is if i have any time before midnight that I close the game and then open it after that
So close game at 23:59:59 then open at 08:15:59
even those roughly 8 hours has passed the equation will throw a negative instead of a positive. The problem occurs even if i incoporate the date just in different ways
Answer by Bunny83 · Apr 13, 2019 at 05:24 PM
Well, you ignored the days that have passed completely. Since you use your custom time from your server it's not clear what you actually receive from your server so we can't suggest a fix. However you generally have to include the date as well as the time (which in computer science is generally called a DateTime value). When you include the date in your calculations the issue should go away. Actually the DateTime struct already can calculate time differences. If you have your date / time values as seperate values you can simply initialize a DateTime value with them.
Keep in mind that using an external time server is pretty pointless if you don't use https with an actual valid certificate. Otherwise it's equally simple to spoof the time you get from your server. An additional symmetric encryption and checksum would help as well, though that's up to you.
well, no, i did mention that i use the date as well in the last line of my question and the problem persists regardless. but i was under the impression that using DateTime value is a localized occurence which would allow the user to simply forward their phones time for rewards.
the server does use the https there is a valid cert so it looks like i have the correct start there at least
my actual code gets this format of information from the server $$anonymous$$$$anonymous$$ - DD - YYYY / HH : $$anonymous$$$$anonymous$$ : SS as a string which i know how to parse to an int if necessary (and i've tried)
can you give me a starting point of code on how to use what i've got? I have gone to your links tho, they really helped conceptually what I'm doing wrong but I'm a bit at a loss on where to start a calc method
thanks either way this is really helpful and should get me moving further.
I just read your comment. The format you use to send the date from your server is way too complicated for computer processing. Why did you choose such a format? It makes things much easier to either use a single seperation character and just split the string on that, or just use a more standardized format like json. A format like this would make much more sense:
yyyy/$$anonymous$$$$anonymous$$/dd/HH/mm/ss
Note that i used the default C# date format string specifiers here. "$$anonymous$$$$anonymous$$" is the two digits month while "mm" are the two digit $$anonymous$$utes. Though for computer processing a format like this would be simpler
yyyy/$$anonymous$$/d/H/m/s
as you don't need any leading zeros which could sometimes create issues. The current date/time would be
2019/4/15/19/5/30
yeah, that makes sense. I'm brand new to using a server and accessing it, but it makes sense that my starting point is too complex I'll simplify it and see if that makes further progress easier.
I'm unfamiliar with JSON, i've tried learning, any good starting tutorials for JSON?
overall your input has been super useful on getting me to some good starting points.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Master Server Requirements? 2 Answers
Faking Dedicated Servers in Unity 1 Answer
time synchronisation 2 Answers