win 0.3.25 → 0.3.26
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +4 -0
- data/VERSION +1 -1
- data/lib/win/time.rb +18 -1
- data/spec/win/time_spec.rb +32 -1
- metadata +1 -1
data/HISTORY
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.26
|
data/lib/win/time.rb
CHANGED
@@ -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
|
data/spec/win/time_spec.rb
CHANGED
@@ -34,7 +34,7 @@ describe Win::Time do
|
|
34
34
|
count.should be > 500000000000
|
35
35
|
end
|
36
36
|
|
37
|
-
it "
|
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
|