-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.castYear.R
54 lines (41 loc) · 1.79 KB
/
types.castYear.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#' @title Cast year
#' @description Cast year. A calendar year as per \href{https://www.w3.org/TR/xmlschema-2/#gYear}{XMLSchema gYear}.
#' Usual lexical representation is: YYYY.
#' @param format no options (other than the default)
#' @param value year to cast
#' @rdname types.castYear
#' @export
#'
#' @seealso \href{https://specs.frictionlessdata.io//table-schema/#year}{Types and formats specifications}
#'
#' @examples
#'
#' types.castYear(format = "default", value = 2000)
#'
#' types.castYear(format = "default", value = "2010")
#'
types.castYear <- function(format, value) {
if (!is_integer(value)) {
if (!is.character(value)) return(config::get("ERROR", file = system.file("config/config.yml", package = "tableschema.r")))
if (length(value) > 1) return(config::get("ERROR", file = system.file("config/config.yml", package = "tableschema.r")))
if (nchar(value) != 4 | isTRUE(grepl('[a-zA-Z]', value))) return(config::get("ERROR", file = system.file("config/config.yml", package = "tableschema.r")))
tryCatch({
result <- as.integer(value)
if (is.na(result) |
is.nan(result) |
as.character(result) != value) {
return(config::get("ERROR", file = system.file("config/config.yml", package = "tableschema.r")))
}
value <- result
},
warning = function(w) {
message(config::get("WARNING", file = system.file("config/config.yml", package = "tableschema.r")))
},
error = function(e) {
return(config::get("ERROR", file = system.file("config/config.yml", package = "tableschema.r")))
},
finally = {})
}
if (is.na(value) | value < 0 | value > 9999) return(config::get("ERROR", file = system.file("config/config.yml", package = "tableschema.r")))
return(value)
}