aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/time.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-06-17 01:26:24 +1000
committerSam Potts <sam@potts.es>2018-06-17 01:26:24 +1000
commitf1b4db4f3665ea824ad75d7b68df1efc9f1a0524 (patch)
treed3fd6d76a15a94f8a5889b3d0033c65b35420c4f /src/js/utils/time.js
parent6a6f3914c0943c72a85c290a8175d759e7f70809 (diff)
parentd4abb4b1438cb316aacae480e7b7e9b055a60b24 (diff)
downloadplyr-f1b4db4f3665ea824ad75d7b68df1efc9f1a0524.tar.lz
plyr-f1b4db4f3665ea824ad75d7b68df1efc9f1a0524.tar.xz
plyr-f1b4db4f3665ea824ad75d7b68df1efc9f1a0524.zip
Merge branch 'develop' into a11y-improvements
# Conflicts: # dist/plyr.js # dist/plyr.js.map # dist/plyr.min.js # dist/plyr.min.js.map # dist/plyr.polyfilled.js # dist/plyr.polyfilled.js.map # dist/plyr.polyfilled.min.js # dist/plyr.polyfilled.min.js.map # src/js/controls.js # src/js/fullscreen.js # src/js/plyr.js # src/js/ui.js # src/js/utils.js
Diffstat (limited to 'src/js/utils/time.js')
-rw-r--r--src/js/utils/time.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/js/utils/time.js b/src/js/utils/time.js
new file mode 100644
index 00000000..7c9860fd
--- /dev/null
+++ b/src/js/utils/time.js
@@ -0,0 +1,36 @@
+// ==========================================================================
+// Time utils
+// ==========================================================================
+
+import is from './is';
+
+// Time helpers
+export const getHours = value => parseInt((value / 60 / 60) % 60, 10);
+export const getMinutes = value => parseInt((value / 60) % 60, 10);
+export const getSeconds = value => parseInt(value % 60, 10);
+
+// Format time to UI friendly string
+export function formatTime(time = 0, displayHours = false, inverted = false) {
+ // Bail if the value isn't a number
+ if (!is.number(time)) {
+ return formatTime(null, displayHours, inverted);
+ }
+
+ // Format time component to add leading zero
+ const format = value => `0${value}`.slice(-2);
+
+ // Breakdown to hours, mins, secs
+ let hours = getHours(time);
+ const mins = getMinutes(time);
+ const secs = getSeconds(time);
+
+ // Do we need to display hours?
+ if (displayHours || hours > 0) {
+ hours = `${hours}:`;
+ } else {
+ hours = '';
+ }
+
+ // Render
+ return `${inverted && time > 0 ? '-' : ''}${hours}${format(mins)}:${format(secs)}`;
+}