Skip to content

Commit

Permalink
#167 support UTC tz in for POSIX values
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Feb 21, 2020
1 parent 3551391 commit e091cf0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: geometa
Type: Package
Title: Tools for Reading and Writing ISO/OGC Geographic Metadata
Version: 0.6-2
Date: 2020-02-17
Date: 2020-02-20
Authors@R: c(person("Emmanuel", "Blondel", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0002-5870-5762")))
Maintainer: Emmanuel Blondel <[email protected]>
Description: Provides facilities to handle reading and writing of geographic metadata
Expand Down
6 changes: 4 additions & 2 deletions R/ISOAbstractObject.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,19 @@ ISOAbstractObject <- R6Class("ISOAbstractObject",
newvalue <- value
#datetime types
if(regexpr(pattern = "^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})$", value)>0){
newvalue <- as.POSIXct(strptime(value, "%Y-%m-%dT%H:%M:%S"), tz = "GMT")
newvalue <- as.POSIXct(strptime(value, "%Y-%m-%dT%H:%M:%S"), tz = ifelse(endsWith(value,"Z"),"UTC",""))
}else if(regexpr(pattern = "^(\\d{4})-(\\d{2})-(\\d{2})$", value)>0){
newvalue <- as.Date(as.POSIXct(strptime(value, "%Y-%m-%d"), tz = "GMT"))
newvalue <- as.Date(as.POSIXct(strptime(value, "%Y-%m-%d"), tz = "UTC"))
}

return(newvalue)
},
fromComplexTypes = function(value){
#datetime types
if(suppressWarnings(all(class(value)==c("POSIXct","POSIXt")))){
tz <- attr(value, "tzone")
value <- format(value,"%Y-%m-%dT%H:%M:%S")
if(tz=="UTC") value <- paste0(value,"Z")
}else if(class(value)[1] == "Date"){
value <- format(value,"%Y-%m-%d")
}
Expand Down
2 changes: 1 addition & 1 deletion R/geometa.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' Package: \tab geometa\cr
#' Type: \tab Package\cr
#' Version: \tab 0.6-2\cr
#' Date: \tab 2020-02-20\cr
#' Date: \tab 2020-02-21\cr
#' License: \tab MIT\cr
#' LazyLoad: \tab yes\cr
#' }
Expand Down
2 changes: 1 addition & 1 deletion man/geometa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test_ISOBaseDateTime.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ test_that("encoding",{

expect_true(ISOAbstractObject$compare(md, md2))

})

test_that("encoding - datetime in UTC",{
#encoding
date <- ISOdate(2015, 1, 1, 1)
attr(date, "tzone") <- "UTC"
md <- ISOBaseDateTime$new(value = date)
expect_is(md, "ISOBaseDateTime")
expect_equal(md$value, date)
xml <- md$encode()
expect_is(xml, "XMLInternalNode")

#decoding
md2 <- ISOBaseDateTime$new(xml = xml)
xml2 <- md2$encode()

expect_true(ISOAbstractObject$compare(md, md2))

})

0 comments on commit e091cf0

Please sign in to comment.