[gnome-maps/wip/mlundblad/transit-routing: 1/13] Add module with time utility functions
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/mlundblad/transit-routing: 1/13] Add module with time utility functions
- Date: Sun, 12 Feb 2017 22:06:49 +0000 (UTC)
commit bb26cfc9a27c5fdc76b1c9b6a1a10347593a8140
Author: Marcus Lundblad <ml update uu se>
Date: Sun Feb 12 21:40:20 2017 +0100
Add module with time utility functions
Currently contains a function to parse an
entered time into a normalized HH:MM string
format.
https://bugzilla.gnome.org/show_bug.cgi?id=755808
src/org.gnome.Maps.src.gresource.xml | 2 +
src/time.js | 83 ++++++++++++++++++++++++++++++++++
2 files changed, 85 insertions(+), 0 deletions(-)
---
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index 5442def..ce72b67 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -77,6 +77,8 @@
<file>socialPlaceMatcher.js</file>
<file>storedRoute.js</file>
<file>togeojson/togeojson.js</file>
+ <file>time.js</file>
+ <file>transitArrivalMarker.js</file>
<file>transitArrivalRow.js</file>
<file>transitItineraryRow.js</file>
<file>transitLegRow.js</file>
diff --git a/src/time.js b/src/time.js
new file mode 100644
index 0000000..e6b1127
--- /dev/null
+++ b/src/time.js
@@ -0,0 +1,83 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2017, Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+/* parse a time from free-format into a string representation:
+ * hour:min
+ * TODO: maybe try to use some library to get better locale handling,
+ * or push for something in GLib */
+function parseTimeString(timeString) {
+ let pmSet = false;
+ let hours;
+ let mins;
+ /* remove extra whitespaces */
+ timeString = timeString.replace(/\s+/g, '');
+
+ if (timeString.endsWith('am')) {
+ timeString = timeString.substring(0, timeString.length - 2);
+ } else if (timeString.endsWith('pm')) {
+ timeString = timeString.substring(0, timeString.length - 2);
+ pmSet = true;
+ }
+
+ /* allow using :, ., and the ratio symbol to separate hours:mins */
+ if (timeString.charAt(2) === ':' || timeString.charAt(1) === ':')
+ timeString = timeString.replace(':', '');
+ else if (timeString.charAt(2) === '.' || timeString.charAt(1) === '.')
+ timeString = timeString.replace('.', '');
+ else if (timeString.charAt(2) === '\u2236' ||
+ timeString.charAt(1) === '\u2236')
+ timeString = timeString.replace('\u2236', '');
+
+ if (timeString.length === 4) {
+ /* expect a full time specification (hours, minutes) */
+ hours = timeString.substring(0, 2);
+ mins = timeString.substring(2, 4);
+ } else if (timeString.length === 3) {
+ /* interpret a 3 digit string as h:mm */
+ hours = '0' + timeString.substring(0, 1);
+ mins = timeString.substring(1, 3);
+ } else if (timeString.length === 2) {
+ /* expect just the hour part */
+ hours = timeString.substring(0, 2);
+ mins = '00';
+ } else if (timeString.length === 1) {
+ /* expect just the hour part, one digit */
+ hours = '0' + timeString;
+ mins = '00';
+ } else {
+ /* this makes no sense, just bail out */
+ return null;
+ }
+
+ /* check if the parts can be interpreted as numbers */
+ if (hours % 1 === 0 && mins % 1 === 0) {
+ if (pmSet)
+ hours = parseInt(hours) + 12;
+
+ /* if the hours or minutes is out-of-range, bail out */
+ if (hours < 0 || hours > 24 || mins < 0 || mins > 59)
+ return null;
+
+ return hours + ':' + mins;
+ } else {
+ return null;
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]