]> code.delx.au - comingnext/commitdiff
fixed 24/12 time format detection
authorMichael Prager <mail@michaelprager.de>
Sun, 15 Jan 2012 20:45:08 +0000 (21:45 +0100)
committerMichael Prager <mail@michaelprager.de>
Mon, 16 Jan 2012 00:32:01 +0000 (01:32 +0100)
comingNext/index.html

index 8241fe9ee7cb92a2fd32494261eff2105e7bc23b..7a867235ee33683bb982e9ef151c2a84bf533c7d 100644 (file)
@@ -107,6 +107,7 @@ var errorOccured = false;
 var entryLists = null; // stores all fetched calendar entries until data is refreshed\r
 var statupSuccessful = false; // indicates if everything started up wihtout errors. If we detect an error after that, it might just be a temporary problem e.g. by a backup process.\r
 var use12hoursTimeFormat = false; // defines how time should be formated: 19:00 or 07:00 pm\r
+var timeFormatSeparator = ":"; // format time 19:00 or 19.00 depending on system setting\r
 \r
 // vars for daylight saving time\r
 var summertime = false; // true, if current date is in summer, false if in winter\r
@@ -346,6 +347,7 @@ function collectLocales()
                var curWeekday = "";\r
 \r
                while (weekdaylist && (entry = weekdaylist.getNext()) != undefined) {\r
+                       detectTimeFormat(entry.StartTime + '');\r
                        curWeekday = (entry.StartTime + '').split(',')[0];\r
                        log(entry.StartTime + ' -> ' + curWeekday + ' ' + counter2);\r
                        ids[counter + counter2] = entry.id;\r
@@ -373,6 +375,14 @@ function collectLocales()
        }\r
 }\r
 \r
+// detects the system's current time format by parsing a native calendar timestamp (this is the only reliable formating across all devices and firmwares)\r
+function detectTimeFormat(localeTimeString)\r
+{\r
+       localeTimeString = localeTimeString.toLowerCase();\r
+       use12hoursTimeFormat = localeTimeString.indexOf("am") != -1 || localeTimeString.indexOf("pm") != -1 ? true : false;\r
+       timeFormatSeparator = localeTimeString.indexOf(":") != -1 ? ":" : ".";\r
+}\r
+\r
 function requestNotification()\r
 {\r
        var criteria = new Object();\r
@@ -492,10 +502,6 @@ function parseDate(dateString)
                if (dateArr.length == 6 && dateArr[5].toLowerCase() == 'am' && hours == 12) \r
                        hours = 0;\r
                \r
-               // remember if date was formated using 12h time format, we need to use this information later when formating time\r
-               if (dateArr.length == 6 && (dateArr[5].toLowerCase() == 'am' || dateArr[5].toLowerCase() == 'pm'))\r
-                       use12hoursTimeFormat = true;\r
-\r
                result = new Date(year, month - 1, day, hours, minutes, seconds);\r
        }\r
        \r
@@ -521,7 +527,7 @@ function parseDate(dateString)
 \r
 function getWeekdayLocalized(date) {\r
        var localizedString = date.toLocaleDateString();\r
-       if (localizedString.match(/\d\d.\d\d.\d\d/)) {\r
+       if (localizedString.match(/\d\d.\d\d.\d\d(\d\d)?/)) {\r
                return weekdays_translated[date.getDay()];\r
        } else\r
                return localizedString.split(',')[0];\r
@@ -593,15 +599,11 @@ function formatDate(date, format)
 function formatTime(date)\r
 {\r
        // date is a Date() object\r
-       date.setSeconds(0); // we don't care about seconds\r
-       var time = date.toLocaleTimeString().replace(/[\.:]00/, ''); // remove seconds from string\r
-       if (time.replace(/\./, ':').split(':')[0].length < 2)\r
-               time = '0' + time;\r
-\r
-       // workaround for bug introduced by Anna firmwares, which causes Date().toLocaleTimeString() to no longer return time in 12h format even though this has been defined in system settings\r
-       if (use12hoursTimeFormat && time.toLowerCase().indexOf('am') == -1 && time.toLowerCase().indexOf('pm') == -1) { \r
-               var hour = date.getHours();\r
-               var minute = date.getMinutes();\r
+       var hour = date.getHours();\r
+       var minute = date.getMinutes();\r
+       \r
+       // don't use Date().toLocaleTimeString() as it is utterly broken on newer firmwares\r
+       if (use12hoursTimeFormat) {\r
                var ap = "AM";\r
                if (hour > 11)\r
                        ap = "PM";\r
@@ -613,11 +615,19 @@ function formatTime(date)
                        hour = "0" + hour;\r
                if (minute < 10)\r
                        minute = "0" + minute;\r
-               time = hour + ":" + minute + " " + ap;\r
+               time = hour + timeFormatSeparator + minute + " " + ap;\r
+       }\r
+       else {\r
+               if (hour < 10)\r
+                       hour = "0" + hour;\r
+               if (minute < 10)\r
+                       minute = "0" + minute;\r
+               time = hour + timeFormatSeparator + minute;\r
        }\r
        \r
        if (config['showNowAsText'].Value && date.getTime() == now.getTime())\r
                time = '<span class="now">' + config['nowText'].Value + '</span>';\r
+       log("formatTime(): " + time + ", use12hoursTimeFormat=" + use12hoursTimeFormat + ", timeFormatSeparator=" + timeFormatSeparator + ", date.toLocateTimeString(): " + date.toLocaleTimeString());\r
        return time;\r
 }\r
 \r