Skip to content

Commit ac905ab

Browse files
committed
tm
1 parent 8b8475e commit ac905ab

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- `unique<SpatVector>` gained arguments "geom=TRUE" and "atts=TRUE" to allow uniqueness to be based on geometry or attributes only [#1928](https://github.com/rspatial/terra/issues/1928) by Andrea Titolo
2020
- `buffer<SpatRaster>` gains argument "include=TRUE" to allow exclusion of the cells that are not `NA` from the buffer [#1929](https://github.com/rspatial/terra/issues/1929) by Márcia Barbosa
2121
- argument "names" to `distance<SpatVector,missing>` [#1942](https://github.com/rspatial/terra/issues/1942) by Márcia Barbosa
22+
- `subset<SpatVector>` now works without a value for argument "subset", to allow subsetting variables only [#1953](https://github.com/rspatial/terra/issues/1953) by Márcia Barbosa
2223

2324

2425
## new
@@ -27,6 +28,7 @@
2728
- `cartogram<SpatRaster>` can now return a "circles" (dorling) cartogram [#1911](https://github.com/rspatial/terra/issues/1911) by Márcia Barbosa
2829
- `subset<SpatVector>` can now use a Spat object to spatially subset [#1937](https://github.com/rspatial/terra/issues/1937) by Márcia Barbosa
2930

31+
3032
# version 1.8-70
3133

3234
Released 2025-09-27

R/time.R

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ setMethod("time", signature(x="SpatRaster"),
113113
tstep <- x@pntr$timestep
114114

115115
if (format != "") {
116-
steps <- c("seconds", "days", "months", "years", "yearmonths")
116+
steps <- c("seconds", "days", "months", "years", "yearmonths", "raw")
117117
format <- match.arg(tolower(format), steps)
118+
if (format == "raw") return(d)
119+
118120
if ((format == "months") && (tstep == "years")) {
119121
error("time", "cannot extract months from years-time")
120122
} else if ((format == "years") && (tstep %in% c("months"))) {
@@ -133,22 +135,19 @@ setMethod("time", signature(x="SpatRaster"),
133135
}
134136

135137

138+
tz <- x@pntr$timezone
139+
tz[tz == ""] <- "UTC"
136140
d <- strptime("1970-01-01", "%Y-%m-%d", tz="UTC") + d
137-
if (tstep == "seconds") {
138-
tz <- x@pntr$timezone
139-
if (!(tz %in% c("", "UTC"))) {
140-
attr(d, "tzone") = tz
141-
}
142-
if (format == "seconds") {
143-
return(d)
144-
}
141+
attr(d, "tzone") = tz
142+
if (format == "seconds") {
143+
return(d)
145144
}
146145

147146
if (format == "days") {
148147
# first use format to avoid time zone trouble #1896
149148
# as.Date(format(d, "%Y-%m-%d"))
150149
# above does not work with negative years. #1951
151-
as.Date(d, tz="")
150+
as.Date(d, tz=tz)
152151
} else if (format == "yearmonths") {
153152
y <- as.integer(format(d, "%Y"))
154153
y + (as.integer(format(d, "%m"))-1)/12

src/spatTime.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,15 @@ SpatTime_t time_from_day(int syear, int smonth, int sday, double ndays) {
241241
}
242242

243243

244+
#include "Rcpp.h"
244245

245246
SpatTime_t get_time_noleap(int syear, int smonth, int sday, int shour, int smin, int ssec, double n, std::string step) {
246247

247248
static const int md[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
248249

249250
// set start to beginning of year
250-
double s = ssec + smin * 60 + shour * 3600 + (sday-1) * 24 * 3600;
251-
for (int i=0; i < smonth; i++) {
252-
s += (md[i] * 24 * 3600);
253-
}
251+
double s = ssec + smin * 60 + shour * 3600 + (sday-1) * 86400;
252+
s += md[smonth-1] * 86400;
254253

255254
double ndays;
256255
if (step == "hours") {
@@ -274,6 +273,8 @@ SpatTime_t get_time_noleap(int syear, int smonth, int sday, int shour, int smin,
274273
break;
275274
}
276275
}
276+
Rcpp::Rcout << ndays << " " << year << " " << month << " " << rem << std::endl;
277+
277278
rem -= md[month-1];
278279
int day = rem;
279280
rem -= day;
@@ -284,6 +285,11 @@ SpatTime_t get_time_noleap(int syear, int smonth, int sday, int shour, int smin,
284285
int mn = rem * 60;
285286
rem -= mn;
286287
int sc = rem * 60;
288+
289+
SpatTime_t tmp = get_time(year+syear, month, day, hr, mn, sc);
290+
291+
Rcpp::Rcout << syear << " " << smonth << " " << sday << " " << shour << " " << smin << " " << ssec << " " << n << " " << step << std::endl;
292+
Rcpp::Rcout << year << " " << month << " " << day << " " << tmp << std::endl << std::endl;
287293

288294
return get_time(year+syear, month, day, hr, mn, sc);
289295
}
@@ -293,7 +299,8 @@ SpatTime_t get_time_noleap(int syear, int smonth, int sday, int shour, int smin,
293299
SpatTime_t get_time_360(int syear, int smonth, int sday, int shour, int smin, int ssec, double n, std::string step) {
294300

295301
// set start to beginning of year
296-
double s = ssec + smin * 60 + shour * 3600 + (sday-1) * 24 * 3600 + (smonth-1) * 30;
302+
int sdays = (smonth-1) * 30 + (sday-1);
303+
double s = ssec + smin * 60 + shour * 3600 + sdays * 86400;
297304

298305
double ndays;
299306
if (step == "hours") {

0 commit comments

Comments
 (0)