-
Notifications
You must be signed in to change notification settings - Fork 25
/
arith.R
67 lines (51 loc) · 1.24 KB
/
arith.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
SPLIT_SECOND_DIGITS <- 6L
TICS_PER_SECOND <- 10^SPLIT_SECOND_DIGITS
SECONDS_PER_MINUTE <- 60
MINUTES_PER_HOUR <- 60
HOURS_PER_DAY <- 24
TICS_PER_MINUTE <- SECONDS_PER_MINUTE * TICS_PER_SECOND
TICS_PER_HOUR <- MINUTES_PER_HOUR * TICS_PER_MINUTE
TICS_PER_DAY <- HOURS_PER_DAY * TICS_PER_HOUR
days <- function(x) {
trunc(x / TICS_PER_DAY)
}
hours <- function(x) {
trunc(x / TICS_PER_HOUR)
}
hour_of_day <- function(x) {
abs(hours(x) - days(x) * HOURS_PER_DAY)
}
minutes <- function(x) {
trunc(x / TICS_PER_MINUTE)
}
minute_of_hour <- function(x) {
abs(minutes(x) - hours(x) * MINUTES_PER_HOUR)
}
seconds <- function(x) {
trunc(x / TICS_PER_SECOND)
}
second_of_minute <- function(x) {
abs(seconds(x) - minutes(x) * SECONDS_PER_MINUTE)
}
tics <- function(x) {
x
}
tic_of_second <- function(x) {
abs(tics(x) - seconds(x) * TICS_PER_SECOND)
}
decompose <- function(x) {
x <- vec_data(x) * TICS_PER_SECOND
# #140
xr <- round(x)
out <- list(
sign = xr < 0 & !is.na(xr),
hours = abs(hours(xr)),
minute_of_hour = minute_of_hour(xr),
second_of_minute = second_of_minute(xr),
tics = tic_of_second(xr)
)
# #140: Make sure zeros are printed
fake_zero <- (out$tics == 0) & (xr != x)
out$tics[fake_zero] <- 0.25
out
}