v

Зеркало из https://github.com/vlang/v
Форк
0
/
time_windows.c.v 
229 строк · 5.4 Кб
1
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2
// Use of this source code is governed by an MIT license
3
// that can be found in the LICENSE file.
4
module time
5

6
#include <time.h>
7
// #include <sysinfoapi.h>
8

9
pub struct C.tm {
10
pub mut:
11
	tm_year int
12
	tm_mon  int
13
	tm_mday int
14
	tm_hour int
15
	tm_min  int
16
	tm_sec  int
17
}
18

19
pub struct C._FILETIME {
20
pub mut:
21
	dwLowDateTime  u32
22
	dwHighDateTime u32
23
}
24

25
struct SystemTime {
26
	year        u16
27
	month       u16
28
	day_of_week u16
29
	day         u16
30
	hour        u16
31
	minute      u16
32
	second      u16
33
	millisecond u16
34
}
35

36
fn C.GetSystemTimeAsFileTime(lpSystemTimeAsFileTime &C._FILETIME)
37

38
fn C.FileTimeToSystemTime(lpFileTime &C._FILETIME, lpSystemTime &SystemTime)
39

40
fn C.SystemTimeToTzSpecificLocalTime(lpTimeZoneInformation &C.TIME_ZONE_INFORMATION, lpUniversalTime &SystemTime,
41
	lpLocalTime &SystemTime)
42

43
fn C.localtime_s(t &C.time_t, tm &C.tm)
44

45
fn C.timespec_get(t &C.timespec, base int) int
46

47
// start_time is needed on Darwin and Windows because of potential overflows
48
const start_time = init_win_time_start()
49
const freq_time = init_win_time_freq()
50
const start_local_time = local_as_unix_time()
51

52
// in most systems, these are __quad_t, which is an i64
53
pub struct C.timespec {
54
pub:
55
	tv_sec  i64
56
	tv_nsec i64
57
}
58

59
fn C.QueryPerformanceCounter(&u64) C.BOOL
60

61
fn C.QueryPerformanceFrequency(&u64) C.BOOL
62

63
fn make_unix_time(t C.tm) i64 {
64
	return portable_timegm(&t)
65
}
66

67
fn init_win_time_freq() u64 {
68
	f := u64(0)
69
	C.QueryPerformanceFrequency(voidptr(&f))
70
	return f
71
}
72

73
fn init_win_time_start() u64 {
74
	s := u64(0)
75
	C.QueryPerformanceCounter(voidptr(&s))
76
	return s
77
}
78

79
// sys_mono_now returns a *monotonically increasing time*, NOT a time adjusted for daylight savings, location etc.
80
pub fn sys_mono_now() u64 {
81
	tm := u64(0)
82
	C.QueryPerformanceCounter(voidptr(&tm)) // XP or later never fail
83
	return (tm - start_time) * 1000000000 / freq_time
84
}
85

86
// Note: vpc_now is used by `v -profile` .
87
// It should NOT call *any other v function*, just C functions and casts.
88
@[inline]
89
fn vpc_now() u64 {
90
	tm := u64(0)
91
	C.QueryPerformanceCounter(voidptr(&tm))
92
	return tm
93
}
94

95
// local_as_unix_time returns the current local time as unix time
96
fn local_as_unix_time() i64 {
97
	t := C.time(0)
98
	tm := C.localtime(&t)
99
	return make_unix_time(tm)
100
}
101

