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;
This commit is contained in:
Bob Wen 2021-11-06 23:27:06 +08:00
parent fe6c91fcc7
commit c532751be0
4 changed files with 27 additions and 13 deletions

View File

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

View File

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

5
delay.coffee Normal file
View File

@ -0,0 +1,5 @@
util = require 'util'
module.exports = util.promisify (ms, cb) ->
return cb() if ms <= 0
setTimeout cb, ms

View File

@ -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 = ''