diff --git a/client/package.json b/client/package.json index da7b48116..0550ae1ac 100644 --- a/client/package.json +++ b/client/package.json @@ -11,7 +11,7 @@ "@testing-library/react": "^11.1.0", "@testing-library/user-event": "^12.1.10", "axios": "^0.21.1", - "date-fns": "^2.19.0", + "date-fns": "^2.20.1", "framer-motion": "^4", "react": "^17.0.1", "react-dom": "^17.0.1", diff --git a/client/src/common/dateConfig.js b/client/src/common/dateConfig.js index dc6531a75..c4d94b2af 100644 --- a/client/src/common/dateConfig.js +++ b/client/src/common/dateConfig.js @@ -1,22 +1,41 @@ -import { addMinutes, format, parseISO } from 'date-fns'; export const timeFormat = 'HH:mm'; export const timeFormatSeconds = 'HH:mm:ss'; +// time string from miliseconds +export const stringFromMillis = ( + ms, + showSeconds = true, + delim = ':', + ifNull = '...' +) => { + if (ms === null) return ifNull; + const showWith0 = (value) => (value < 10 ? `0${value}` : value); + const hours = showWith0(Math.floor((ms / (1000 * 60 * 60)) % 60)); + const minutes = showWith0(Math.floor((ms / (1000 * 60)) % 60)); + const seconds = showWith0(Math.floor((ms / 1000) % 60)); + return showSeconds + ? `${parseInt(hours) ? `${hours}${delim}` : ''}${minutes}${delim}${seconds}` + : `${parseInt(hours) ? `${hours}${delim}` : ''}${minutes}`; +}; + +// millis to seconds +export const millisToSeconds = (millis) => { + return Math.floor(millis / 1000); +}; + +// millis to minutes +export const millisToMinutes = (millis) => { + return Math.floor(millis / 60000); +}; + // make date with string export const timeToDate = (time) => { - const today = new Date(); - return new Date(today.toDateString() + ' ' + time); + return new Date(refDate.toDateString() + ' ' + time); }; -// utility to parse and format -export const dateToTime = (time) => { - const fTime = parseISO(time, 1); - return format(fTime, timeFormat); -}; - -// small shorthand for adding delay and formatting date -export const addAndFormat = (time, delay) => { - const fTime = parseISO(time, 1); - return format(addMinutes(fTime, delay), timeFormat); +// timeStringToMillis +export const timeStringToMillis = (string) => { + let time = string.split(':'); + return time[0] * 3600000 + time[1] * 60000; }; diff --git a/client/yarn.lock b/client/yarn.lock index e177320a4..a79b7405c 100644 --- a/client/yarn.lock +++ b/client/yarn.lock @@ -4696,10 +4696,10 @@ data-urls@^2.0.0: whatwg-mimetype "^2.3.0" whatwg-url "^8.0.0" -date-fns@^2.19.0: - version "2.19.0" - resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.19.0.tgz#65193348635a28d5d916c43ec7ce6fbd145059e1" - integrity sha512-X3bf2iTPgCAQp9wvjOQytnf5vO5rESYRXlPIVcgSbtT5OTScPcsf9eZU+B/YIkKAtYr5WeCii58BgATrNitlWg== +date-fns@^2.20.1: + version "2.20.1" + resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.20.1.tgz#7e60b7035284a5f83e37500376e738d9f49ecfd3" + integrity sha512-8P5M8Kxbnovd0zfvOs7ipkiVJ3/zZQ0F/nrBW4x5E+I0uAZVZ80h6CKd24fSXQ5TLK5hXMtI4yb2O5rEZdUt2A== debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.0, debug@^2.6.9: version "2.6.9"