Skip to content

Commit a50e7fb

Browse files
authored
Merge pull request #17 from olivroy/testing2
A tiny performance improvement
2 parents f2e43e3 + 3fb2d71 commit a50e7fb

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

.Rbuildignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ man/figures/
1414
tests/testthat/_snaps
1515
tests/testthat/test-fdf_ftf.R
1616
tests/testthat/test-standard_date_time.R
17+
^\.covrignore$

.covrignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R/days_months.R

R/dt_replace.R

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
77
# This means we can use sub() instead of gsub()
88

99
if ("G" %in% dt_lett) {
10+
# TODO extract the exact substitution to do to avoid problematic
1011
dt <- sub("{G}", dt_G(input_dt, locale), dt, fixed = TRUE)
1112
dt <- sub("{GG}", dt_G(input_dt, locale), dt, fixed = TRUE)
1213
dt <- sub("{GGG}", dt_G(input_dt, locale), dt, fixed = TRUE)
@@ -34,6 +35,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
3435
}
3536

3637
if ("U" %in% dt_lett) {
38+
# TODO extract the exact substitution to do to avoid problematic
3739
dt <- sub("{U}", dt_U(input_dt), dt, fixed = TRUE)
3840
dt <- sub("{UU}", dt_U(input_dt), dt, fixed = TRUE)
3941
dt <- sub("{UUU}", dt_U(input_dt), dt, fixed = TRUE)
@@ -47,6 +49,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
4749
}
4850

4951
if ("q" %in% dt_lett) {
52+
# TODO extract the exact substitution to do to avoid problematic
5053
dt <- sub("{q}", dt_q(input_dt), dt, fixed = TRUE)
5154
dt <- sub("{qq}", dt_qq(input_dt), dt, fixed = TRUE)
5255
dt <- sub("{qqq}", dt_qqq(input_dt, locale), dt, fixed = TRUE)
@@ -64,6 +67,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
6467
}
6568

6669
if ("L" %in% dt_lett) {
70+
# TODO extract the exact substitution to do to avoid problematic
6771
dt <- sub("{L}", dt_L(input_dt), dt, fixed = TRUE)
6872
dt <- sub("{LL}", dt_LL(input_dt), dt, fixed = TRUE)
6973
dt <- sub("{LLL}", dt_LLL(input_dt, locale), dt, fixed = TRUE)
@@ -72,6 +76,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
7276
}
7377

7478
if ("w" %in% dt_lett) {
79+
# TODO extract the exact substitution to do to avoid problematic
7580
dt <- sub("{w}", dt_w(input_dt), dt, fixed = TRUE)
7681
dt <- sub("{ww}", dt_ww(input_dt), dt, fixed = TRUE)
7782
}
@@ -95,6 +100,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
95100
}
96101

97102
if ("E" %in% dt_lett) {
103+
# TODO extract the exact substitution to do to avoid problematic
98104
dt <- sub("{E}", dt_E(input_dt, locale), dt, fixed = TRUE)
99105
dt <- sub("{EE}", dt_E(input_dt, locale), dt, fixed = TRUE)
100106
dt <- sub("{EEE}", dt_E(input_dt, locale), dt, fixed = TRUE)
@@ -104,6 +110,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
104110
}
105111

106112
if ("e" %in% dt_lett) {
113+
# TODO extract the exact substitution to do to avoid problematic
107114
dt <- sub("{e}", dt_e(input_dt, locale), dt, fixed = TRUE)
108115
dt <- sub("{ee}", dt_ee(input_dt, locale), dt, fixed = TRUE)
109116
dt <- sub("{eee}", dt_eee(input_dt, locale), dt, fixed = TRUE)
@@ -113,6 +120,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
113120
}
114121

115122
if ("c" %in% dt_lett) {
123+
# TODO extract the exact substitution to do to avoid problematic
116124
dt <- sub("{c}", dt_c(input_dt, locale), dt, fixed = TRUE)
117125
dt <- sub("{cc}", dt_cc(input_dt, locale), dt, fixed = TRUE)
118126
dt <- sub("{ccc}", dt_ccc(input_dt, locale), dt, fixed = TRUE)
@@ -122,6 +130,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
122130
}
123131

