v

Зеркало из https://github.com/vlang/v
Форк
0
/
duration.v 
121 строка · 2.8 Кб
1
module time
2

3
// A lot of these are taken from the Go library.
4
pub type Duration = i64
5

6
pub const nanosecond = Duration(1)
7
pub const microsecond = Duration(1000 * nanosecond)
8
pub const millisecond = Duration(1000 * microsecond)
9
pub const second = Duration(1000 * millisecond)
10
pub const minute = Duration(60 * second)
11
pub const hour = Duration(60 * minute)
12
//	day         = Duration(24 * hour)
13
pub const infinite = Duration(i64(9223372036854775807))
14

15
// nanoseconds returns the duration as an integer number of nanoseconds.
16
pub fn (d Duration) nanoseconds() i64 {
17
	return i64(d)
18
}
19

20
// microseconds returns the duration as an integer number of microseconds.
21
pub fn (d Duration) microseconds() i64 {
22
	return i64(d) / microsecond
23
}
24

25
// milliseconds returns the duration as an integer number of milliseconds.
26
pub fn (d Duration) milliseconds() i64 {
27
	return i64(d) / millisecond
28
}
29

30
// The following functions return floating point numbers because it's common to
31
// consider all of them in sub-one intervals
32
// seconds returns the duration as a floating point number of seconds.
33
pub fn (d Duration) seconds() f64 {
34
	return f64(d) / f64(second)
35
}
36

37
// minutes returns the duration as a floating point number of minutes.
38
pub fn (d Duration) minutes() f64 {
39
	return f64(d) / f64(minute)
40
}
41

42
// hours returns the duration as a floating point number of hours.
43
pub fn (d Duration) hours() f64 {
44
	return f64(d) / f64(hour)
45
}
46

47
// days returns the duration as a floating point number of days.
48
pub fn (d Duration) days() f64 {
49
	return f64(d) / f64(hour * 24)
50
}
51

52
// str pretty prints the duration
53
//
54
// ```
55
// h:m:s      // 5:02:33
56
// m:s.mi<s>  // 2:33.015
57
// s.mi<s>    // 33.015s
58
// mi.mc<ms>  // 15.007ms
59
// mc.ns<ns>  // 7.234us
60
// ns<ns>     // 234ns
61
// ```
62
pub fn (d Duration) str() string {
63
	if d == infinite {
64
		return 'inf'
65
	}
66
	mut sign := ''
67
	mut t := i64(d)
68
	if t < 0 {
69
		sign = '-'
70
		t = -t
71
	}
72
	hr := t / hour
73
	t -= hr * hour
74
	min := t / minute
75
	t -= min * minute
76
	sec := t / second
77
	t -= sec * second
78
	ms := t / millisecond
79
	t -= ms * millisecond
80
	us := t / microsecond
81
	t -= us * microsecond
82
	ns := t
83

84
	return match true {
85
		hr > 0 { '${sign}${hr}:${min:02}:${sec:02}' }
86
		min > 0 { '${sign}${min}:${sec:02}.${ms:03}' }
87
		sec > 0 { '${sign}${sec}.${ms:03}s' }
88
		ms > 0 { '${sign}${ms}.${us:03}ms' }
89
		us > 0 { '${sign}${us}.${ns:03}us' }
90
		else { '${sign}${ns}ns' }
91
	}
92
}
93

94
// debug returns a detailed breakdown of the Duration, as: 'Duration: - 50days, 4h, 3m, 7s, 541ms, 78us, 9ns'
95
pub fn (d Duration) debug() string {
96
	mut res := []string{}
97
	mut x := i64(d)
98
	mut sign := ''
99
	if x < 0 {
100
		sign = '- '
101
		x = -x
102
	}
103
	for label, v in {
104
		'days': 24 * hour
105
		'h':    hour
106
		'm':    minute
107
		's':    second
108
		'ms':   millisecond
109
		'us':   microsecond
110
	} {
111
		if x > v {
112
			xx := x / v
113
			x = x % v
114
			res << xx.str() + label
115
		}
116
	}
117
	if x > 0 {
118
		res << '${x}ns'
119
	}
120
	return 'Duration: ${sign}${res.join(', ')}'
121
}
122

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

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

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

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