102
// local - return the time `t`, converted to the currently active local timezone
103
pub fn (t Time) local() Time {
104
	if t.is_local {
105
		return t
106
	}
107
	st_utc := SystemTime{
108
		year:        u16(t.year)
109
		month:       u16(t.month)
110
		day:         u16(t.day)
111
		hour:        u16(t.hour)
112
		minute:      u16(t.minute)
113
		second:      u16(t.second)
114
		millisecond: u16(t.nanosecond / 1_000_000)
115
	}
116
	st_local := SystemTime{}
117
	C.SystemTimeToTzSpecificLocalTime(unsafe { nil }, voidptr(&st_utc), voidptr(&st_local))
118
	t_local := Time{
119
		year:       st_local.year
120
		month:      st_local.month
121
		day:        st_local.day
122
		hour:       st_local.hour
123
		minute:     st_local.minute
124
		second:     st_local.second // These are the same
125
		nanosecond: int(st_local.millisecond) * 1_000_000
126
		unix:       st_local.unix()
127
	}
128
	return t_local
129
}
130

131
// win_now calculates current time using winapi to get higher resolution on windows
132
// GetSystemTimeAsFileTime is used and converted to local time. It can resolve time
133
// down to millisecond. Other more precise methods can be implemented in the future
134
fn win_now() Time {
135
	ft_utc := C._FILETIME{}
136
	C.GetSystemTimeAsFileTime(&ft_utc)
137
	st_utc := SystemTime{}
138
	C.FileTimeToSystemTime(&ft_utc, voidptr(&st_utc))
139
	st_local := SystemTime{}
140
	C.SystemTimeToTzSpecificLocalTime(unsafe { nil }, voidptr(&st_utc), voidptr(&st_local))
141
	t := Time{
142
		year:       st_local.year
143
		month:      st_local.month
144
		day:        st_local.day
145
		hour:       st_local.hour
146
		minute:     st_local.minute
147
		second:     st_local.second
148
		nanosecond: int(st_local.millisecond) * 1_000_000
149
		unix:       st_local.unix()
150
		is_local:   true
151
	}
152
	return t
153
}
154

155
// win_utc calculates current time using winapi to get higher resolution on windows
156
// GetSystemTimeAsFileTime is used. It can resolve time down to millisecond
157
// other more precise methods can be implemented in the future
158
fn win_utc() Time {
159
	ft_utc := C._FILETIME{}
160
	C.GetSystemTimeAsFileTime(&ft_utc)
161
	st_utc := SystemTime{}
162
	C.FileTimeToSystemTime(&ft_utc, voidptr(&st_utc))
163
	t := Time{
164
		year:       st_utc.year
165
		month:      st_utc.month
166
		day:        st_utc.day
167
		hour:       st_utc.hour
168
		minute:     st_utc.minute
169
		second:     st_utc.second
170
		nanosecond: int(st_utc.millisecond) * 1_000_000
171
		unix:       st_utc.unix()
172
		is_local:   false
173
	}
174
	return t
175
}
176

177
// unix_time returns Unix time.
178
@[deprecated: 'use `st.unix()` instead']
179
fn (st SystemTime) unix_time() i64 {
180
	return st.unix()
181
}
182

183
// unix returns Unix time.
184
fn (st SystemTime) unix() i64 {
185
	tt := C.tm{
186
		tm_sec:  st.second
187
		tm_min:  st.minute
188
		tm_hour: st.hour
189
		tm_mday: st.day
190
		tm_mon:  st.month - 1
191
		tm_year: st.year - 1900
192
	}
193
	return make_unix_time(tt)
194
}
195

196
// dummy to compile with all compilers
197
fn darwin_now() Time {
198
	return Time{}
199
}
200

201
// dummy to compile with all compilers
202
fn linux_now() Time {
203
	return Time{}
204
}
205

206
// dummy to compile with all compilers
207
fn solaris_now() Time {
208
	return Time{}
209
}
210

211
// dummy to compile with all compilers
212
fn darwin_utc() Time {
213
	return Time{}
214
}
215

216
// dummy to compile with all compilers
217
fn linux_utc() Time {
218
	return Time{}
219
}
220

221
// dummy to compile with all compilers
222
fn solaris_utc() Time {
223
	return Time{}
224
}
225

226
// sleep makes the calling thread sleep for a given duration (in nanoseconds).
227
pub fn sleep(duration Duration) {
228
	C.Sleep(int(duration / millisecond))
229
}
230

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.