From 79e77ffef0223d804a1d8931500d1d6c8caeeb7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 19 Sep 2016 10:02:31 +0200 Subject: [PATCH] Date format: match ordering of components in weechat --- js/connection.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/js/connection.js b/js/connection.js index 8648b5b..1484661 100644 --- a/js/connection.js +++ b/js/connection.js @@ -137,21 +137,39 @@ weechat.factory('connection', angularFormat = short24; } - // Check for day of month in time string - if (timeFormat.indexOf("%d") > -1 || timeFormat.indexOf("%e") > -1) { - angularFormat = "dd" + _timeDelimiter(" ") + angularFormat; + // Assemble day of month + var date_components = []; - // month, too? - if (timeFormat.indexOf("%m") > -1) { - angularFormat = "MM" + _timeDelimiter("-") + angularFormat; + // Check for day of month in time format + var day_pos = Math.max(timeFormat.indexOf("%d"), + timeFormat.indexOf("%e")); + date_components.push([day_pos, "dd"]); - // year as well? - if (timeFormat.indexOf("%y") > -1) { - angularFormat = "yy" + _timeDelimiter("-") + angularFormat; - } else if (timeFormat.indexOf("%Y") > -1) { - angularFormat = "yyyy" + _timeDelimiter("-") + angularFormat; - } + // month of year? + var month_pos = timeFormat.indexOf("%m"); + date_components.push([month_pos, "MM"]); + + // year as well? + var year_pos = Math.max(timeFormat.indexOf("%y"), + timeFormat.indexOf("%Y")); + if (timeFormat.indexOf("%y") > -1) { + date_components.push([year_pos, "yy"]); + } else if (timeFormat.indexOf("%Y") > -1) { + date_components.push([year_pos, "yyyy"]); + } + + // if there is a date, assemble it in the right order + if (date_components.length > 0) { + date_components.sort(); + var format_array = []; + for (var i = 0; i < date_components.length; i++) { + if (date_components[i][0] == -1) continue; + format_array.push(date_components[i][1]); } + // TODO: parse delimiter as well? For now, use '/' as it is + // more common internationally than '-' + var date_format = format_array.join(_timeDelimiter("/")); + angularFormat = date_format + _timeDelimiter(" ") + angularFormat; } $rootScope.angularTimeFormat = angularFormat;