File: clock.R

package info (click to toggle)
r-cran-clock 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,856 kB
  • sloc: cpp: 19,564; sh: 17; makefile: 2
file content (276 lines) | stat: -rw-r--r-- 8,254 bytes parent folder | download
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(clock)
library(magrittr)

## -----------------------------------------------------------------------------
date_build(2019, 2, 1:5)

## ----error=TRUE---------------------------------------------------------------
try({
date_build(2019, 1:12, 31)
})

## -----------------------------------------------------------------------------
date_build(2019, 1:12, 31, invalid = "previous")

## -----------------------------------------------------------------------------
date_build(2019, 1:12, "last")

## -----------------------------------------------------------------------------
date_time_build(2019, 1:5, 1, 2, 30, zone = "America/New_York")

## ----error=TRUE---------------------------------------------------------------
try({
date_time_build(2019:2021, 3, 8, 2, 30, zone = "America/New_York")
})

## -----------------------------------------------------------------------------
zone <- "America/New_York"

date_time_build(2019:2021, 3, 8, 2, 30, zone = zone, nonexistent = "roll-forward")
date_time_build(2019:2021, 3, 8, 2, 30, zone = zone, nonexistent = "roll-backward")

## -----------------------------------------------------------------------------
date_parse("2019-01-05")

## -----------------------------------------------------------------------------
date_parse("January 5, 2020", format = "%B %d, %Y")

## -----------------------------------------------------------------------------
date_parse(
  "juillet 10, 2021", 
  format = "%B %d, %Y", 
  locale = clock_locale("fr")
)

## -----------------------------------------------------------------------------
x <- c("2020/1/5", "10-03-05", "2020/2/2")
formats <- c("%Y/%m/%d", "%y-%m-%d")

date_parse(x, format = formats)

## -----------------------------------------------------------------------------
date_time_parse("2020-01-01 01:02:03", "America/New_York")

## -----------------------------------------------------------------------------
before <- date_time_parse("2020-11-01 00:59:59", "America/New_York")

# First 1 o'clock
before + 1

# Second 1 o'clock
before + 1 + 3600

## ----error=TRUE---------------------------------------------------------------
try({
date_time_parse("2020-11-01 01:30:00", "America/New_York")
})

## -----------------------------------------------------------------------------
zone <- "America/New_York"

date_time_parse("2020-11-01 01:30:00", zone, ambiguous = "earliest")
date_time_parse("2020-11-01 01:30:00", zone, ambiguous = "latest")

## -----------------------------------------------------------------------------
x <- "2020-01-01T01:02:03-05:00[America/New_York]"

date_time_parse_complete(x)

## -----------------------------------------------------------------------------
x <- "2020-01-01 01:02:03 EST"

date_time_parse_abbrev(x, "America/New_York")

## -----------------------------------------------------------------------------
x <- c(
  "1970-10-25 01:30:00 EDT",
  "1970-10-25 01:30:00 EST"
)

date_time_parse_abbrev(x, "America/New_York")

## -----------------------------------------------------------------------------
x <- "1970-01-01 02:30:30 IST"

# IST = India Standard Time
date_time_parse_abbrev(x, "Asia/Kolkata")

# IST = Israel Standard Time
date_time_parse_abbrev(x, "Asia/Jerusalem")

## -----------------------------------------------------------------------------
x <- "2020-01-01T01:02:03Z"

date_time_parse_RFC_3339(x)

## -----------------------------------------------------------------------------
x <- "2020-01-01T01:02:03-0500"

date_time_parse_RFC_3339(x, offset = "%z")

x <- "2020-01-01T01:02:03-05:00"

date_time_parse_RFC_3339(x, offset = "%Ez")

## -----------------------------------------------------------------------------
x <- seq(date_build(2019, 1, 20), date_build(2019, 2, 5), by = 1)
x

# Grouping by 5 days of the current month
date_group(x, "day", n = 5)

## -----------------------------------------------------------------------------
date_group(x, "month")

