This is an automated email from the ASF dual-hosted git repository.

casion pushed a commit to branch dev-1.2.0
in repository https://gitbox.apache.org/repos/asf/incubator-linkis.git


The following commit(s) were added to refs/heads/dev-1.2.0 by this push:
     new f8cbf458a Add built-in variables run_today_h and run_today_h_std 
(#2299)
f8cbf458a is described below

commit f8cbf458a7aa5bb8383687971acb65987549073c
Author: peacewong <[email protected]>
AuthorDate: Mon Jun 20 15:31:26 2022 +0800

    Add built-in variables run_today_h and run_today_h_std (#2299)
    
    * Add built-in variables:run_today_h,run_today_h_std close #2179
    * Solve problems with hour less than 10
---
 .../apache/linkis/common/utils/VariableUtils.scala |  86 +++++++++---------
 .../linkis/common/variable/CustomDateType.scala    |  36 ++++++++
 .../linkis/common/variable/DateTypeUtils.scala     |  23 +++++
 .../linkis/common/variable/VariableType.scala      |  12 +++
 .../linkis/common/utils/VariableUtilsTest.scala    |  65 ++++++++++++++
 .../linkis/common/variable/DateTypeUtilsTest.scala |  38 ++++++++
 .../linkis/common/variable/VariableTypeTest.scala  | 100 +++++++++++++++++++++
 7 files changed, 319 insertions(+), 41 deletions(-)

diff --git 
a/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/utils/VariableUtils.scala
 
b/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/utils/VariableUtils.scala
index 192343d8d..f57637c24 100644
--- 
a/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/utils/VariableUtils.scala
+++ 
b/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/utils/VariableUtils.scala
@@ -20,14 +20,13 @@ package org.apache.linkis.common.utils
 import org.apache.commons.lang3.StringUtils
 import org.apache.linkis.common.exception.LinkisCommonErrorException
 import org.apache.linkis.common.variable
-import org.apache.linkis.common.variable.DateTypeUtils.{getMonthDay, getToday, 
getYesterday}
+import org.apache.linkis.common.variable.DateTypeUtils.{getCurHour, 
getMonthDay, getToday, getYesterday}
 import org.apache.linkis.common.variable._
 
 import java.util
 import scala.collection.JavaConverters.mapAsScalaMapConverter
 import scala.collection.convert.WrapAsScala._
 import scala.collection.mutable
-import scala.collection.mutable.ArrayBuffer
 import scala.util.control.Exception.allCatch
 
 
@@ -121,48 +120,48 @@ object VariableUtils extends Logging {
   }
 
   private def initAllDateVars(run_date: CustomDateType, nameAndType: 
mutable.Map[String, variable.VariableType]): Unit = {
-    val run_data_str = run_date.toString
+    val run_date_str = run_date.toString
     nameAndType("run_date_std") = variable.DateType(new 
CustomDateType(run_date.getStdDate))
-    nameAndType("run_month_begin") = MonthType(new 
CustomMonthType(run_data_str, false))
-    nameAndType("run_month_begin_std") = variable.MonthType(new 
CustomMonthType(run_data_str))
-    nameAndType("run_month_end") = variable.MonthType(new 
CustomMonthType(run_data_str, false, true))
-    nameAndType("run_month_end_std") = variable.MonthType(new 
CustomMonthType(run_data_str, true, true))
-
-    nameAndType("run_quarter_begin") = QuarterType(new 
CustomQuarterType(run_data_str, false))
-    nameAndType("run_quarter_begin_std") = QuarterType(new 
CustomQuarterType(run_data_str))
-    nameAndType("run_quarter_end") = QuarterType(new 
CustomQuarterType(run_data_str, false, true))
-    nameAndType("run_quarter_end_std") = QuarterType(new 
CustomQuarterType(run_data_str, true, true))
-
-    nameAndType("run_half_year_begin") = HalfYearType(new 
CustomHalfYearType(run_data_str, false))
-    nameAndType("run_half_year_begin_std") = HalfYearType(new 
CustomHalfYearType(run_data_str))
-    nameAndType("run_half_year_end") = HalfYearType(new 
CustomHalfYearType(run_data_str, false, true))
-    nameAndType("run_half_year_end_std") = HalfYearType(new 
CustomHalfYearType(run_data_str, true, true))
-
-    nameAndType("run_year_begin") = YearType(new CustomYearType(run_data_str, 
false))
-    nameAndType("run_year_begin_std") = YearType(new 
CustomYearType(run_data_str))
-    nameAndType("run_year_end") = YearType(new CustomYearType(run_data_str, 
false, true))
-    nameAndType("run_year_end_std") = YearType(new 
CustomYearType(run_data_str, true, true))
+    nameAndType("run_month_begin") = MonthType(new 
CustomMonthType(run_date_str, false))
+    nameAndType("run_month_begin_std") = variable.MonthType(new 
CustomMonthType(run_date_str))
+    nameAndType("run_month_end") = variable.MonthType(new 
CustomMonthType(run_date_str, false, true))
+    nameAndType("run_month_end_std") = variable.MonthType(new 
CustomMonthType(run_date_str, true, true))
+
+    nameAndType("run_quarter_begin") = QuarterType(new 
CustomQuarterType(run_date_str, false))
+    nameAndType("run_quarter_begin_std") = QuarterType(new 
CustomQuarterType(run_date_str))
+    nameAndType("run_quarter_end") = QuarterType(new 
CustomQuarterType(run_date_str, false, true))
+    nameAndType("run_quarter_end_std") = QuarterType(new 
CustomQuarterType(run_date_str, true, true))
+
+    nameAndType("run_half_year_begin") = HalfYearType(new 
CustomHalfYearType(run_date_str, false))
+    nameAndType("run_half_year_begin_std") = HalfYearType(new 
CustomHalfYearType(run_date_str))
+    nameAndType("run_half_year_end") = HalfYearType(new 
CustomHalfYearType(run_date_str, false, true))
+    nameAndType("run_half_year_end_std") = HalfYearType(new 
CustomHalfYearType(run_date_str, true, true))
+
+    nameAndType("run_year_begin") = YearType(new CustomYearType(run_date_str, 
false))
+    nameAndType("run_year_begin_std") = YearType(new 
CustomYearType(run_date_str))
+    nameAndType("run_year_end") = YearType(new CustomYearType(run_date_str, 
false, true))
+    nameAndType("run_year_end_std") = YearType(new 
CustomYearType(run_date_str, true, true))
 
     nameAndType("run_date_std") = variable.DateType(new 
CustomDateType(run_date.getStdDate))
-    nameAndType("run_month_begin") = variable.MonthType(new 
CustomMonthType(run_data_str, false))
-    nameAndType("run_month_begin_std") = variable.MonthType(new 
CustomMonthType(run_data_str))
-    nameAndType("run_month_end") = variable.MonthType(new 
CustomMonthType(run_data_str, false, true))
-    nameAndType("run_month_end_std") = variable.MonthType(new 
CustomMonthType(run_data_str, true, true))
-
-    nameAndType("run_quarter_begin") = QuarterType(new 
CustomQuarterType(run_data_str, false))
-    nameAndType("run_quarter_begin_std") = QuarterType(new 
CustomQuarterType(run_data_str))
-    nameAndType("run_quarter_end") = QuarterType(new 
CustomQuarterType(run_data_str, false, true))
-    nameAndType("run_quarter_end_std") = QuarterType(new 
CustomQuarterType(run_data_str, true, true))
-
-    nameAndType("run_half_year_begin") = HalfYearType(new 
CustomHalfYearType(run_data_str, false))
-    nameAndType("run_half_year_begin_std") = HalfYearType(new 
CustomHalfYearType(run_data_str))
-    nameAndType("run_half_year_end") = HalfYearType(new 
CustomHalfYearType(run_data_str, false, true))
-    nameAndType("run_half_year_end_std") = HalfYearType(new 
CustomHalfYearType(run_data_str, true, true))
-
-    nameAndType("run_year_begin") = YearType(new CustomYearType(run_data_str, 
false))
-    nameAndType("run_year_begin_std") = YearType(new 
CustomYearType(run_data_str))
-    nameAndType("run_year_end") = YearType(new CustomYearType(run_data_str, 
false, true))
-    nameAndType("run_year_end_std") = YearType(new 
CustomYearType(run_data_str, true, true))
+    nameAndType("run_month_begin") = variable.MonthType(new 
CustomMonthType(run_date_str, false))
+    nameAndType("run_month_begin_std") = variable.MonthType(new 
CustomMonthType(run_date_str))
+    nameAndType("run_month_end") = variable.MonthType(new 
CustomMonthType(run_date_str, false, true))
+    nameAndType("run_month_end_std") = variable.MonthType(new 
CustomMonthType(run_date_str, true, true))
+
+    nameAndType("run_quarter_begin") = QuarterType(new 
CustomQuarterType(run_date_str, false))
+    nameAndType("run_quarter_begin_std") = QuarterType(new 
CustomQuarterType(run_date_str))
+    nameAndType("run_quarter_end") = QuarterType(new 
CustomQuarterType(run_date_str, false, true))
+    nameAndType("run_quarter_end_std") = QuarterType(new 
CustomQuarterType(run_date_str, true, true))
+
+    nameAndType("run_half_year_begin") = HalfYearType(new 
CustomHalfYearType(run_date_str, false))
+    nameAndType("run_half_year_begin_std") = HalfYearType(new 
CustomHalfYearType(run_date_str))
+    nameAndType("run_half_year_end") = HalfYearType(new 
CustomHalfYearType(run_date_str, false, true))
+    nameAndType("run_half_year_end_std") = HalfYearType(new 
CustomHalfYearType(run_date_str, true, true))
+
+    nameAndType("run_year_begin") = YearType(new CustomYearType(run_date_str, 
false))
+    nameAndType("run_year_begin_std") = YearType(new 
CustomYearType(run_date_str))
+    nameAndType("run_year_end") = YearType(new CustomYearType(run_date_str, 
false, true))
+    nameAndType("run_year_end_std") = YearType(new 
CustomYearType(run_date_str, true, true))
 
     /*
     calculate run_today based on run_date
@@ -183,6 +182,11 @@ object VariableUtils extends Logging {
     nameAndType("run_mon_start_std") = MonType(new 
CustomMonType(run_mon.toString, true, false))
     nameAndType("run_mon_end") = MonType(new CustomMonType(run_mon.toString, 
false, true))
     nameAndType("run_mon_end_std") = MonType(new 
CustomMonType(run_mon.toString, true, true))
+
+    // calculate run_mon base on run_date
+    val run_today_h = new CustomHourType(getCurHour(false, 
run_today.toString), false)
+    nameAndType("run_today_h") = HourType(run_today_h)
+    nameAndType("run_today_h_std") = HourType(new 
CustomHourType(run_today_h.toString, true))
   }
 
   /**
diff --git 
a/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/CustomDateType.scala
 
b/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/CustomDateType.scala
index 22b2d4edf..bc3e53ff0 100644
--- 
a/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/CustomDateType.scala
+++ 
b/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/CustomDateType.scala
@@ -138,6 +138,8 @@ class CustomMonType(date: String, std: Boolean = true, 
isEnd: Boolean = false) {
 
 }
 
+
+
 /*
  Given a Date, convert into Quarter
  */
@@ -223,3 +225,37 @@ class CustomYearType(date: String, std: Boolean = true, 
isEnd: Boolean = false)
   }
 
 }
+
+class CustomHourType(dateH: String, std: Boolean = true) {
+
+  def -(hour: Int): String = {
+    val dateFormat = DateTypeUtils.dateFormatHourLocal.get()
+    val dateFormatStd = DateTypeUtils.dateFormatHourStdLocal.get()
+    if (std) {
+      dateFormatStd.format(DateUtils.addHours(dateFormat.parse(dateH), -hour))
+    } else {
+      dateFormat.format(DateUtils.addHours(dateFormat.parse(dateH), -hour))
+    }
+  }
+
+  def +(hour: Int): String = {
+    val dateFormat = DateTypeUtils.dateFormatHourLocal.get()
+    val dateFormatStd = DateTypeUtils.dateFormatHourStdLocal.get()
+    if (std) {
+      dateFormatStd.format(DateUtils.addHours(dateFormat.parse(dateH), hour))
+    } else {
+      dateFormat.format(DateUtils.addHours(dateFormat.parse(dateH), hour))
+    }
+  }
+
+  override def toString: String = {
+    val dateFormat = DateTypeUtils.dateFormatHourLocal.get()
+    val dateFormatStd = DateTypeUtils.dateFormatHourStdLocal.get()
+    if (std) {
+      dateFormatStd.format(dateFormat.parse(dateH))
+    } else {
+      dateH
+    }
+  }
+
+}
diff --git 
a/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/DateTypeUtils.scala
 
b/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/DateTypeUtils.scala
index f9b0d2cb0..cedc8933c 100644
--- 
a/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/DateTypeUtils.scala
+++ 
b/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/DateTypeUtils.scala
@@ -38,6 +38,14 @@ object DateTypeUtils {
     override protected def initialValue = new SimpleDateFormat("yyyy-MM")
   }
 
+  val dateFormatHourLocal = new ThreadLocal[SimpleDateFormat]() {
+    override protected def initialValue = new SimpleDateFormat("yyyyMMddHH")
+  }
+
+  val dateFormatHourStdLocal = new ThreadLocal[SimpleDateFormat]() {
+    override protected def initialValue = new SimpleDateFormat("yyyy-MM-dd-HH")
+  }
+
   /**
    * Get Today"s date
    *
@@ -221,4 +229,19 @@ object DateTypeUtils {
     }
   }
 
+  def getCurHour(std: Boolean = true, dateString: String = null): String = {
+    val dateFormat = dateFormatHourLocal.get()
+    val dateFormat_std = dateFormatHourStdLocal.get()
+    val cal: Calendar = Calendar.getInstance()
+    val hour = cal.get(Calendar.HOUR_OF_DAY)
+    val hourOfDayStd = if (hour < 10) "0" + hour else "" + hour
+    val curHourStr = dateString + hourOfDayStd
+    val curHour = dateFormat.parse(curHourStr)
+    if (std) {
+      dateFormat_std.format(curHour)
+    } else {
+      curHourStr
+    }
+  }
+
 }
diff --git 
a/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/VariableType.scala
 
b/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/VariableType.scala
index 756ffb386..cece5f8a6 100644
--- 
a/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/VariableType.scala
+++ 
b/linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/VariableType.scala
@@ -160,4 +160,16 @@ case class StringType(value: String) extends VariableType {
       case _ => throw new LinkisCommonErrorException(20049, s"String class is 
not supported to use:$signal")
     }
   }
+}
+
+case class HourType(value: CustomHourType) extends VariableType {
+  override def getValue: String = value.toString
+
+  def calculator(signal: String, bValue: String): String = {
+    signal match {
+      case "+" => value + bValue.toInt
+      case "-" => value - bValue.toInt
+      case _ => throw new LinkisCommonErrorException(20046, s"HourType is not 
supported to use:$signal")
+    }
+  }
 }
\ No newline at end of file
diff --git 
a/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/utils/VariableUtilsTest.scala
 
b/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/utils/VariableUtilsTest.scala
new file mode 100644
index 000000000..d737990a1
--- /dev/null
+++ 
b/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/utils/VariableUtilsTest.scala
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.linkis.common.utils
+
+import org.apache.linkis.common.variable.{CustomDateType, CustomHourType, 
DateType, HourType}
+import org.apache.linkis.common.variable.DateTypeUtils.{getCurHour, getToday}
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
+
+import java.util
+
+class VariableUtilsTest {
+
+  private val run_date_str = "20200228"
+
+  @Test def testReplace(): Unit = {
+    val sql = """select
+                |'${run_date}' as run_date,
+                |'${run_today}' as run_today,
+                |'${run_year_begin}' as run_year_begin,
+                |'${run_year_begin_std-3}' as run_year_begin_std_sub3,
+                |'${run_year_end+10}' as run_year_end_add10,
+                |'${run_year_end_std}' as run_half_year_end_std,
+                |'${run_half_year_begin-1}' as run_half_year_begin_sub1,
+                |'${run_half_year_begin_std}' as run_half_year_begin_std,
+                |'${run_half_year_end}' as run_half_year_end,
+                |'${run_today_h+12}' as run_today_h_add1""".stripMargin
+    val run_date = new CustomDateType(run_date_str, false)
+    val dateType = DateType(run_date)
+    //add 1 days
+    val dateTypeRes = dateType.calculator("+", "1")
+    val hourType = HourType(new CustomHourType(getCurHour(false, dateTypeRes), 
false))
+    val hourTypeRes = hourType.calculator("+", "12")
+    val resSql = s"""select
+                    |'20200228' as run_date,
+                    |'20200229' as run_today,
+                    |'20200101' as run_year_begin,
+                    |'2017-01-01' as run_year_begin_std_sub3,
+                    |'20301231' as run_year_end_add10,
+                    |'2020-12-31' as run_half_year_end_std,
+                    |'20190701' as run_half_year_begin_sub1,
+                    |'2020-01-01' as run_half_year_begin_std,
+                    |'20200630' as run_half_year_end,
+                    |'${hourTypeRes}' as run_today_h_add1""".stripMargin
+    val varMap = new util.HashMap[String, String]()
+    varMap.put("run_date", run_date_str)
+    assertEquals(VariableUtils.replace(sql, "sql", varMap), resSql)
+  }
+
+}
diff --git 
a/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/variable/DateTypeUtilsTest.scala
 
b/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/variable/DateTypeUtilsTest.scala
new file mode 100644
index 000000000..20d80778b
--- /dev/null
+++ 
b/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/variable/DateTypeUtilsTest.scala
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.linkis.common.variable
+
+import org.junit.jupiter.api.Assertions._
+import org.junit.jupiter.api.Test
+
+import java.util.Calendar
+
+class DateTypeUtilsTest {
+
+  @Test def testGetCurHour(): Unit = {
+    val dateFormat = DateTypeUtils.dateFormatLocal.get()
+    val runDateStr = "20220617"
+    val cal: Calendar = Calendar.getInstance()
+    val hourOfDay = cal.get(Calendar.HOUR_OF_DAY)
+    val hourOfDayStd = if (hourOfDay < 10) "0" + hourOfDay else "" + hourOfDay
+    val hour = runDateStr + hourOfDayStd
+    val curHour = DateTypeUtils.getCurHour(false, runDateStr)
+    assertEquals(hour, curHour)
+  }
+
+}
diff --git 
a/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/variable/VariableTypeTest.scala
 
b/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/variable/VariableTypeTest.scala
new file mode 100644
index 000000000..c57692445
--- /dev/null
+++ 
b/linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/variable/VariableTypeTest.scala
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.linkis.common.variable
+
+import org.apache.linkis.common.variable.DateTypeUtils.{getCurHour, 
getMonthDay}
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Test
+
+import java.util.Calendar
+
+class VariableTypeTest {
+
+  private val run_date_str = "20200228"
+
+  @Test def testDateType(): Unit = {
+    val run_date = new CustomDateType(run_date_str, false)
+    val dateType = DateType(run_date)
+    val dateTypeRes = dateType.calculator("+", "10")
+    assertEquals(dateTypeRes, "20200309")
+  }
+
+  @Test def testMonthType(): Unit = {
+    val monthType = MonthType(new CustomMonthType(run_date_str, false))
+    val monthTypeRes = monthType.calculator("+", "3")
+    assertEquals(monthTypeRes, "20200501")
+    val monthEndType = MonthType(new CustomMonthType(run_date_str, false, 
true))
+    val monthEndTypeRes = monthEndType.calculator("+", "3")
+    assertEquals(monthEndTypeRes, "20200531")
+  }
+
+  @Test def testMonType(): Unit = {
+    val run_date = new CustomDateType(run_date_str, false)
+    val run_mon = new CustomMonType(getMonthDay(false, run_date.getDate), 
false)
+    val run_mon_end = new CustomMonType(getMonthDay(false, run_date.getDate), 
false, true)
+    val monType = MonType(run_mon)
+    val monTypeRes = monType.calculator("+", "1")
+    assertEquals(monTypeRes, "202003")
+    val monTypeEnd = MonType(run_mon_end)
+    val monTypeEndRes = monTypeEnd.calculator("+", "1")
+    assertEquals(monTypeEndRes, "202012")
+  }
+
+  @Test def testQuarterType(): Unit = {
+    val quarterType = QuarterType(new CustomQuarterType(run_date_str, false))
+    val quarterTypeRes = quarterType.calculator("+", "1")
+    assertEquals(quarterTypeRes, "20200401")
+    val quarterTypeEnd = QuarterType(new CustomQuarterType(run_date_str, 
false, true))
+    val quarterTypeEndRes = quarterTypeEnd.calculator("+", "1")
+    assertEquals(quarterTypeEndRes, "20200630")
+  }
+
+  @Test def testHalfYearType(): Unit = {
+    val halfYearType = HalfYearType(new CustomHalfYearType(run_date_str, 
false))
+    val halfYearTypeRes = halfYearType.calculator("+", "1")
+    assertEquals(halfYearTypeRes, "20200701")
+    val halfYearTypeEnd = HalfYearType(new CustomHalfYearType(run_date_str, 
false, true))
+    val halfYearTypeEndRes = halfYearTypeEnd.calculator("+", "1")
+    assertEquals(halfYearTypeEndRes, "20201231")
+  }
+
+  @Test def testYearType(): Unit = {
+    val yearType = YearType(new CustomYearType(run_date_str, false))
+    val yearTypeRes = yearType.calculator("+", "1")
+    assertEquals(yearTypeRes, "20210101")
+    val yearTypeEnd = YearType(new CustomYearType(run_date_str, false, true))
+    val yearTypeEndRes = yearTypeEnd.calculator("+", "1")
+    assertEquals(yearTypeEndRes, "20211231")
+  }
+
+  @Test def testHourType(): Unit = {
+    val hourType = HourType(new CustomHourType(getCurHour(false, 
run_date_str), false))
+    // add 24 h
+    val hourTypeRes = hourType.calculator("+", "24")
+    val run_date = new CustomDateType(run_date_str, false)
+    val dateType = DateType(run_date)
+    //add 1 days
+    val dateTypeRes = dateType.calculator("+", "1")
+    val cal: Calendar = Calendar.getInstance()
+    val hourOfDay = cal.get(Calendar.HOUR_OF_DAY)
+    val hourOfDayStd = if (hourOfDay < 10) "0" + hourOfDay else "" + hourOfDay
+    val hour = dateTypeRes + hourOfDayStd
+    assertEquals(hourTypeRes, hour)
+  }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to