124132
if ("a" %in% dt_lett) {
133+
# TODO extract the exact substitution to do to avoid problematic
125134
dt <- sub("{a}", dt_a(input_dt, locale), dt, fixed = TRUE)
126135
dt <- sub("{aa}", dt_a(input_dt, locale), dt, fixed = TRUE)
127136
dt <- sub("{aaa}", dt_a(input_dt, locale), dt, fixed = TRUE)
@@ -130,6 +139,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
130139
}
131140

132141
if ("b" %in% dt_lett) {
142+
# TODO extract the exact substitution to do to avoid problematic
133143
dt <- sub("{b}", dt_b(input_dt, locale), dt, fixed = TRUE)
134144
dt <- sub("{bb}", dt_b(input_dt, locale), dt, fixed = TRUE)
135145
dt <- sub("{bbb}", dt_b(input_dt, locale), dt, fixed = TRUE)
@@ -138,6 +148,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
138148
}
139149

140150
if ("B" %in% dt_lett) {
151+
# TODO extract the exact substitution to do to avoid problematic
141152
dt <- sub("{B}", dt_B(input_dt, locale), dt, fixed = TRUE)
142153
dt <- sub("{BB}", dt_B(input_dt, locale), dt, fixed = TRUE)
143154
dt <- sub("{BBB}", dt_B(input_dt, locale), dt, fixed = TRUE)
@@ -146,6 +157,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
146157
}
147158

148159
if ("h" %in% dt_lett) {
160+
# TODO extract the exact substitution to do to avoid problematic
149161
dt <- sub("{h}", dt_h(input_dt), dt, fixed = TRUE)
150162
dt <- sub("{hh}", dt_hh(input_dt), dt, fixed = TRUE)
151163
}
@@ -159,26 +171,31 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
159171
}
160172

161173
if ("K" %in% dt_lett) {
174+
# TODO extract the exact substitution to do to avoid problematic
162175
dt <- sub("{K}", dt_K(input_dt), dt, fixed = TRUE)
163176
dt <- sub("{KK}", dt_KK(input_dt), dt, fixed = TRUE)
164177
}
165178

166179
if ("k" %in% dt_lett) {
180+
# TODO extract the exact substitution to do to avoid problematic
167181
dt <- sub("{k}", dt_k(input_dt), dt, fixed = TRUE)
168182
dt <- sub("{kk}", dt_kk(input_dt), dt, fixed = TRUE)
169183
}
170184

171185
if ("m" %in% dt_lett) {
186+
# TODO extract the exact substitution to do to avoid problematic
172187
dt <- sub("{m}", dt_m(input_dt), dt, fixed = TRUE)
173188
dt <- sub("{mm}", dt_mm(input_dt), dt, fixed = TRUE)
174189
}
175190

176191
if ("s" %in% dt_lett) {
192+
# TODO extract the exact substitution to do to avoid problematic
177193
dt <- sub("{s}", dt_s(input_dt), dt, fixed = TRUE)
178194
dt <- sub("{ss}", dt_ss(input_dt), dt, fixed = TRUE)
179195
}
180196

181197
if ("S" %in% dt_lett) {
198+
# TODO extract the exact substitution to do to avoid problematic
182199
dt <- sub("{S}", dt_S(input_dt), dt, fixed = TRUE)
183200
dt <- sub("{SS}", dt_SS(input_dt), dt, fixed = TRUE)
184201
dt <- sub("{SSS}", dt_SSS(input_dt), dt, fixed = TRUE)
@@ -191,6 +208,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
191208
}
192209

193210
if ("A" %in% dt_lett) {
211+
# TODO extract the exact substitution to do to avoid problematic
194212
dt <- sub("{A}", dt_A(input_dt), dt, fixed = TRUE)
195213
dt <- sub("{AA}", dt_AA(input_dt), dt, fixed = TRUE)
196214
dt <- sub("{AAA}", dt_AAA(input_dt), dt, fixed = TRUE)
@@ -203,13 +221,15 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
203221
}
204222

