2021-01-25 05:20:52 +01:00
|
|
|
util = require 'util'
|
2023-08-08 11:38:15 +02:00
|
|
|
{ spawn, execSync } = require 'child_process'
|
2021-01-25 05:20:52 +01:00
|
|
|
dayjs = require 'dayjs'
|
2021-11-06 16:27:06 +01:00
|
|
|
delay = require './delay'
|
2021-11-04 06:31:33 +01:00
|
|
|
dayjs.extend require './dayjs_format_ms'
|
2021-01-25 05:20:52 +01:00
|
|
|
platform = require('os').platform()
|
|
|
|
|
|
|
|
|
2023-08-08 11:38:15 +02:00
|
|
|
adjust_time = (delta, adjust_command) ->
|
2021-01-25 05:20:52 +01:00
|
|
|
console.log 'Adjust time...'
|
|
|
|
shell = spawn spawn_args...
|
|
|
|
shell.stderr.on 'data', (data) -> console.error "> " + "#{data}".trim()
|
|
|
|
shell.stdout.on 'data', (data) -> console.debug "> " + "#{data}".trim()
|
|
|
|
shell.on 'close', (code) ->
|
|
|
|
shell.stdin.end()
|
|
|
|
console.log 'Done'
|
|
|
|
process.exit 0
|
|
|
|
wait_data = util.promisify (cb) ->
|
|
|
|
shell.stdout.once 'data', (chunk) ->
|
|
|
|
setTimeout ->
|
|
|
|
cb null, chunk
|
|
|
|
, 1
|
|
|
|
input_line = util.promisify (cmd, cb) ->
|
|
|
|
console.debug "$ #{cmd}"
|
|
|
|
shell.stdin.write "#{cmd}\n", (p...) ->
|
|
|
|
await wait_data() if platform in ['win32']
|
|
|
|
cb p...
|
2021-11-06 16:27:06 +01:00
|
|
|
if platform in ['win32']
|
|
|
|
await wait_data()
|
|
|
|
new_time = dayjs().add(delta, 'ms')
|
2023-08-08 11:38:15 +02:00
|
|
|
cmd = new_time.format adjust_command
|
2021-11-06 16:27:06 +01:00
|
|
|
else
|
2023-08-08 11:38:15 +02:00
|
|
|
adjust_command = adjust_command.replace /ss\.S+/, 'ss'
|
2021-11-06 16:27:06 +01:00
|
|
|
new_time = dayjs().add(delta, 'ms')
|
|
|
|
ms = new_time.get('millisecond')
|
|
|
|
if ms > 0
|
|
|
|
wait_time = 1000 - ms
|
|
|
|
new_time = new_time.add(wait_time, 'ms')
|
|
|
|
delay wait_time
|
2023-08-08 11:38:15 +02:00
|
|
|
cmd = new_time.format adjust_command
|
2021-01-25 05:20:52 +01:00
|
|
|
await input_line cmd
|
|
|
|
await input_line 'exit'
|
|
|
|
|
|
|
|
|
2023-08-08 11:38:15 +02:00
|
|
|
# win32: get local date format by command:
|
|
|
|
# reg query "HKCU\Control Panel\International" /v sShortDate
|
|
|
|
#* @see # https://calendars.wikia.org/wiki/Date_format_by_country
|
|
|
|
if platform isnt 'win32'
|
|
|
|
adjust_time.COMMAND = '[date -s ]"YYYY-MM-DD HH:mm:ss"'
|
|
|
|
else
|
|
|
|
date_format = 'MM-DD-YY'
|
|
|
|
try
|
|
|
|
date_format_raw = ('' + execSync(
|
|
|
|
'reg query "HKCU\\Control Panel\\International" /v sShortDate'
|
|
|
|
)).trim().split(/[\n\r]+/).pop()?.trim().split(/\s+/).pop()
|
|
|
|
catch err
|
|
|
|
null
|
|
|
|
if date_format_raw and date_format_raw isnt ''
|
|
|
|
date_format = date_format_raw.
|
|
|
|
replace(/[^YMDymd]+/g, '-').
|
|
|
|
replace(/y+/gi, 'YY').
|
|
|
|
replace(/m+/gi, 'MM').
|
|
|
|
replace(/d+/gi, 'DD')
|
|
|
|
adjust_time.COMMAND = '[time ]HH:mm:ss.SS[ && date ]' + date_format
|
2021-01-25 05:20:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
SHELL_ARGS = {
|
|
|
|
win32: [
|
|
|
|
'cmd'
|
|
|
|
['/q', '/k', 'prompt $H && chcp 65001 > nul']
|
|
|
|
{
|
|
|
|
windowsHide: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
linux: [
|
|
|
|
'sh'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
spawn_args = SHELL_ARGS[platform] or SHELL_ARGS.linux
|
|
|
|
if not /UTF\-8/i.test (process.env.LANGUAGE or '')
|
|
|
|
process.env.LANGUAGE = 'C.UTF-8'
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = adjust_time
|