From 6b485781ab5d2877b52f21e7f03dcab013a76c45 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 17 Jun 2016 09:29:52 -0400 Subject: [PATCH] Refactored Javascript time functions. --- common.js | 6 ++++++ elite_time.js | 38 ++++++++++++++++++++++++++++++++++++++ entry.js | 45 --------------------------------------------- lib/footer.php | 7 ++----- 4 files changed, 46 insertions(+), 50 deletions(-) create mode 100644 elite_time.js diff --git a/common.js b/common.js index 26dd094..287dc05 100644 --- a/common.js +++ b/common.js @@ -1,3 +1,9 @@ +/* Pad string. */ +function zero_pad(n) { + if (n < 10) return '0' + String(n); + else return String(n); +} + /* Copy bbcode to clipboard. */ function copy_clipboard(event) { try { diff --git a/elite_time.js b/elite_time.js new file mode 100644 index 0000000..8da88ca --- /dev/null +++ b/elite_time.js @@ -0,0 +1,38 @@ +/* Convert a 21st century date to 34th century. */ +function elite_date(then) { + var now = null; + var then_date = null; + var then_time = null; + if (then) { + var m = then.match(/^(\d\d\d\d-\d\d-\d\d)(?:\s+(\d\d:\d\d:\d\d))?$/); + if (m) { + then_date = m[1]; + then_time = m[2]; + } + } + if (! then_date || ! then_time) now = new Date(); + if (then_date) date = then_date; + else { + var year = String(now.getFullYear() + 1286); + var month = zero_pad(now.getMonth() + 1); + var day = zero_pad(now.getDate()); + var date = [year, month, day].join('-'); + } + if (then_time) time = then_time; + else { + var hours = zero_pad(now.getHours()); + var minutes = zero_pad(now.getMinutes()); + var seconds = zero_pad(now.getSeconds()); + var time = [hours, minutes, seconds].join(':'); + } + return [date, time].join(' '); +} + +/* Convert a 34th century date to 21st century. */ +function real_date(elite_date) { + var m = elite_date.match(/^(\d\d\d\d)(.*)/); + if (! m) return elite_date; + var elite_year = parseInt(m[1]); + if (elite_year < 3300) return elite_date; + return String(elite_year - 1286) + m[2]; +} diff --git a/entry.js b/entry.js index e62e49a..48a7dc7 100644 --- a/entry.js +++ b/entry.js @@ -131,12 +131,6 @@ function get_station_name(table, i) { return p.innerHTML; } -/* Pad string. */ -function zero_pad(n) { - if (n < 10) return '0' + String(n); - else return String(n); -} - /* Is this an existing entry we are displaying? */ function get_entry_id() { var hidden = document.getElementsByName('add_entry'); @@ -144,45 +138,6 @@ function get_entry_id() { return parseInt(hidden[0].value); } -/* Convert a 21st century date to 34th century. */ -function elite_date(then) { - var now = null; - var then_date = null; - var then_time = null; - if (then) { - var m = then.match(/^(\d\d\d\d-\d\d-\d\d)(?:\s+(\d\d:\d\d:\d\d))?$/); - if (m) { - then_date = m[1]; - then_time = m[2]; - } - } - if (! then_date || ! then_time) now = new Date(); - if (then_date) date = then_date; - else { - var year = String(now.getFullYear() + 1286); - var month = zero_pad(now.getMonth() + 1); - var day = zero_pad(now.getDate()); - var date = [year, month, day].join('-'); - } - if (then_time) time = then_time; - else { - var hours = zero_pad(now.getHours()); - var minutes = zero_pad(now.getMinutes()); - var seconds = zero_pad(now.getSeconds()); - var time = [hours, minutes, seconds].join(':'); - } - return [date, time].join(' '); -} - -/* Convert a 34th century date to 21st century. */ -function real_date(elite_date) { - var m = elite_date.match(/^(\d\d\d\d)(.*)/); - if (! m) return elite_date; - var elite_year = parseInt(m[1]); - if (elite_year < 3300) return elite_date; - return String(elite_year - 1286) + m[2]; -} - /* Convert a date to timestamp. */ function from_iso8601(date) { var m = date.match(/^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/); diff --git a/lib/footer.php b/lib/footer.php index a9f5f27..bc0ecf6 100644 --- a/lib/footer.php +++ b/lib/footer.php @@ -1,9 +1,6 @@ \n"; - } - if ($module && file_exists("$root/$module.js")) { - echo "\n"; + foreach (array('common', 'elite_time', $module) as $script) { + if (file_exists("$root/$script.js")) echo "\n"; } ?> -- 2.7.4