Why Twitter was down for 5 hours. It’s all about calendars.

Standard

Twitter was down for many users this Monday, between midnight and 5 AM, CET. Android app users were logged out and the API was not working during a few hours. A bug in a line of code caused the service to think that it was 29 December, 2015.

Gregorian VS ISO calendars

The ISO week numbering system uses the YYYY format for the year instead of the Gregorian calendar’s yyyy. It then looks at which week of the year it is, and then uses a date digit with 1 starting on Monday. So, for example, Tuesday of the 50th week of 2014 would have been 2014-W50-2 in ISO week format. The problem comes in when January 1 of the new year ends up falling on a date that doesn’t get along well with the ISO week format. The first day of 2015 will start on a Thursday, whereas the ISO standard expects the first week of the year to start on “the Monday that contains the first Thursday in January.” In 2014-2015, that would be January 1st, 2015. That’s why Twitter believed that we were in 2015 on Monday, because “the first Thursday in January” is in fact this week.

Afterwards, some parts of the system believed we were almost a year later, while other were perfectly aware that we were still in 2014. Users were not able to log in (and were logged out) because tokens have a limited lifetime and therefore are not valid a year later.

Dates are hard to handle

Dates are really hard to handle when programming, and it’s very easy to make a mistake without breaking a thing until the day it will eventually explode everything because calendars or timezones are different. The same kind of mistake happened two years ago with the “Do Not Disturb” feature on iPhones, and it isn’t the last time it will happen for sure.

Using JavaScript / jQuery to detect if someone is using AdBlock

Standard

Let’s face it: ads are not cool. I know, but sometimes you have no other choice than to use them to pay your server / your coffee / whatever. And if you are using ads, you know that some people will not see your ads because they are using a plugin that blocks ads on your website. You want to reach these visitors and just say

“Hey, we know ads are not cool, but please make an exception for us or give us a small amount of money, just to support our work”

On Teen Quotes I am showing a friendly message in place of the only ad to say what I have to say. Here is what people with an ad blocker are seeing:
adblock

But let’s get back to the point: detect visitors that are using an ad blocker with JavaScript.

Some code

Let’s write some code. You’re going to write the most simple JS file you have ever written. Ready? Here it is:

var isNaughtyVisitor = false;

BOOM! That was fast, isn’t it? In fact, we don’t care about the variable name or its value, we just want to define a new variable. But this variable needs to be defined in a file called ads.js that you will include in your HTML. Why so? This is were it gets funny. Ad blockers are a little dumb, so when they see a file named something like ad.js or ads.js, they will block the request. And if the request was blocked… your variable will not be defined!

The second step is just to test in your regular JS file of your application if the variable was defined. This file needs to be include after the previous file, ads.js. You can add something like this:

$(document).ready(function() {
    // The div that will add to the DOM if the visitor is 
    // using an ad blocker
    var div = 'Your amazing HTML block here';
    
    // The script was never called, probably using an ad blocker
    if (typeof(isNaughtyVisitor) == "undefined") {
        // Insert the div wherever you want
        $("#footer").before(div);
        // Send the event to Google Analytics if you want to track
        ga('send', 'event', 'ads', 'hidden');
    // A friendly user!
    } else {
        // Send the event to Google Analytics if you want to track
        ga('send', 'event', 'ads', 'displayed');
    }
});

Pretty darn simple, and really effective. The downside is that friendly users will have to make an extra HTTP request. Yep, I know, it’s not fair for them.