## -----------------------------------------------------------------------------
x <- seq(
  date_time_build(2019, 1, 1, 1, 55, zone = "UTC"),
  date_time_build(2019, 1, 1, 2, 15, zone = "UTC"),
  by = 120
)
x

date_group(x, "minute", n = 5)

## -----------------------------------------------------------------------------
unclass(date_build(2020, 1, 1))

## -----------------------------------------------------------------------------
x <- seq(date_build(1970, 01, 01), date_build(1970, 05, 10), by = 20)

date_floor(x, "day", n = 60)
date_ceiling(x, "day", n = 60)

## -----------------------------------------------------------------------------
as_weekday(date_floor(x, "week", n = 14))

## -----------------------------------------------------------------------------
sunday <- date_build(1970, 01, 04)

date_floor(x, "week", n = 14, origin = sunday)

as_weekday(date_floor(x, "week", n = 14, origin = sunday))

## -----------------------------------------------------------------------------
x <- date_build(2020, 1, 1:2)

# Wednesday / Thursday
as_weekday(x)

# `clock_weekdays` is a helper that returns the code corresponding to
# the requested day of the week
clock_weekdays$tuesday

tuesday <- weekday(clock_weekdays$tuesday)
tuesday

date_shift(x, target = tuesday)

## -----------------------------------------------------------------------------
x <- seq(date_build(1970, 01, 01), date_build(1970, 01, "last"), by = 3)

date_shift(x, tuesday, which = "previous")

## -----------------------------------------------------------------------------
x <- date_build(2020, 1, 1)

add_years(x, 1:5)

## ----error=TRUE---------------------------------------------------------------
try({
x <- date_build(2020, 1, 31)

add_months(x, 1)
})

## -----------------------------------------------------------------------------
# The previous valid moment in time
add_months(x, 1, invalid = "previous")

# The next valid moment in time
add_months(x, 1, invalid = "next")

# Overflow the days. There were 29 days in February, 2020, but we
# specified 31. So this overflows 2 days past day 29.
add_months(x, 1, invalid = "overflow")

# If you don't consider it to be a valid date
add_months(x, 1, invalid = "NA")

## -----------------------------------------------------------------------------
ymd <- as_year_month_day(x) + duration_months(1)
ymd

## -----------------------------------------------------------------------------
# Adding 1 more month makes it valid again
ymd + duration_months(1)

## -----------------------------------------------------------------------------
x <- date_time_build(2020, 1, 1, 2, 30, zone = "America/New_York")

x %>%
  add_days(1) %>%
  add_hours(2:5)

## ----error=TRUE---------------------------------------------------------------
try({
x <- date_time_build(1970, 04, 25, 02, 30, 00, zone = "America/New_York")
x

# Daylight saving time gap on the 26th between 01:59:59 -> 03:00:00
x %>% add_days(1)
})

## -----------------------------------------------------------------------------
# Roll forward to the next valid moment in time
x %>% add_days(1, nonexistent = "roll-forward")

# Roll backward to the previous valid moment in time
x %>% add_days(1, nonexistent = "roll-backward")

# Shift forward by adding the size of the DST gap
# (this often keeps the time of day,
# but doesn't guaratee that relative ordering in `x` is maintained
# so I don't recommend it)
x %>% add_days(1, nonexistent = "shift-forward")

# Replace nonexistent times with an NA
x %>% add_days(1, nonexistent = "NA")

## -----------------------------------------------------------------------------
x <- date_build(2019, 5, 6)

get_year(x)
get_month(x)
get_day(x)

x %>%
  set_day(22) %>%
  set_month(10)

## ----error=TRUE---------------------------------------------------------------
try({
x %>%
  set_day(31) %>%
  set_month(4)

x %>%
  set_day(31) %>%
  set_month(4, invalid = "previous")
})

## -----------------------------------------------------------------------------
x <- date_time_build(2020, 1, 2, 3, zone = "America/New_York")
x

x %>%
  set_minute(5) %>%
  set_second(10)