Skip to content

Commit 03f07ea

Browse files
timbookellisvalentiner
authored andcommitted
Added *_ago and *_hence functions (#12)
* added weeks, months, years_ago functions * added minutes, seconds, hours_ago functions * added weeks, months, years_hence
1 parent 33c67bb commit 03f07ea

File tree

1 file changed

+186
-1
lines changed

1 file changed

+186
-1
lines changed

R/instants.R

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,51 @@ tomorrow <- function(tzone = "") {
3232
as_date(now(tzone = tzone) + days(1))
3333
}
3434

35+
#' The time x seconds before now
36+
#'
37+
#' @param seconds integer number of seconds
38+
#' @param tzone a character vector specifying which time zone you would like to
39+
#' find the previous date of. tzone defaults to the system time zone set on
40+
#' your computer.
41+
#'
42+
#' @return The datetime, x seconds ago
43+
#' @export seconds_ago
44+
#'
45+
#' @examples
46+
seconds_ago <- function(seconds = 0, tzone = "") {
47+
as_datetime(now() - seconds(x = seconds), tz = tzone)
48+
}
49+
50+
#' The time x minutes before now
51+
#'
52+
#' @param minutes integer number of minutes
53+
#' @param tzone a character vector specifying which time zone you would like to
54+
#' find the previous date of. tzone defaults to the system time zone set on
55+
#' your computer.
56+
#'
57+
#' @return
58+
#' @export minutes_ago
59+
#'
60+
#' @examples
61+
minutes_ago <- function(minutes = 0, tzone = "") {
62+
as_datetime(now() - minutes(x = minutes), tz = tzone)
63+
}
64+
65+
#' the time x hours ago
66+
#'
67+
#' @param hours integer number of hours
68+
#' @param tzone a character vector specifying which time zone you would like to
69+
#' find the previous date of. tzone defaults to the system time zone set on
70+
#' your computer.
71+
#'
72+
#' @return
73+
#' @export hours_ago
74+
#'
75+
#' @examples
76+
hours_ago <- function(hours = 0, tzone = "") {
77+
as_datetime(now() - hours(x = hours), tz = tzone)
78+
}
79+
3580
#' The date x days ago
3681
#'
3782
#' @export days_ago
@@ -49,14 +94,106 @@ days_ago <- function(days = 0, tzone = "") {
4994
as_date(today(tzone = tzone) - days(x = days))
5095
}
5196

97+
#' The date x weeks ago
98+
#'
99+
#' @param weeks an integer specifying the number of weeks to subtract from the
100+
#' current
101+
#' @param tzone a character vector specifying which time zone you would like to
102+
#' find the previous date of. tzone defaults to the system time zone set on
103+
#' your computer.
104+
#'
105+
#' @return the date, x weeks ago
106+
#' @export weeks_ago
107+
#'
108+
#' @examples
109+
weeks_ago <- function(weeks = 0, tzone = "") {
110+
as_date(today(tzone = tzone) - weeks(x = weeks))
111+
}
112+
113+
114+
#' The date x months ago
115+
#'
116+
#' @param months an integer specifying th enumber of weeks to subtract from the current
117+
#' @param tzone a character vector specifying which time zone you would like to
118+
#' find the previous date of. tzone defaults to the system time zone set on
119+
#' your computer.
120+
#'
121+
#' @return the date, x months ago
122+
#' @export months_ago
123+
#'
124+
#' @examples
125+
months_ago <- function(months = 0, tzone = "") {
126+
as_date(today(tzone = tzone) - months(x = months))
127+
}
128+
129+
#' The date x years ago
130+
#'
131+
#' @param years an integer specifying the number of days to subtract from the
132+
#' current
133+
#' @param tzone a character vector specifying which time zone you would like to
134+
#' find the previous date of. tzone defaults to the system time zone set on
135+
#' your computer.
136+
#'
137+
#' @return The date, x years ago
138+
#' @export years_ago
139+
#'
140+
#' @examples
141+
years_ago <- function(years = 0, tzone = "") {
142+
as_date(today(tzone = tzone) - years(x = years))
143+
}
144+
145+
#' The time x seconds before now
146+
#'
147+
#' @param seconds integer number of seconds
148+
#' @param tzone a character vector specifying which time zone you would like to
149+
#' find the previous date of. tzone defaults to the system time zone set on
150+
#' your computer.
151+
#'
152+
#' @return The datetime, x seconds from now
153+
#' @export seconds_hence
154+
#'
155+
#' @examples
156+
seconds_hence <- function(seconds = 0, tzone = "") {
157+
as_datetime(now() + seconds(x = seconds), tz = tzone)
158+
}
159+
160+
#' The time x minutes before now
161+
#'
162+
#' @param minutes integer number of minutes
163+
#' @param tzone a character vector specifying which time zone you would like to
164+
#' find the previous date of. tzone defaults to the system time zone set on
165+
#' your computer.
166+
#'
167+
#' @return The date time, x minutes from now
168+
#' @export minutes_hence
169+
#'
170+
#' @examples
171+
minutes_hence <- function(minutes = 0, tzone = "") {
172+
as_datetime(now() + minutes(x = minutes), tz = tzone)
173+
}
174+
175+
#' the time x hours hence
176+
#'
177+
#' @param hours integer number of hours
178+
#' @param tzone a character vector specifying which time zone you would like to
179+
#' find the previous date of. tzone defaults to the system time zone set on
180+
#' your computer.
181+
#'
182+
#' @return The datetime, x hours from now
183+
#' @export hours_hence
184+
#'
185+
#' @examples
186+
hours_hence <- function(hours = 0, tzone = "") {
187+
as_datetime(now() + hours(x = hours), tz = tzone)
188+
}
52189
#' The date x days hence
53190
#'
54191
#' @export days_hence
55192
#' @param days an integer specifying the number of days to add from the current
56193
#' @param tzone a character vector specifying which time zone you would like to
57194
#' find the previous date of. tzone defaults to the system time zone set on
58195
#' your computer.
59-
#' @return the date, x days hence
196+
#' @return the datetime, x days from now
60197
#'
61198
#' @examples
62199
#' days_hence(3)
@@ -76,6 +213,54 @@ this_month <- function(tzone = "") {
76213
floor_date(x = today(tzone = tzone), unit = "month")
77214
}
78215

216+
#' The date x weeks hence
217+
#'
218+
#' @param weeks an integer specifying the number of weeks to add to the
219+
#' current
220+
#' @param tzone a character vector specifying which time zone you would like to
221+
#' find the previous date of. tzone defaults to the system time zone set on
222+
#' your computer.
223+
#'
224+
#' @return the date, x weeks from now
225+
#' @export weeks_hence
226+
#'
227+
#' @examples
228+
weeks_hence <- function(weeks = 0, tzone = "") {
229+
as_date(today(tzone = tzone) + weeks(x = weeks))
230+
}
231+
232+
233+
#' The date x months hence
234+
#'
235+
#' @param months an integer specifying the number of weeks to add to the current
236+
#' @param tzone a character vector specifying which time zone you would like to
237+
#' find the previous date of. tzone defaults to the system time zone set on
238+
#' your computer.
239+
#'
240+
#' @return the date, x months from now
241+
#' @export months_hence
242+
#'
243+
#' @examples
244+
months_hence <- function(months = 0, tzone = "") {
245+
as_date(today(tzone = tzone) + months(x = months))
246+
}
247+
248+
#' The date x years hence
249+
#'
250+
#' @param years an integer specifying the number of days to add to the
251+
#' current
252+
#' @param tzone a character vector specifying which time zone you would like to
253+
#' find the previous date of. tzone defaults to the system time zone set on
254+
#' your computer.
255+
#'
256+
#' @return The date, x years form now
257+
#' @export years_hence
258+
#'
259+
#' @examples
260+
years_hence <- function(years = 0, tzone = "") {
261+
as_date(today(tzone = tzone) + years(x = years))
262+
}
263+
79264
#' The previous month
80265
#'
81266
#' @export last_month

0 commit comments

Comments
 (0)