glossy icon indicating copy to clipboard operation
glossy copied to clipboard

Incoherent Date format

Open jeromevalentin opened this issue 6 years ago • 5 comments

Standard JS Date is displaying:

new Date() 2019-10-11T08:56:49.584Z new Date().toString() 'Fri Oct 11 2019 11:01:59 GMT+0200 (Central European Summer Time)'

You can notice, than when the time zone is displayed, the hour value is upgraded accordingly.

In glossy, by using 'RFC5424', the generateDate function code in produce.js is leading to generate:

2019-10-11T08:46:11.515+02:00 while the locale time was 10:46

Two options should be supported and could be chosen through options:

  1. Generate the date/time in UTC with the Z at the end
  2. Generate the CORRECT locale time with time zone appended

jeromevalentin avatar Oct 11 '19 09:10 jeromevalentin

Proposal of fix in produce.js


...
msgData.push(generateDate(options.date, options.utc));
...

/*
 *  Generate date in RFC 3339 format. If no date is supplied, the default is
 *  the current time in GMT + 0.
 *  @param {Date} dateObject optional Date object
 *  @returns {String} formatted date
 */
function generateDate(dateObject, utc = false) {
    if(!(dateObject instanceof Date)) dateObject = new Date(Date());
    
    // Calcutate the offset
    var timeOffset = 'Z';
    var minutes = 0;
    var hours = 0;
    if (!utc) {
        minutes = Math.abs(dateObject.getTimezoneOffset());
        while(minutes >= 60) {
            hours++;
            minutes -= 60;
        }

        if(dateObject.getTimezoneOffset() < 0) {
            // Ahead of UTC
            timeOffset = '+' + leadZero(hours) + '' + ':' + leadZero(minutes);
        } else if(dateObject.getTimezoneOffset() > 0) {
            // Behind UTC
            timeOffset = '-' + leadZero(hours) + '' + ':' + leadZero(minutes);
        } else {
            // UTC
            timeOffset = 'Z';
        }
    }


    // Date
    formattedDate = dateObject.getUTCFullYear()         + '-' +
    // N.B. Javascript Date objects return months of the year indexed from
    // zero, while the RFC 5424 syslog standard expects months indexed from
    // one.
    leadZero(dateObject.getUTCMonth() + 1)  + '-' +
    // N.B. Javascript Date objects return days of the month indexed from one
    // (unlike months of year), so this does not need any correction.
    leadZero(dateObject.getUTCDate())   + 'T' +
    // Time
    leadZero(utc ? dateObject.getUTCHours(): dateObject.getHours())         + ':' +
    leadZero(utc ? dateObject.getUTCMinutes(): dateObject.getMinutes())       + ':' +
    leadZero(utc ? dateObject.getUTCSeconds(): dateObject.getSeconds())       + '.' +
    leadZero(utc ? dateObject.getUTCMilliseconds() : dateObject.getMilliseconds())  +
    timeOffset;
    
    return formattedDate;
    
}

jeromevalentin avatar Oct 11 '19 09:10 jeromevalentin

Any chance this is going to get merged?

mscottnelson avatar Jun 26 '20 21:06 mscottnelson

I wonder if this was fixed already with #34 ? It was merged on 2016-01-11, but the last glossy release (0.1.7) was in 2014. So it's quite possible this is already fixed.

I checked NPM and there's one newer fork of glossy that fixed the issue for me: https://www.npmjs.com/package/@myndzi/glossy

npm i @myndzi/glossy

bmaupin avatar Sep 27 '21 17:09 bmaupin

... unfortunately I just saw myndzi/glossy also has a bug:

<131>1 2021-09-27T15:52:34.137-07:00 localhost test-graylog-syslog 103039 - - error: Test error message, without stack trace
<131>1 2021-09-27T15:52:35.35-07:00 localhost test-graylog-syslog 103065 - - error: Test error message, without stack trace

I'm guessing 15:52:35.35 should be 15:52:35.035. Unfortunately Graylog interprets it as 15:52:35.350.

bmaupin avatar Sep 27 '21 19:09 bmaupin

... okay, I figured out a way to work around that too; there's yet another fork where that's fixed (https://github.com/pavkriz/glossy). It's not published on NPM, but apparently NPM can actually install from a git repo:

npm install git+https://github.com/pavkriz/glossy

bmaupin avatar Sep 27 '21 20:09 bmaupin