Skip to content

Commit

Permalink
add option for gregorian dates in getTimeseries2 for R (for VTL users)
Browse files Browse the repository at this point in the history
  • Loading branch information
amattioc committed Mar 21, 2024
1 parent cd4327a commit 960ff88
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion BUILD
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20240321-1338
20240321-1536
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
package it.bancaditalia.oss.sdmx.api;

import java.io.Serializable;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -206,22 +209,13 @@ public String[] getGregorianTimeStamps() throws DataStructureException, SdmxInva
}

if(freq.equalsIgnoreCase("M")){
if(time.endsWith("01") || time.endsWith("03") || time.endsWith("05") || time.endsWith("07") ||
time.endsWith("08") || time.endsWith("10") || time.endsWith("12") ){
result[i] = time + "-31";
}
else if(time.endsWith("02")){
result[i] = time + "-28"; // handle leap year
}
else{
result[i] = time + "-30";
}
result[i] = YearMonth.parse(time).atEndOfMonth().toString();
}
else if(freq.equalsIgnoreCase("Q")){
result[i] = time.replace("Q1", "03-31").replace("Q2", "06-30").replace("Q3", "09-30").replace("Q4", "12-31");
}
else if(freq.equalsIgnoreCase("A")){
result[i] = ((String) getValueAt(i, timeCol)) + "-12-31";
result[i] = Year.parse(time).atMonth(12).atEndOfMonth().toString();
}
else if(freq.equalsIgnoreCase("H")){
result[i] = ((String) getValueAt(i, timeCol)).replace("S1", "06-30").replace("S2", "12-31");
Expand Down
10 changes: 6 additions & 4 deletions RJSDMX/R/SdmxClient.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,13 @@ getTimeSeries2 <- function(provider, dataflow, key='', filter='', start='', end=
#' @param id timeseries key
#' @param end the end time - optional
#' @param start the start time - optional
#' @param gregorianTime set to true to have all daily dates - optional
#' @rdname getTimeSeriesTable
#' @export
getTimeSeriesTable <- function(provider, id, start='', end='') {
getTimeSeriesTable <- function(provider, id, start='', end='', gregorianTime=F) {
res <- J("it.bancaditalia.oss.sdmx.client.SdmxClientHandler")$getTimeSeriesTable(provider, id, start, end, FALSE, .jnull(), FALSE)
#convert to an R data.frame
res = convertTSDF(res)
res = convertTSDF(res, , gregorianTime)
return(res)
}

Expand All @@ -221,12 +222,13 @@ getTimeSeriesTable <- function(provider, id, start='', end='') {
#' @param measures the comma separated list of measures to be returned - optional, default='all', 'none' for no measures
#' @param updatedAfter return only changes after this date - optional
#' @param includeHistory include history of revisions - optional, default=false
#' @param gregorianTime set to true to have all daily dates - optional
#' @rdname getTimeSeriesTable2
#' @export
getTimeSeriesTable2 <- function(provider, dataflow, key='', filter='', start='', end='', attributes='all', measures='all', updatedAfter=.jnull(), includeHistory=FALSE) {
getTimeSeriesTable2 <- function(provider, dataflow, key='', filter='', start='', end='', attributes='all', measures='all', updatedAfter=.jnull(), includeHistory=FALSE, gregorianTime = F) {
res <- J("it.bancaditalia.oss.sdmx.client.SdmxClientHandler")$getTimeSeriesTable2(provider, dataflow, key, filter, start, end, attributes, measures, updatedAfter, includeHistory)
#convert to an R data.frame
res = convertTSDF(res)
res = convertTSDF(res, , gregorianTime)
return(res)
}

Expand Down
7 changes: 5 additions & 2 deletions RJSDMX/R/TSConverter.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ convertTSList <- function (javaList, plain = F) {
}

# convert a java PortableDataSet
convertTSDF <- function (jtable) {
convertTSDF <- function (jtable, gregorianTime) {
isNumeric <- .jcall(jtable,"Z","isNumeric");
dataflow <- .jcall(jtable,"Ljava/lang/String;","getDataflow");
time = .jcall(jtable,"[Ljava/lang/String;","getTimeStamps", evalString = TRUE, evalArray = TRUE)
if(gregorianTime)
time = .jcall(jtable,"[Ljava/lang/String;","getGregorianTimeStamps", evalString = TRUE, evalArray = TRUE)
else
time = .jcall(jtable,"[Ljava/lang/String;","getTimeStamps", evalString = TRUE, evalArray = TRUE)
values = .jcall(jtable,"[Ljava/lang/Object;","getObservations", evalArray = TRUE)
if(isNumeric){
values = sapply(values, .jcall,"D","doubleValue")
Expand Down
Binary file modified RJSDMX/inst/java/SDMX.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions RJSDMX/man/getTimeSeriesTable.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ getTimeSeriesTable(provider, id, start='', end='')

\item{start}{the start time - optional}

\item{gregorianTime}{set to true to have all daily dates - optional}

}
\description{
Extract a list of time series identified by the parameters provided in input, and return a data.frame as result.
Expand Down
3 changes: 3 additions & 0 deletions RJSDMX/man/getTimeSeriesTable2.Rd

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

Binary file modified STATA/jar/SDMX.jar
Binary file not shown.

0 comments on commit 960ff88

Please sign in to comment.