Skip to content

Commit f3accf4

Browse files
dschorimrul
andcommitted
Downloads (MacOS): show relative date
This is yet another thing that was computed server-side but can be computed on the client side instead. The logic for the approximate date difference was inspired by https://github.com/rails/rails/blob/4136145819ab/actionview/lib/action_view/helpers/date_helper.rb#L29-L158 (because this was used indirectly in the Rails app, via the `time_ago_in_words` function). However, rather than reimplementing all of that complicated logic, let's implement a vastly simpler yet still similar logic that imitates how that Rails function handles year-long differencess: up to 25% longer a time difference and we say "about X ago", up to 25% shorter and we say "almost X ago", otherwise we say "over X ago". Co-authored-by: Matthias Aßhauer <[email protected]> Signed-off-by: Matthias Aßhauer <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 129b879 commit f3accf4

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

content/downloads/mac.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h3>Xcode</h3>
2828
<h3>Binary installer</h3>
2929
<p>Tim Harper provides an <a href="https://sourceforge.net/projects/git-osx-installer/">installer</a>
3030
for Git. The latest version is <a href="{{< site-param macos_installer.url >}}">{{< site-param macos_installer.version >}}</a>,
31-
which was released on {{< site-param macos_installer.release_date >}}.
31+
which was released <span id="relative-release-date"></span>on <span id="auto-download-date">{{< site-param macos_installer.release_date >}}</span>.
3232

3333
<h3>Building from Source</h3>
3434
<p>If you prefer to build from source, you can find tarballs

static/js/application.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ var Downloads = {
248248
Downloads.observeGUIOSFilter();
249249
Downloads.observePopState();
250250
Downloads.filterGUIS();
251+
Downloads.postProcessDownloadPage();
251252
},
252253

253254
getOSFromQueryString: function() {
@@ -314,7 +315,35 @@ var Downloads = {
314315
onPopState(function() {
315316
Downloads.filterGUIS();
316317
});
317-
}
318+
},
319+
320+
// say how many days ago this version was released
321+
postProcessReleaseDate: function(index, releaseDateString) {
322+
const daysAgo = Math.floor((Date.now() - Date.parse($('#auto-download-date').html())) / 86400000);
323+
if (daysAgo < 0) return releaseDateString; // leave unparseable content alone
324+
325+
const rest = (count, unit) => `${count} ${unit}${count > 1 ? "s" : ""} ago`;
326+
let ago = rest(daysAgo, "day");
327+
328+
const handwave = (exact, unit) => {
329+
const roundedDown = Math.floor(exact);
330+
const fract = exact - roundedDown;
331+
if (fract < 0.25) return `about ${rest(roundedDown, unit)}`;
332+
if (fract < 0.75) return `over ${rest(roundedDown, unit)}`;
333+
return `almost ${rest(roundedDown + 1, unit)}`;
334+
}
335+
336+
if (daysAgo == 0) ago = "today";
337+
else if (daysAgo == 1) ago = "yesterday";
338+
// from here on out, we keep it only approximately exact
339+
else if (daysAgo > 365 * 0.75) ago = handwave(daysAgo / 365.25, "year");
340+
else if (daysAgo > 45) ago = handwave(daysAgo / 30.4, "month");
341+
return `<strong>${ago}</strong>, `;
342+
},
343+
344+
postProcessDownloadPage: function() {
345+
$('#relative-release-date').html(Downloads.postProcessReleaseDate);
346+
},
318347
}
319348

320349
// Scroll to Top

0 commit comments

Comments
 (0)