aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils/time.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-06-13 00:02:55 +1000
committerSam Potts <sam@potts.es>2018-06-13 00:02:55 +1000
commit392dfd024c505f5ae1bbb2f0d3e0793c251a1f35 (patch)
treeaedb56d3945eaa10bf74e61902e16c08fd24914a /src/js/utils/time.js
parent840e31a693462e7ed9f7644a13a0187d9e9d93a9 (diff)
downloadplyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.tar.lz
plyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.tar.xz
plyr-392dfd024c505f5ae1bbb2f0d3e0793c251a1f35.zip
Utils broken down into seperate files and exports
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..0c9fce64
--- /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 ? '-' : ''}${hours}${format(mins)}:${format(secs)}`;
+}