Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incoherent Date format #43

Open
jeromevalentin opened this issue Oct 11, 2019 · 5 comments
Open

Incoherent Date format #43

jeromevalentin opened this issue Oct 11, 2019 · 5 comments

Comments

@jeromevalentin
Copy link

jeromevalentin commented Oct 11, 2019

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
Copy link
Author

jeromevalentin commented Oct 11, 2019

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;
    
}

@mscottnelson
Copy link

Any chance this is going to get merged?

@bmaupin
Copy link

bmaupin commented Sep 27, 2021

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
Copy link

bmaupin commented Sep 27, 2021

... 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
Copy link

bmaupin commented Sep 27, 2021

... 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants