mirror of
https://github.com/psychopy/psychojs.git
synced 2025-05-11 16:18:10 +00:00
util/Clock: split date formatting and parsing into separate funcs, drop moment import
This commit is contained in:
parent
4ce8641639
commit
4c1426dea5
@ -7,8 +7,6 @@
|
||||
* @license Distributed under the terms of the MIT License
|
||||
*/
|
||||
|
||||
import moment from 'moment';
|
||||
|
||||
|
||||
/**
|
||||
* <p>MonotonicClock offers a convenient way to keep track of time during experiments. An experiment can have as many independent clocks as needed, e.g. one to time responses, another one to keep track of stimuli, etc.</p>
|
||||
@ -69,20 +67,22 @@ export class MonotonicClock
|
||||
|
||||
|
||||
/**
|
||||
* Get the clock's current time as a formatted string.
|
||||
* Get the current timestamp with language-sensitive formatting rules applied.
|
||||
*
|
||||
* <p>Note: this is mostly used as an appendix to the name of the keys save to the server.</p>
|
||||
* <p>Note: This is just a convenience wrapper around `Intl.DateTimeFormat()`.</p>
|
||||
*
|
||||
* @name module:util.MonotonicClock.getDateStr
|
||||
* @name module:util.MonotonicClock.getDate
|
||||
* @function
|
||||
* @public
|
||||
* @static
|
||||
* @return {string} a string representing the current time formatted as YYYY-MM-DD_HH[h]mm.ss.sss
|
||||
* @param {string|array.string} locales - A string with a BCP 47 language tag, or an array of such strings.
|
||||
* @param {object} options - An object with detailed date and time styling information.
|
||||
* @return {string} The current timestamp in the chosen format.
|
||||
*/
|
||||
static getDateStr()
|
||||
static getDate(locales = 'en-CA', optionsMaybe)
|
||||
{
|
||||
const date = new Date();
|
||||
const dateTimeFormat = new Intl.DateTimeFormat('en-CA', {
|
||||
const options = Object.assign({
|
||||
hour12: false,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
@ -91,11 +91,28 @@ export class MonotonicClock
|
||||
minute: 'numeric',
|
||||
second: 'numeric',
|
||||
fractionalSecondDigits: 3
|
||||
});
|
||||
}, optionsMaybe);
|
||||
|
||||
return dateTimeFormat
|
||||
// yyyy-mm-dd, hh:mm:ss.sss
|
||||
.format(date)
|
||||
const dateTimeFormat = new Intl.DateTimeFormat(locales, options);
|
||||
|
||||
return dateTimeFormat.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the clock's current time in the default format filtering out file name unsafe characters.
|
||||
*
|
||||
* <p>Note: This is mostly used as an appendix to the name of the keys save to the server.</p>
|
||||
*
|
||||
* @name module:util.MonotonicClock.getDateStr
|
||||
* @function
|
||||
* @public
|
||||
* @static
|
||||
* @return {string} A string representing the current time formatted as YYYY-MM-DD_HH[h]mm.ss.sss
|
||||
*/
|
||||
static getDateStr()
|
||||
{
|
||||
// yyyy-mm-dd, hh:mm:ss.sss
|
||||
return MonotonicClock.getDate()
|
||||
// yyyy-mm-dd_hh:mm:ss.sss
|
||||
.replace(', ', '_')
|
||||
// yyyy-mm-dd_hh[h]mm:ss.sss
|
||||
|
Loading…
Reference in New Issue
Block a user