Snowflakes, timestamps, and MediaWiki

Published on .


Recently, I was left unsatisfied by the traditional {{cite web}} template when citing a tweet in the Protagonist Object Show wiki I’m working on.

{{cite web
| url = https://twitter.com/pixequil/status/1763287703813013644
| author = Satomi Hinatsu
| username = pixequil
| date = 2024-02-29
| title = Protagonist Object Show
}}

This is inconvenient! The username occurs once in the URL and once again as an argument. I have to find the date manually, and it’ll be biased to my time zone.

Converting snowflakes to Unix timestamps

A tweet is identified by a snowflake, a special 63-bit integer. The relevant portion is the high 41 bits, which represent the milliseconds since the Twitter epoch.

We first isolate the timestamp from the snowflake by performing a right shift by 22 bits, equivalent to dividing by 222 or 4,194,304.

We then convert to seconds by dividing by 1,000.

Finally, we convert to a Unix timestamp by adding the seconds between the Unix epoch and the Twitter epoch: 1,288,834,974.

Thus, assuming integer division, we can calculate the Unix timestamp of a snowflake with the following expression: snowflake / 4194304000 + 1288834974.

MediaWiki parser functions

The #explode function splits a string at each occurrence of a delimiter and extracts the part at a given index.

Let’s use my URL from the example. After splitting it by /, we need to choose which index to select.

Segment https: (empty) twitter.com pixequil status 1763287703813013644
Positive index 0 1 2 3 4 5
Negative index -6 -5 -4 -3 -2 -1

Let’s go with index -1. Assuming we take a {{{url}}} parameter, that’s:

{{#explode: {{{url}}} | / | -1 }}

Then, the #expr function can calculate the timestamp using the expression we found. Note that #expr uses floating-point arithmetic, so we must convert the quotient to an integer with trunc().

{{#expr: trunc({{#explode:{{{url}}}|/|-1}} / 4194304000) + 1288834974 }}

This evaluates to “1709235523”.

This {{cite web}} uses the #time function for date formatting, so it displays “2024-02-29” as “29 Feb 2026”. We can make use of its support for Unix timestamps. {{#time:@1709235523}} also displays “29 Feb 2024”!

The final template

We extract the username with {{#explode:{{{url}}}|/|-3}} to get the final {{cite Twitter}} template.

{{cite web
| url = {{{url}}}
| author = {{{author}}}
| username = {{#explode:{{{url}}}|/|-3}}
| date = @{{#expr:trunc({{#explode:{{{url}}}|/|-1}}/4194304000)+1288834974}}
| title = {{{title}}}
}}

The example looks much prettier!

{{cite Twitter
| url = https://twitter.com/pixequil/status/1763287703813013644
| author = Satomi Hinatsu
| title = Protagonist Object Show
}}

Citing a Discord message

Like a tweet’s URL, the last segment of a Discord message URL is its ID. Discord also uses an identical snowflake format; the only difference is their epoch is January 1, 2015, or 1,420,070,400 seconds since the Unix epoch.

sona pona’s own {{cite Discord}} template inspired me to write this article.