205223
if ("z" %in% dt_lett) {
224+
# TODO extract the exact substitution to do to avoid problematic
206225
dt <- sub("{z}", dt_z(input_dt, tz_info, locale), dt, fixed = TRUE)
207226
dt <- sub("{zz}", dt_z(input_dt, tz_info, locale), dt, fixed = TRUE)
208227
dt <- sub("{zzz}", dt_z(input_dt, tz_info, locale), dt, fixed = TRUE)
209228
dt <- sub("{zzzz}", dt_zzzz(input_dt, tz_info, locale), dt, fixed = TRUE)
210229
}
211230

212231
if ("Z" %in% dt_lett) {
232+
# TODO extract the exact substitution to do to avoid problematic
213233
dt <- sub("{Z}", dt_Z(input_dt, tz_info, locale), dt, fixed = TRUE)
214234
dt <- sub("{ZZ}", dt_Z(input_dt, tz_info, locale), dt, fixed = TRUE)
215235
dt <- sub("{ZZZ}", dt_Z(input_dt, tz_info, locale), dt, fixed = TRUE)
@@ -228,13 +248,15 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
228248
}
229249

230250
if ("V" %in% dt_lett) {
251+
# TODO extract the exact substitution to do to avoid problematic
231252
dt <- sub("{V}", dt_V(input_dt, tz_info, locale), dt, fixed = TRUE)
232253
dt <- sub("{VV}", dt_VV(input_dt, tz_info, locale), dt, fixed = TRUE)
233254
dt <- sub("{VVV}", dt_VVV(input_dt, tz_info, locale), dt, fixed = TRUE)
234255
dt <- sub("{VVVV}", dt_VVVV(input_dt, tz_info, locale), dt, fixed = TRUE)
235256
}
236257

237258
if ("X" %in% dt_lett) {
259+
# TODO extract the exact substitution to do to avoid problematic
238260
dt <- sub("{X}", dt_X(input_dt, tz_info, locale), dt, fixed = TRUE)
239261
dt <- sub("{XX}", dt_XX(input_dt, tz_info, locale), dt, fixed = TRUE)
240262
dt <- sub("{XXX}", dt_XXX(input_dt, tz_info, locale), dt, fixed = TRUE)
@@ -243,6 +265,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
243265
}
244266

