Advertisement
quark_zju

set_time.rb

Sep 30th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.87 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'time'
  4.  
  5. # Set system time. If the new time is in the past, "Freeze" system time for some time.
  6. # Useful when your system time is ahead of actural time and you cannot adjust time to the past directly.
  7. #
  8. # Example:
  9. #
  10. #   $0 '2013-10-1 9:30:00'  
  11.  
  12. # set date needs root
  13. raise "root required" if Process.uid != 0
  14. raise "Please provide some time such as '2013-10-1 9:30:00'" if ARGV.empty?
  15.  
  16. expected = Time.parse(ARGV.join(' '))
  17. actural = Time.now
  18.  
  19. def set_date(date)
  20.   system "date #{date.strftime('%m%d%H%M%Y.%S')} >/dev/null"
  21. end
  22.  
  23. seconds = actural - expected
  24. if seconds <= 0
  25.   puts "Fast forward to #{expected}"
  26.   set_date expected
  27. else
  28.   print "Freeze system time for #{seconds} seconds."
  29.   (seconds * 2).to_i.times do |i|
  30.     Thread.new { set_date actural }
  31.     sleep 0.5
  32.     print '.' if i % 4 == 0
  33.   end
  34.   puts ' Done.'
  35. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement