cleanup date parsing

development
Norwin 5 years ago
parent c618907853
commit 9ddc077bfd

@ -1,13 +1,13 @@
# parses from/to params for get_measurements_ and get_boxes_
parse_dateparams = function (from, to) {
from = utc_date(from)
to = utc_date(to)
from = date_as_utc(from)
to = date_as_utc(to)
if (from - to > 0) stop('"from" must be earlier than "to"')
c(date_as_isostring(from), date_as_isostring(to))
}
# NOTE: cannot handle mixed vectors of POSIXlt and POSIXct
utc_date = function (date) {
date_as_utc = function (date) {
time = as.POSIXct(date)
attr(time, 'tzone') = 'UTC'
time
@ -16,6 +16,8 @@ utc_date = function (date) {
# NOTE: cannot handle mixed vectors of POSIXlt and POSIXct
date_as_isostring = function (date) format.Date(date, format = '%FT%TZ')
isostring_as_date = function (x) as.POSIXct(strptime(x, format = '%FT%T', tz = 'GMT'))
#' Checks for an interactive session using interactive() and a knitr process in
#' the callstack. See https://stackoverflow.com/a/33108841
#'

@ -97,7 +97,7 @@ osem_boxes = function (exposure = NA, model = NA, grouptag = NA,
if (!is.na(to) && !is.na(from))
query$date = parse_dateparams(from, to) %>% paste(collapse = ',')
else if (!is.na(date))
query$date = utc_date(date) %>% date_as_isostring()
query$date = date_as_utc(date) %>% date_as_isostring()
do.call(get_boxes_, query)
}
@ -152,11 +152,11 @@ parse_senseboxdata = function (boxdata) {
thebox = as.data.frame(boxdata, stringsAsFactors = F)
# parse timestamps (updatedAt might be not defined)
thebox$createdAt = as.POSIXct(strptime(thebox$createdAt, format = '%FT%T', tz = 'GMT'))
thebox$createdAt = isostring_as_date(thebox$createdAt)
if (!is.null(thebox$updatedAt))
thebox$updatedAt = as.POSIXct(strptime(thebox$updatedAt, format = '%FT%T', tz = 'GMT'))
thebox$updatedAt = isostring_as_date(thebox$updatedAt)
if (!is.null(lastMeasurement))
thebox$lastMeasurement = as.POSIXct(strptime(lastMeasurement, format = '%FT%T', tz = 'GMT'))
thebox$lastMeasurement = isostring_as_date(lastMeasurement)
# create a dataframe of sensors
thebox$sensors = sensors %>%

@ -46,7 +46,7 @@ summary.sensebox = function(object, ...) {
table(object$model) %>% print()
cat('\n')
diffNow = (utc_date(Sys.time()) - object$lastMeasurement) %>% as.numeric(unit = 'hours')
diffNow = (date_as_utc(Sys.time()) - object$lastMeasurement) %>% as.numeric(unit = 'hours')
list(
'last_measurement_within' = c(
'1h' = nrow(dplyr::filter(object, diffNow <= 1)),

@ -180,8 +180,8 @@ parse_get_measurements_params = function (params) {
if (!is.na(params$from) && !is.na(params$to)) {
parse_dateparams(params$from, params$to) # only for validation sideeffect
query$`from-date` = utc_date(params$from)
query$`to-date` = utc_date(params$to)
query$`from-date` = date_as_utc(params$from)
query$`to-date` = date_as_utc(params$to)
}
if (!is.na(params$exposure)) query$exposure = params$exposure

Loading…
Cancel
Save