win 0.3.25 → 0.3.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/HISTORY +4 -0
  2. data/VERSION +1 -1
  3. data/lib/win/time.rb +18 -1
  4. data/spec/win/time_spec.rb +32 -1
  5. metadata +1 -1
data/HISTORY CHANGED
@@ -94,3 +94,7 @@
94
94
  == 0.3.25 / 2010-10-17
95
95
 
96
96
  * Minor refactoring
97
+
98
+ == 0.3.26 / 2011-03-14
99
+
100
+ * Win::Time.now returns enhanced-precision Time value with granularity below microsecond
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.25
1
+ 0.3.26
@@ -10,7 +10,8 @@ module Win
10
10
  # The caller of these functions should not rely on more than second granularity.
11
11
  # In fact, granularity of all the GetTime... functions is about ~16msec on Windows.
12
12
  # You'll need to mess with QueryPerformanceFrequency() and QueryPerformanceCounter()
13
- # to get real msec granularity on MS Windows;
13
+ # to get real msec granularity on MS Windows; OR, just use Win::Time.now() for
14
+ # enhanced-precision Time values
14
15
  #
15
16
  module Time
16
17
  extend Win::Library
@@ -136,5 +137,21 @@ module Win
136
137
  try_function :TzSpecificLocalTimeToSystemTime, 'PPP', :int8, boolean: true
137
138
 
138
139
 
140
+ # The problem: granularity of standard Time.now on Windows is about ~16 milliseconds!
141
+ # Here we try to add enhanced-precision Time support on Windows.
142
+ # Use it like this: t = Win::Time.now
143
+ # It returns enhanced-precision Time value with granularity below microsecond.
144
+ begin
145
+ COUNTER_FREQUENCY = query_performance_frequency()
146
+ INITIAL_COUNTER = query_performance_counter()
147
+ INITIAL_TIME = ::Time.now
148
+
149
+ # Returns enhanced-precision Time value with granularity below microsecond.
150
+ def self.now
151
+ INITIAL_TIME + (query_performance_counter()-INITIAL_COUNTER).to_f/COUNTER_FREQUENCY
152
+ end
153
+ rescue Exception
154
+ raise "Unable to initiate enhanced-precision timer"
155
+ end
139
156
  end
140
157
  end
@@ -34,7 +34,7 @@ describe Win::Time do
34
34
  count.should be > 500000000000
35
35
  end
36
36
 
37
- it "successive function calls return (slightly) incremented counter values" do
37
+ it "return (slightly) incremented counter values in successive function calls" do
38
38
  100.times do
39
39
  count1 = query_performance_counter()
40
40
  count2 = query_performance_counter()
@@ -43,6 +43,37 @@ describe Win::Time do
43
43
  diff.should be < 50000 # GC calls make it hard to guarantee uniform measurements?
44
44
  end
45
45
  end
46
+
47
+ it 'returns correct counter counts' do
48
+ count1 = query_performance_counter()
49
+ sleep 0.3
50
+ count2 = query_performance_counter()
51
+ seconds_passed = (count2 - count1).to_f/query_performance_frequency()
52
+ seconds_passed.should be_within(0.02).of(0.3)
53
+ end
46
54
  end # describe query_performance_counter
47
55
 
56
+ describe '.now' do
57
+ it 'returns enhanced-precision Time on Windows platform' do
58
+ time = Win::Time.now
59
+ time.should be_a_kind_of ::Time
60
+ end
61
+
62
+ it 'is really really enhanced precision' do
63
+ normal_moved = 0
64
+ hi_res_moved = 0
65
+ 100.times do
66
+ normal = ::Time.now
67
+ hi_res = Win::Time.now
68
+ # puts "Normal Time.now: #{normal.strftime('%Y-%m-%d %H:%M:%S.%6N')}," +
69
+ # "Win::Time.now: #{hi_res.strftime('%Y-%m-%d %H:%M:%S.%6N')} "
70
+ normal1 = ::Time.now
71
+ hi_res1 = Win::Time.now
72
+ normal_moved += 1 if normal1 != normal
73
+ hi_res_moved += 1 if hi_res1 != hi_res
74
+ end
75
+ normal_moved.should be < 5
76
+ hi_res_moved.should == 100
77
+ end
78
+ end
48
79
  end # describe Win::Time
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: win
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.25
5
+ version: 0.3.26
6
6
  platform: ruby
7
7
  authors:
8
8
  - arvicco