245267
if ("x" %in% dt_lett) {
268+
# TODO extract the exact substitution to do to avoid problematic
246269
dt <- sub("{x}", dt_x(input_dt, tz_info, locale), dt, fixed = TRUE)
247270
dt <- sub("{xx}", dt_xx(input_dt, tz_info, locale), dt, fixed = TRUE)
248271
dt <- sub("{xxx}", dt_xxx(input_dt, tz_info, locale), dt, fixed = TRUE)
@@ -251,6 +274,7 @@ dt_replace <- function(dt, input_dt, dt_lett, locale, tz_info) {
251274
}
252275

253276
if ("g" %in% dt_lett) {
277+
# TODO extract the exact substitution to do to avoid problematic
254278
dt <- sub("{g}", dt_g(input_dt), dt, fixed = TRUE)
255279
dt <- sub("{gg}", dt_gg(input_dt), dt, fixed = TRUE)
256280
dt <- sub("{ggg}", dt_ggg(input_dt), dt, fixed = TRUE)

R/fdt.R

+10-6
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,11 @@ fdt <- function(
958958

959959
dt_out <- rep_len(NA_character_, length(input))
960960

961+
# Verify if each entry has timezone (checking before loop for performance)
962+
if (is.character(input)) {
963+
tz_present <- is_tz_present(input = input)
964+
}
965+
961966
for (i in seq_along(input)) {
962967

963968
input_i <- input[i]
@@ -981,14 +986,13 @@ fdt <- function(
981986
time_present <- is_time_present(input = input_i)
982987

983988
# Determine if tz information is present, either as:
984-
# [1] a tz offset in hours from GMT
989+
# [1] a tz offset in hours from GMT (defined above before loop)
985990
# [2] a long tz identifier (either canonical or an alias)
986-
987-
tz_present <- is_tz_present(input = input_i)
991+
tz_present_i <- tz_present[i]
988992
long_tzid_present <- is_long_tzid_present(input = input_i)
989993

990994
# Strip away tz information from the input and return as `input_str`
991-
if (tz_present) {
995+
if (tz_present_i) {
992996
input_str <- strip_tz(input = input_i)
993997
} else if (long_tzid_present) {
994998
input_str <- strip_long_tzid(input = input_i)
@@ -1108,7 +1112,7 @@ fdt <- function(
11081112
tz_info$tz_short_specific <- get_tz_short_specific(long_tzid = tz_info$long_tzid, input_dt = input_dt)
11091113
tz_info$tz_long_specific <- get_tz_long_specific(long_tzid = tz_info$long_tzid, input_dt = input_dt, locale = locale)
11101114

1111-
} else if (tz_present) {
1115+
} else if (tz_present_i) {
11121116

11131117
tz_info$tz_str <- get_tz_str(input = input_i)
11141118
tz_info$tz_offset <- get_tz_offset_val(input = input_i)
@@ -1254,7 +1258,7 @@ dt_format_pattern <- function(format) {
12541258
unique(unlist(strsplit(format, "", fixed = TRUE))),
12551259
sub_letters()
12561260
)
1257-
1261+
# Bad error if length(dt_letters) == 0
12581262
pattern <- paste0("(", paste0("[", dt_letters, "]+", collapse = "|"), ")")
12591263

12601264
format <- gsub(pattern, "\\{\\1\\}", format)

R/utils-date_time_parse.R

+6-9
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ is_time_present <- function(input) {
4242
grepl(get_time_pattern(), input)
4343
}
4444

45+
# returns a vector of length of input
4546
is_tz_present <- function(input) {
46-
4747
regex <- paste(
4848
get_tz_pattern_z(),
4949
get_tz_pattern_hh(),
5050
get_tz_pattern_hh_mm(),
5151
get_tz_pattern_hhmm(),
5252
sep = "|"
5353
)
54-
any(grepl(regex, input))
54+
grepl(regex, input)
5555
}
5656

5757
is_long_tzid_present <- function(input) {
@@ -323,21 +323,18 @@ get_localized_exemplar_city <- function(
323323
# The short specific non-location format (e.g., 'PST') from a `long_tzid`
324324
get_tz_short_specific <- function(long_tzid, input_dt) {
325325

326-
input_date <- as.Date(input_dt)
327-
328326
tzdb_entries_tzid <- tzdb[tzdb$zone_name == long_tzid, ]
329327
if (nrow(tzdb_entries_tzid) == 0) {
330328
return(NA_character_)
331329
}
332330

333-
tzdb_idx <- rle(tzdb_entries_tzid$date_start >= input_date)$lengths[1]
331+
input_date <- as.Date(input_dt)
334332

335-
tz_short_specific <- tzdb_entries_tzid[tzdb_idx, "abbrev"]
333+
tzdb_idx <- rle(tzdb_entries_tzid$date_start >= input_date)$lengths[1]
336334

337335
# TODO: add check to ensure that the `abbrev` value is a valid
338336
# short specific non-location time zone
339-
340-
tz_short_specific
337+
tzdb_entries_tzid[tzdb_idx, "abbrev"]
341338
}
342339

343340
# The long specific non-location format (e.g., 'Pacific Standard Time') from
@@ -462,7 +459,7 @@ get_tz_non_location <- function(
462459
tz_name <- tz_metazone_names_entry[[available_items]]
463460
} else if (target_item %in% available_items) {
464461
tz_name <- tz_metazone_names_entry[[target_item]]
465-
} else if (short_long == "short" && !has_short_items) {
462+
} else if (!has_short_items && short_long == "short") {
466463
if (any(grepl(type, available_items))) {
467464
tz_name <- tz_metazone_names_entry[[paste0("long.", type)]]
468465
} else {

0 commit comments

Comments
 (0)