Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include localdatetime to lowercase module #101

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/main/scala/com/datasonnet/DS.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package com.datasonnet
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.math.{BigDecimal, RoundingMode}
import java.net.URL
import java.security.SecureRandom
Expand All @@ -24,12 +25,11 @@ import java.time.temporal.ChronoUnit
import java.time.{DateTimeException, Duration, Instant, LocalDateTime, Period, ZoneId, ZoneOffset, ZonedDateTime}
import java.util.function.Function
import java.util.{Base64, Scanner}

import com.datasonnet
import com.datasonnet.document.{DefaultDocument, MediaType}
import com.datasonnet.header.Header
import com.datasonnet.modules.{Crypto, JsonPath, Regex}
import com.datasonnet.spi.{DataFormatService, Library, ujsonUtils}

import javax.crypto.Cipher
import javax.crypto.spec.{IvParameterSpec, SecretKeySpec}
import sjsonnet.Expr.Member.Visibility
Expand Down Expand Up @@ -796,6 +796,7 @@ object DSLowercase extends Library {
written.substring(written.indexOf(">") + 1, written.length - wrapperStop.length)
},
),

"datetime" -> moduleFrom(
builtin0("now") { (_, _, _) => ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) },

Expand Down Expand Up @@ -1000,6 +1001,39 @@ object DSLowercase extends Library {

),

"localdatetime" -> moduleFrom(
builtin0("now") { (vs, extVars, wd) =>
val datetimeObj = java.time.LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC)
datetimeObj.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
},

builtin("offset", "datetime", "period") { (ev, fs, v1: String, v2: String) =>
// NOTE: DEMO ONLY (in particular, missing proper error handling)
val datetime = java.time.LocalDateTime.parse(v1, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
val period = Period.parse(v2)
datetime.plus(period).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
},

builtin("format", "datetime", "inputFormat", "outputFormat") {
(ev, fs, datetime: String, inputFormat: String, outputFormat: String) =>
val datetimeObj = java.time.LocalDateTime.parse(datetime, DateTimeFormatter.ofPattern(inputFormat))
datetimeObj.format(DateTimeFormatter.ofPattern(outputFormat))
},

builtin0("compare", "datetime1", "format1", "datetime2", "format2") {
(vals, ev, fs) =>
val strValSeq = validate(vals, ev, fs, Array(StringRead, StringRead, StringRead, StringRead))
val datetime1 = strValSeq(0).asInstanceOf[String]
val format1 = strValSeq(1).asInstanceOf[String]
val datetime2 = strValSeq(2).asInstanceOf[String]
val format2 = strValSeq(3).asInstanceOf[String]

val datetimeObj1 = java.time.LocalDateTime.parse(datetime1, DateTimeFormatter.ofPattern(format1))
val datetimeObj2 = java.time.LocalDateTime.parse(datetime2, DateTimeFormatter.ofPattern(format2))
datetimeObj1.compareTo(datetimeObj2)
}
),

"period" -> moduleFrom(
builtin("between", "datetimeone", "datetimetwo") {
(_,_, datetimeone: String , datetimetwo: String) =>
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/datasonnet/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class CoreTest {
private final String lib = "ds";
private final String mathPack = ".math";
private final String datetimePack = ".datetime";
private final String localDateTimePack = ".localdatetime";

@Test
void test_abs() {
Expand Down Expand Up @@ -879,4 +880,29 @@ void test_or() {
String value = mapper.transform("{}").replaceAll("\"", "");
assertEquals("abc", value);
}

@Test
void localDateTime_now() {
Mapper mapper = new Mapper(lib + localDateTimePack + ".now()\n", new ArrayList<>(), new HashMap<>(), true);
String value = mapper.transform("{}").replaceAll("\"", "");
assertNotNull(value);
}

@Test
void localDateTime_offset() {
Mapper mapper = new Mapper(lib + localDateTimePack + ".offset(\"2019-07-22T21:00:00\", \"P1Y1D\")\n", new ArrayList<>(), new HashMap<>(), true);
String value = mapper.transform("{}").replaceAll("\"", "");
assertEquals("2020-07-23T21:00:00", value);
}

@Test
void localDateTime_compare() {
Mapper mapper = new Mapper(lib + localDateTimePack + ".compare(\"2019-07-04T21:00:00\", \"yyyy-MM-dd'T'HH:mm:ss\", \"2019-07-04T21:00:00\", \"yyyy-MM-dd'T'HH:mm:ss\")\n",
new ArrayList<>(),
new HashMap<>(),
true);
String value = mapper.transform("{}").replaceAll("\"", "");
assertEquals("0", value);
}

}