-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-server.js
31 lines (27 loc) · 868 Bytes
/
time-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict'
const net = require('net')
function preFillZerosRecursive(value, numberOfDigits){
if (value.length == numberOfDigits || numberOfDigits == 0) return value
return preFillZeros("0"+value, numberOfDigits)
}
function preFillZerosInterative(value, numberOfDigits){
let diff = numberOfDigits - value.toString().length
if (diff <= 0) {
return value
}
// for (let i = 0; i < diff; i++){
// value = "0" + value
// }
//return value
return "0".repeat(diff)+value
}
const server = net.createServer(function (socket) {
let date = new Date()
let data = `${date.getFullYear()}-\
${preFillZerosInterative(date.getMonth()+1,2)}-\
${preFillZerosInterative(date.getDate(),2)} \
${preFillZerosInterative(date.getHours(),2)}\
:${preFillZerosInterative(date.getMinutes(),2)}\n`
socket.end(data)
})
server.listen(process.argv[2])