From b9657ca4948fa62b50ce119a5a383c82ae3a5e1e Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Mon, 26 Nov 2018 10:55:10 +0100
Subject: [PATCH] Bugfix: Now handling JSON dates also in Safari

---
 VIPSWeb/static/js/frontpageMap.js |  1 +
 VIPSWeb/static/js/util.js         | 27 ++++++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/VIPSWeb/static/js/frontpageMap.js b/VIPSWeb/static/js/frontpageMap.js
index ce0eda28..87c021cf 100755
--- a/VIPSWeb/static/js/frontpageMap.js
+++ b/VIPSWeb/static/js/frontpageMap.js
@@ -64,6 +64,7 @@ var currentDateInMillis;
  */
 function initFrontpageMap(lonLat, zoomLevel, mapAttribution)
 {
+	
 	if(settings.vipslogicServerName === undefined)
 	{
 		alert(gettext("Source hostname not defined."));
diff --git a/VIPSWeb/static/js/util.js b/VIPSWeb/static/js/util.js
index 01db3feb..a0c208de 100755
--- a/VIPSWeb/static/js/util.js
+++ b/VIPSWeb/static/js/util.js
@@ -343,9 +343,21 @@ function isDict(v){
 	return typeof v==='object' && v!==null && !(v instanceof Array) && !(v instanceof Date);
 }
 
+/**
+ * Unfortunately, this method depends on momentjs, since date parsing directly in JavaScript is...hard
+ */
 function getUnixTimestampFromJSON(ambiguousValue)
 {
-	var possibleDateObject = new Date(ambiguousValue);
+	var possibleDateObject;
+	if(!isMomentJSAvailable())
+	{
+		possibleDateObject = new Date(ambiguousValue);
+		console.info("Warning: Parsing date without MomentJS. Can't guarantee correct result.");
+	}
+	else
+	{
+		possibleDateObject = moment(ambiguousValue).toDate();
+	}
 	if(possibleDateObject.getTime() === NaN && typeof parseInt(ambiguousValue) === "number")
 	{
 		return parseInt(ambiguousValue);
@@ -355,3 +367,16 @@ function getUnixTimestampFromJSON(ambiguousValue)
 		return possibleDateObject.getTime();
 	}
 }
+
+function isMomentJSAvailable()
+{
+	try {
+		moment();
+		return true;
+	}
+	catch (err)
+	{
+		return false;
+	}
+
+}
-- 
GitLab