Skip to content

Commit 75e9ac5

Browse files
authored
Merge pull request #1 from bmaupin/fix/milliseconds
fix: Pad milliseconds with three zeroes
2 parents fea0d5c + c276535 commit 75e9ac5

File tree

5 files changed

+56
-36
lines changed

5 files changed

+56
-36
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Source: https://github.com/actions/starter-workflows/blob/main/ci/node.js.yml
2+
3+
name: CI
4+
5+
on:
6+
push:
7+
branches: [master]
8+
pull_request:
9+
branches: [master]
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
matrix:
17+
node-version: [0.10.x, 0.12.x, 4.x, 6.x, 8.x, 10.x, 12.x, 14.x, 16.x]
18+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v2
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
- run: npm test

lib/glossy/produce.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ function generateBSDDate(dateObject) {
354354

355355
/*
356356
* Generate date in RFC 3339 format. If no date is supplied, the default is
357-
* the current time in GMT + 0.
357+
* the current time in local timezone.
358358
* @param {Date} dateObject optional Date object
359359
* @returns {String} formatted date
360360
*/
@@ -364,11 +364,8 @@ function generateDate(dateObject) {
364364
// Calcutate the offset
365365
var timeOffset;
366366
var minutes = Math.abs(dateObject.getTimezoneOffset());
367-
var hours = 0;
368-
while(minutes >= 60) {
369-
hours++;
370-
minutes -= 60;
371-
}
367+
var hours = Math.floor(Math.abs(minutes) / 60);
368+
minutes = Math.abs(minutes) % 60;
372369

373370
if(dateObject.getTimezoneOffset() < 0) {
374371
// Ahead of UTC
@@ -383,20 +380,20 @@ function generateDate(dateObject) {
383380

384381

385382
// Date
386-
var formattedDate = dateObject.getUTCFullYear() + '-' +
387-
// N.B. Javascript Date objects return months of the year indexed from
388-
// zero, while the RFC 5424 syslog standard expects months indexed from
389-
// one.
390-
leadZero(dateObject.getMonth() + 1) + '-' +
391-
// N.B. Javascript Date objects return days of the month indexed from one
392-
// (unlike months of year), so this does not need any correction.
393-
leadZero(dateObject.getDate()) + 'T' +
394-
// Time
395-
leadZero(dateObject.getHours()) + ':' +
396-
leadZero(dateObject.getMinutes()) + ':' +
397-
leadZero(dateObject.getSeconds()) + '.' +
398-
leadZero(dateObject.getMilliseconds()) +
399-
timeOffset;
383+
var formattedDate = dateObject.getFullYear() + '-' +
384+
// N.B. Javascript Date objects return months of the year indexed from
385+
// zero, while the RFC 5424 syslog standard expects months indexed from
386+
// one.
387+
leadZero(dateObject.getMonth() + 1) + '-' +
388+
// N.B. Javascript Date objects return days of the month indexed from one
389+
// (unlike months of year), so this does not need any correction.
390+
leadZero(dateObject.getDate()) + 'T' +
391+
// Time
392+
leadZero(dateObject.getHours()) + ':' +
393+
leadZero(dateObject.getMinutes()) + ':' +
394+
leadZero(dateObject.getSeconds()) + '.' +
395+
(dateObject.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
396+
timeOffset;
400397

401398
return formattedDate;
402399

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@myndzi/glossy",
3-
"version": "0.1.10",
3+
"version": "0.1.11",
44
"description": "Syslog parser and producer",
55
"keywords": [
66
"syslog",
77
"logging"
88
],
9-
"url": "http://github.com/squeeks/glossy",
9+
"url": "http://github.com/myndzi/glossy",
1010
"main": "./index.js",
1111
"author": "Squeeks <[email protected]>",
1212
"maintainers": [
@@ -58,10 +58,7 @@
5858
},
5959
"repository": {
6060
"type": "git",
61-
"url": "http://github.com/squeeks/glossy.git"
62-
},
63-
"bugs": {
64-
"url": "http://github.com/squeeks/glossy/issues"
61+
"url": "http://github.com/myndzi/glossy.git"
6562
},
6663
"licenses": [
6764
{
@@ -70,6 +67,6 @@
7067
}
7168
],
7269
"engines": {
73-
"node": ">= 0.2.5"
70+
"node": ">= 0.10.0"
7471
}
7572
}

test/produce.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ var msg = syslogProducer.produce({
3333
date: new Date(1234567890000),
3434
message: 'Test Message'
3535
});
36-
assert.equal(msg, "<163>1 2009-02-14T00:31:30.00+01:00 localhost sudo 123 - - Test Message",'Valid message returned');
36+
assert.equal(msg, "<163>1 2009-02-14T00:31:30.000+01:00 localhost sudo 123 - - Test Message",'Valid message returned');
3737

3838
syslogProducer.produce({
3939
facility: 'audit',
4040
severity: 'error',
4141
host: '127.0.0.1',
4242
appName: 'sudo',
4343
pid: '419',
44-
date: new Date(1234567890000),
44+
date: new Date(1234567890001),
4545
message: 'Test Message'
4646
}, function(cbMsg) {
47-
assert.equal(cbMsg, '<107>1 2009-02-14T00:31:30.00+01:00 127.0.0.1 sudo 419 - - Test Message', 'Valid message in callback returned');
47+
assert.equal(cbMsg, '<107>1 2009-02-14T00:31:30.001+01:00 127.0.0.1 sudo 419 - - Test Message', 'Valid message in callback returned');
4848
});
4949

5050
BSDProducer.produce({
@@ -137,7 +137,7 @@ var structuredMsg = syslogProducer.produce({
137137
host: 'mymachine.example.com',
138138
appName: 'evntslog',
139139
msgID: 'ID47',
140-
date: new Date(1234567890000),
140+
date: new Date(1234567890010),
141141
structuredData: {
142142
'exampleSDID@32473': {
143143
'iut': "3",
@@ -150,25 +150,25 @@ var structuredMsg = syslogProducer.produce({
150150
});
151151

152152
assert.ok(structuredMsg);
153-
assert.equal(structuredMsg, '<163>1 2009-02-14T00:31:30.00+01:00 mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011" seqNo="1"] BOMAn application event log entry...');
153+
assert.equal(structuredMsg, '<163>1 2009-02-14T00:31:30.010+01:00 mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011" seqNo="1"] BOMAn application event log entry...');
154154

155155
var structuredWithArray = syslogProducer.produce({
156156
facility: 'local4',
157157
severity: 'error',
158158
host: 'mymachine.example.com',
159159
appName: 'evntslog',
160160
msgID: 'ID47',
161-
date: new Date(1234567890000),
161+
date: new Date(1234567890100),
162162
structuredData: {
163163
'origin': {
164164
'ip': ['127.0.1.1', '127.0.0.1']
165165
}
166166
},
167-
message: 'BOMAn application event log entry...'
167+
message: 'BOMAn application event log entry...'
168168
});
169169

170170
assert.ok(structuredWithArray);
171-
assert.equal(structuredWithArray, '<163>1 2009-02-14T00:31:30.00+01:00 mymachine.example.com evntslog - ID47 [origin ip="127.0.1.1" ip="127.0.0.1"] BOMAn application event log entry...');
171+
assert.equal(structuredWithArray, '<163>1 2009-02-14T00:31:30.100+01:00 mymachine.example.com evntslog - ID47 [origin ip="127.0.1.1" ip="127.0.0.1"] BOMAn application event log entry...');
172172

173173
var messageWithOneDigitDate = presetProducer.emergency({
174174
facility: 'news',

0 commit comments

Comments
 (0)