Skip to content

Instantly share code, notes, and snippets.

@Slackwise
Created September 25, 2026 18:55
Show Gist options
  • Select an option

  • Save Slackwise/af295be5bff4cd1e73b964f02409543c to your computer and use it in GitHub Desktop.

Select an option

Save Slackwise/af295be5bff4cd1e73b964f02409543c to your computer and use it in GitHub Desktop.
Old Ruby script to force Windows NTP time to sync (was intended for Windows XP)
# Whenever I wake up my XP virtual machine from sleep, the time is off.
# This throws off a number of scripts and apps that I run off it, so I
# have to sync the time. Unfortunately, XP requries you to have the date
# match in order to sync the time. Originally this script set the date,
# then called the w32tm utilty to sync the time, but it would take too long
# to process, and sometimes time out. Instead, I just decided to say 'fsck it'
# and used the Ruby NTP library fully. The time is maybe off by a second at
# the very most, which is good enough.
require 'net/ntp'
# XP's date command expects MM/DD/YYYY, e.g. 5/15/2009
DATE_FORMAT = '%m/%d/%Y'
# XP's time command expects HH:MM:SS AM/PM, e.g. 5:34:00 PM
TIME_FORMAT = '%I:%M:%S %p'
begin
datetime = Net::NTP.get.time
rescue
puts "Failed to get NTP time information: #$!"
gets # Pause so you can read output.
exit 1
end
system("date #{datetime.strftime(DATE_FORMAT)}")
system("time #{datetime.strftime(TIME_FORMAT)}")
__END__
until system("w32tm /resync /rediscover")
sleep 10
end
puts 'Forced time sync completed.'
puts 'Press ENTER to close window.'
gets # Pause so you can read the output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment