@@ -12,12 +12,35 @@ const colors = Object.freeze({
1212 ERROR : chalk . red
1313} )
1414
15- const defaultTimestampFormatter = ( date ) => {
16- const tzOffset = date . getTimezoneOffset ( ) * 60000 // offset in milliseconds
17- const localISOTime = ( new Date ( date - tzOffset ) ) . toISOString ( ) . slice ( 0 , - 1 ) // => '2015-01-26T06:40:36.181'
18- return localISOTime
15+ /**
16+ * Converts a date to the RFC3339 profile of the ISO 8601 standard
17+ *
18+ * Example: 2019-02-08T19:02:09.432+01:00
19+ * https://tools.ietf.org/html/rfc3339#section-5.6
20+ *
21+ * Inspired by https://stackoverflow.com/a/17415677/1362167
22+ */
23+ const dateToISOString = ( d , milliseconds = true ) => {
24+ // Pad an integer >=0, <= 99 to 2 digits.
25+ // Useful to pad days, minutes, hours, months etc.
26+ const pad2 = num => `${ num < 10 ? '0' : '' } ${ num } `
27+ const pad3 = num => `${ num < 100 ? ( num < 10 ? '00' : '0' ) : '' } ${ num } `
28+ const offset = - d . getTimezoneOffset ( )
29+ const absOffset = Math . abs ( offset )
30+ return d . getFullYear ( )
31+ + '-' + pad2 ( d . getMonth ( ) + 1 )
32+ + '-' + pad2 ( d . getDate ( ) )
33+ + 'T' + pad2 ( d . getHours ( ) )
34+ + ':' + pad2 ( d . getMinutes ( ) )
35+ + ':' + pad2 ( d . getSeconds ( ) )
36+ + ( milliseconds ? ( '.' + pad3 ( d . getMilliseconds ( ) ) ) : '' )
37+ + ( offset >= 0 ? '+' : '-' )
38+ + pad2 ( Math . floor ( absOffset / 60 ) )
39+ + ':' + pad2 ( absOffset % 60 )
1940}
2041
42+ const defaultTimestampFormatter = dateToISOString
43+
2144const defaultNameFormatter = ( name ) => {
2245 return chalk . green ( name )
2346}
@@ -99,3 +122,6 @@ defaultLogger.getLoggers = () => _loggersByName
99122// Export default logger. Can be used directly, or can be used
100123// to obtain child loggers.
101124module . exports = defaultLogger
125+ // Export dateToISOString, users may want to use it to define a custom
126+ // timestampformatter
127+ module . exports . dateToISOString = dateToISOString
0 commit comments