http://www.politiker-stopp.de/gfx/politiker-stopp-print.png

Benjamin Schieder

HANDLING TIME AND DATE IN CODE CORRECTLY

2017 August 10 | 0 comments

I’m so angry, I could write a blog post! Wait, I can write a blog post! Here goes!

What am I angry about? Pokemon GO raids not working for me the last couple weeks. Why does that make me angry? Because the reason for it being broken is lazy/buggy/stupid programming! Why do I think it’s lazy/buggy/stupid programming? I’m glad you asked!

What happened?

Every time I started a raid, I had to wait around two minutes for it to start only to be greeted by a very generic Error message. That was the first thing making me angry. If you, as a programmer, ever feel like only presenting Error is a valid thing to do, please stop what you’re doing, take your hands away from your computer, go find someone more experienced and willing to help you and ask for help. Seriously. Just presenting Error (or Network Error, Generic Error, Uncaught Error or anything like it) to an end-user is not helpful! Not to the user, not to your First Level Support, not to anyone! Thank you for listening.

Let me show you some examples:

Bad Good
Uncaught error Could not open file /etc/application.conf
Network error Server example.com unreachable
Error Time and date not synchronised
Error LITERALLY ANYTHING ELSE

Now, you might wonder about that Time and date not synchronised error. Why did I add that seemingly very specific error in that list? Because it’s what Pokemon GO developers fucked up! Time and date synchronisation is way down the list of things you can assume to be in sync across any two devices. In fact, even with NTP enabled, there might be discrepancies, depending on the specific client and server implementation of NTP. Some implementations might just warp you back and forth through time. Others might speed up or slow down the system clock. And both behaviours are, well, at least not wrong.

Now, back to Pokemon GO. It literally assumes that the time on your cellphone is in sync with their servers. Rereading the last paragraph, that is a fucking stupid thing to assume! I checked my phones time against several devices, and it was seemingly in sync. Date, hours and minutes were the same across 4 devices. Good enough, right? WRONG! The time was off by 20 seconds, which is enough for Pokemon GO raids to break entirely.

Now, what I assume to have happened in code is something like this:

Date now = new Date();
Date raidStart = getRaidStartDateFromServer();

while (raidStart >= now){
	  sleep 1;
		now = new Date();
} 

Now, don’t get me wrong, this is overly simplified. And also horribly broken. And also horribly easy to fix! Consider the following, unbroken code:

Date now = new Date();
Date serverTime = getTimeFromServer(); 
Integer timeDifference = serverTime - now;
Date raidStart = getRaidStartDateFromServer();

while (raidStart >= now + timeDifference){
	  sleep 1;
	  now = new Date();
} 

Notice how a few lines of code make the whole timesync problems an entire non-issue? This still assumes that a second on the server is equal to a second on your cellphone, but that can easily be rectified by querying the server afterwards if it’s raidStart time now and sleeping another few seconds if not.

It could be so easy if done correctly, damn it!

EOF

Category: blog

Tags: PokemonGO Date Time Code


Post a comment

All comments are held for moderation; basic HTML formatting is accepted.

Name: (required)
E-mail: (required, not published)
Website: (optional)
Comment: