From c532751be06056956bdac9a186f773162f7a4c61 Mon Sep 17 00:00:00 2001 From: Bob Wen Date: Sat, 6 Nov 2021 23:27:06 +0800 Subject: [PATCH] better adjust method win32: there's a way to get the perfect command for everybody in every location; linux: wait for a whole second tick before date command; --- README.md | 2 +- adjust_time.coffee | 26 ++++++++++++++++++++------ delay.coffee | 5 +++++ index.coffee | 7 +------ 4 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 delay.coffee diff --git a/README.md b/README.md index 45e9c1b..c8789e6 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Done Note: Windows users need to be aware of the date format in their region. Default format(from [dayjs](https://day.js.org/docs/en/display/format)) is `MM-DD-YY`, users in non-U.S. regions may need to customize it with the `-C` parameter, eg: -`htpdate -s -C "[time ]HH[:]mm[:]ss[.]SS[ && date ]YY[-]MM[-]DD" github.com`. +`htpdate -s -C "[time ]HH:mm:ss.SS[ && date ]YY-MM-DD" github.com`. Query from multiple URLs diff --git a/adjust_time.coffee b/adjust_time.coffee index e474e26..d8743ad 100644 --- a/adjust_time.coffee +++ b/adjust_time.coffee @@ -1,6 +1,7 @@ util = require 'util' { spawn } = require 'child_process' dayjs = require 'dayjs' +delay = require './delay' dayjs.extend require './dayjs_format_ms' platform = require('os').platform() @@ -24,17 +25,30 @@ adjust_time = (delta) -> shell.stdin.write "#{cmd}\n", (p...) -> await wait_data() if platform in ['win32'] cb p... - await wait_data() if platform in ['win32'] - cmd = dayjs().add(delta, 'ms').format adjust_time.command + if platform in ['win32'] + await wait_data() + new_time = dayjs().add(delta, 'ms') + cmd = new_time.format adjust_time.command + else + adjust_time.command = adjust_time.command.replace /ss\.S+/, 'ss' + 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 + cmd = new_time.format adjust_time.command await input_line cmd await input_line 'exit' COMMANDS = { - win32: '[time ]HH[:]mm[:]ss[.]SS[ && date ]MM[-]DD[-]YY' - # FIXME linux command date or timedatectl cannot process the millisecond parts (.SSS) - # we can wait for a whole second tick to solve the problem - linux: '[date +%FT%T -s ]YYYY[-]MM[-]DDTHH[:]mm[:]ss[.]SSS' + # FIXME get the perfect win32 command with right format + # yes I got it, weird but effective + # reg query "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate + # various formats need to be recognized... + win32: '[time ]HH:mm:ss.SS[ && date ]MM-DD-YY' + linux: '[date -s ]"YYYY-MM-DD HH:mm:ss"' } adjust_time.command = COMMANDS[platform] or COMMANDS.linux diff --git a/delay.coffee b/delay.coffee new file mode 100644 index 0000000..846bd14 --- /dev/null +++ b/delay.coffee @@ -0,0 +1,5 @@ +util = require 'util' + +module.exports = util.promisify (ms, cb) -> + return cb() if ms <= 0 + setTimeout cb, ms diff --git a/index.coffee b/index.coffee index f98237e..38ee965 100755 --- a/index.coffee +++ b/index.coffee @@ -1,6 +1,5 @@ #!/usr/bin/env coffee -util = require 'util' got = require 'got' Agent = require 'agentkeepalive' { HttpProxyAgent, HttpsProxyAgent } = require('hpagent') @@ -8,6 +7,7 @@ dayjs = require 'dayjs' info = require './package.json' median = require './median' adjust_time = require './adjust_time' +delay = require './delay' argv = require('./argv') { @@ -177,11 +177,6 @@ get_time_delta = (url) -> delta -delay = util.promisify (ms, cb) -> - return cb() if ms <= 0 - setTimeout cb, ms - - do -> if proxy isnt '' msg = ''