From a5c742b7cbedc341c26fe7ec47c1a038cce7959f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?axt=E2=98=85?= Date: Wed, 5 Jun 2019 00:43:06 +0900 Subject: [PATCH 1/4] fix date --- .../com/axtstar/asta4e/basic/CsvBasic.scala | 6 ++- .../com/axtstar/asta4e/converter/CC.scala | 7 ++-- .../com/axtstar/asta4e/converter/Config.scala | 7 +--- .../com/axtstar/asta4e/csv/CsvTest.scala | 39 ++++++++----------- 4 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala b/src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala index 34dab09..4604d3d 100644 --- a/src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala +++ b/src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala @@ -1,8 +1,10 @@ package com.axtstar.asta4e.basic import java.io.{FileInputStream, InputStreamReader, OutputStreamWriter} +import java.time.{LocalDateTime, ZoneId} import java.util.Date +import com.axtstar.asta4e.converter.Config import com.axtstar.asta4e.core.{DataCore, InitialCore} import com.opencsv.{CSVParserBuilder, CSVReaderBuilder, CSVWriterBuilder, ICSVWriter} @@ -117,7 +119,7 @@ trait CsvBasic extends DataCore with InitialCore [CsvBasic] /*with DataCore[CsvB m(l.positionX) = map(l.name) match { case null => "" case mm:Date => - s"${quoteChar}${mm.toString}${quoteChar}" + s"${quoteChar}${LocalDateTime.ofInstant(mm.toInstant, ZoneId.systemDefault()).format(Config.DateReadParse)}${quoteChar}" case mm:String => s"${quoteChar}${map(l.name).toString}${quoteChar}" case _ => @@ -163,7 +165,7 @@ trait CsvBasic extends DataCore with InitialCore [CsvBasic] /*with DataCore[CsvB m(l.positionX) = map(l.name) match { case null => "" case mm:Date => - s"${quoteChar}${mm.toString}${quoteChar}" + s"${quoteChar}${LocalDateTime.ofInstant(mm.toInstant, ZoneId.systemDefault()).format(Config.DateReadParse)}${quoteChar}" case mm:String => s"${quoteChar}${map(l.name).toString}${quoteChar}" case _ => diff --git a/src/main/scala/com/axtstar/asta4e/converter/CC.scala b/src/main/scala/com/axtstar/asta4e/converter/CC.scala index 9938094..7725b9d 100644 --- a/src/main/scala/com/axtstar/asta4e/converter/CC.scala +++ b/src/main/scala/com/axtstar/asta4e/converter/CC.scala @@ -1,9 +1,8 @@ package com.axtstar.asta4e.converter -import java.text.{DateFormat, SimpleDateFormat} -import java.util.{Date, Locale} +import java.time.LocalDate +import java.util.Date -import shapeless.PolyDefns.Case import shapeless.{::, HList, HNil, LabelledGeneric, Lazy, Poly0, Poly1, Typeable, Witness, ops} import shapeless.labelled.{FieldType, field} @@ -285,7 +284,7 @@ object CC { case xx:Date => xx case xx:String => - Try(Config.DateReadParse.parse(xx)).getOrElse(new Date(Long.MinValue)) + Try(LocalDate.parse(xx, Config.DateReadParse)).getOrElse(new Date(Long.MinValue)) case _ => new Date(Long.MinValue) }) diff --git a/src/main/scala/com/axtstar/asta4e/converter/Config.scala b/src/main/scala/com/axtstar/asta4e/converter/Config.scala index 7cd50f4..843c67e 100644 --- a/src/main/scala/com/axtstar/asta4e/converter/Config.scala +++ b/src/main/scala/com/axtstar/asta4e/converter/Config.scala @@ -1,12 +1,9 @@ package com.axtstar.asta4e.converter -import java.io.{FileInputStream, FileOutputStream} -import java.text.SimpleDateFormat +import java.time.format.DateTimeFormatter import java.util.Locale -import com.axtstar.asta4e.core.Location - object Config { - var DateReadParse = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("GMT")) + var DateReadParse = DateTimeFormatter.ISO_DATE_TIME } \ No newline at end of file diff --git a/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala b/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala index 0622760..09b5aff 100644 --- a/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala +++ b/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala @@ -18,11 +18,6 @@ import org.specs2.runner.JUnitRunner class CsvTest extends Specification { val currentDir = new File(".").getAbsoluteFile().getParent() - val dateFormat = new SimpleDateFormat("yyyy/MM/dd") - val timeFormat = new SimpleDateFormat("HH:mm:ss") - - val dateFormatFull = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") - "CSV" should { "Get 0 row" in { val target = CsvMapper.by[CSV_Data] @@ -43,11 +38,11 @@ class CsvTest extends Specification { val map = MapHelper.to[VariousCell].from("numeric" -> 1001 & "string" -> "1000" & - "date" -> dateFormat.parse("2018/7/7") & + "date" -> new SimpleDateFormat("yyyy/MM/dd").parse("2018/7/7") & "formula" -> "=B2" & "bool" -> true & - "time" -> timeFormat.parse("23:32:41") & - "userDate" -> dateFormatFull.parse("2018/11/23 18:52:56") + "time" -> new SimpleDateFormat("HH:mm:ss").parse("23:32:41") & + "userDate" -> new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2018/11/23 18:52:56") ) val ff = java.io.File.createTempFile(s"${currentDir}/target/","data_w_r_1.csv") @@ -67,7 +62,7 @@ class CsvTest extends Specification { target.size must be_==(1) target(0)._2.get.string must be_==("1000") //println(target(0)._2.get.date) - target(0)._2.get.date must be_==(dateFormat.parse("2018/7/7")) + target(0)._2.get.date must be_==(new SimpleDateFormat("yyyy/MM/dd").parse("2018/7/7")) target(0)._2.get.formula must be_==("") //Location設定なし } @@ -76,11 +71,11 @@ class CsvTest extends Specification { val map = MapHelper.to[VariousCell].from("numeric" -> 1001 & "string" -> "1000" & - "date" -> dateFormat.parse("2018/7/7") & + "date" -> new SimpleDateFormat("yyyy/MM/dd").parse("2018/7/7") & "formula" -> "=B2" & "bool" -> true & - "time" -> timeFormat.parse("23:32:41") & - "userDate" -> dateFormatFull.parse("2018/11/23 18:52:56") + "time" -> new SimpleDateFormat("HH:mm:ss").parse("23:32:41") & + "userDate" -> new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2018/11/23 18:52:56") ) val ff = java.io.File.createTempFile(s"${currentDir}/target/","data_w_r_1.csv") @@ -118,11 +113,11 @@ class CsvTest extends Specification { val map = MapHelper.to[VariousCell].from("numeric" -> 1001 & "string" -> "1000" & - "date" -> dateFormat.parse("2018/7/7") & + "date" -> new SimpleDateFormat("yyyy/MM/dd").parse("2018/7/7") & "formula" -> "=B2" & "bool" -> true & - "time" -> timeFormat.parse("23:32:41") & - "userDate" -> dateFormatFull.parse("2018/11/23 18:52:56") + "time" -> new SimpleDateFormat("HH:mm:ss").parse("23:32:41") & + "userDate" -> new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2018/11/23 18:52:56") ) val ff = java.io.File.createTempFile(s"${currentDir}/target/","data_w_r_down.csv").getAbsolutePath @@ -147,7 +142,7 @@ class CsvTest extends Specification { println(target(0)._2(0).get.date) target.size must be_==(1) target(0)._2(0).get.string must be_==("1000") - target(0)._2(0).get.date must be_==(dateFormat.parse("2018/7/7")) + target(0)._2(0).get.date must be_==(new SimpleDateFormat("yyyy/MM/dd").parse("2018/7/7")) target(0)._2(0).get.formula must be_==("") //Location設定なし } @@ -180,20 +175,20 @@ class CsvTest extends Specification { ("Sheet1" -> ( "numeric" -> 1001 & "string" -> "1000" & - "date" -> dateFormat.parse("2018/7/7") & + "date" -> new SimpleDateFormat("yyyy/MM/dd").parse("2018/7/7") & "formula" -> "=B2" & "bool" -> true & - "time" -> timeFormat.parse("23:32:41") & - "userDate" -> dateFormatFull.parse("2018/11/23 18:52:56") + "time" -> new SimpleDateFormat("HH:mm:ss").parse("23:32:41") & + "userDate" -> new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2018/11/23 18:52:56") ) & "Sheet2" -> ( "numeric" -> 1002 & "string" -> "1001" & - "date" -> dateFormat.parse("2018/7/8") & + "date" -> new SimpleDateFormat("yyyy/MM/dd").parse("2018/7/8") & "formula" -> "=B3" & "bool" -> false & - "time" -> timeFormat.parse("23:32:42") & - "userDate" -> dateFormatFull.parse("2018/11/24 18:52:56") + "time" -> new SimpleDateFormat("HH:mm:ss").parse("23:32:42") & + "userDate" -> new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2018/11/24 18:52:56") )):_* ) From 272e5359c902d3ce0cfa6adcefd9d3c4aa9e9c03 Mon Sep 17 00:00:00 2001 From: axt Date: Wed, 5 Jun 2019 01:08:53 +0900 Subject: [PATCH 2/4] more precious date --- build.sbt | 2 +- src/main/scala/com/axtstar/asta4e/converter/CC.scala | 7 +++++-- src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index 143afa5..9aa5645 100644 --- a/build.sbt +++ b/build.sbt @@ -11,7 +11,7 @@ licenses += ("Apache-2.0", url("http://www.apache.org/licenses/LICENSE-2.0")) publishMavenStyle := true name := "asta4e" -version := "0.0.14" +version := "0.0.15-SNAPSHOT" scalaVersion := "2.11.12" crossScalaVersions := Seq("2.11.12", "2.12.8") diff --git a/src/main/scala/com/axtstar/asta4e/converter/CC.scala b/src/main/scala/com/axtstar/asta4e/converter/CC.scala index 7725b9d..65c1982 100644 --- a/src/main/scala/com/axtstar/asta4e/converter/CC.scala +++ b/src/main/scala/com/axtstar/asta4e/converter/CC.scala @@ -1,6 +1,6 @@ package com.axtstar.asta4e.converter -import java.time.LocalDate +import java.time.{LocalDate, ZoneId} import java.util.Date import shapeless.{::, HList, HNil, LabelledGeneric, Lazy, Poly0, Poly1, Typeable, Witness, ops} @@ -284,7 +284,10 @@ object CC { case xx:Date => xx case xx:String => - Try(LocalDate.parse(xx, Config.DateReadParse)).getOrElse(new Date(Long.MinValue)) + Try({ + val localDate = LocalDate.parse(xx, Config.DateReadParse) + Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()) + }).getOrElse(new Date(Long.MinValue)) case _ => new Date(Long.MinValue) }) diff --git a/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala b/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala index 09b5aff..ec2e438 100644 --- a/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala +++ b/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala @@ -46,6 +46,7 @@ class CsvTest extends Specification { ) val ff = java.io.File.createTempFile(s"${currentDir}/target/","data_w_r_1.csv") + println(ff.getAbsolutePath) CsvMapper.by[VariousCell] .withLocation(VariousCell.getLocation()) From 31edf1a0f91d2f333368e6136a1378ece385d105 Mon Sep 17 00:00:00 2001 From: axt Date: Thu, 6 Jun 2019 06:45:32 +0900 Subject: [PATCH 3/4] add test --- .../com/axtstar/asta4e/basic/CsvBasic.scala | 10 +++---- .../com/axtstar/asta4e/csv/CsvTest.scala | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala b/src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala index 4604d3d..56a23e2 100644 --- a/src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala +++ b/src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala @@ -12,7 +12,7 @@ object CsvBasic { } -trait CsvBasic extends DataCore with InitialCore [CsvBasic] /*with DataCore[CsvBasic]*/ { +trait CsvBasic extends DataCore with InitialCore [CsvBasic] { protected var separator = ',' protected var quoteChar = '"' protected var encoding = "UTF-8" @@ -45,7 +45,7 @@ trait CsvBasic extends DataCore with InitialCore [CsvBasic] /*with DataCore[CsvB map ) } catch { - case ex:Exception => + case ex:Throwable => throw ex } finally { reader.close() @@ -85,7 +85,7 @@ trait CsvBasic extends DataCore with InitialCore [CsvBasic] /*with DataCore[CsvB map })) } catch { - case ex:Exception => + case ex:Throwable => throw ex } finally { reader.close() @@ -131,7 +131,7 @@ trait CsvBasic extends DataCore with InitialCore [CsvBasic] /*with DataCore[CsvB } } catch { - case ex:Exception => + case ex:Throwable => throw ex } finally { writer.close() @@ -178,7 +178,7 @@ trait CsvBasic extends DataCore with InitialCore [CsvBasic] /*with DataCore[CsvB } } catch { - case ex:Exception => + case ex:Throwable => throw ex } finally { writer.close() diff --git a/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala b/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala index ec2e438..31d32c6 100644 --- a/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala +++ b/src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala @@ -19,6 +19,36 @@ class CsvTest extends Specification { val currentDir = new File(".").getAbsoluteFile().getParent() "CSV" should { + "Not Found" in { + def a = { + val target = CsvMapper.by[CSV_Data] + .withLocation(Location_4_CSV.ao_a1_a2_startRow_as_0) + .getCC( + new FileInputStream(s"${currentDir}/src/test/resources/csv/notfound") + ) + } + a must throwA[java.io.IOException] + + def b = { + val target = CsvMapper.by[CSV_Data] + .withLocation(Location_4_CSV.ao_a1_a2_startRow_as_0) + .getCCDown( + new FileInputStream(s"${currentDir}/src/test/resources/csv/notfound") + ) + } + b must throwA[java.io.IOException] + + def c = { + CsvMapper.by[VariousCell] + .withLocation(VariousCell.getLocation()) + .withOutStream(new FileOutputStream(s"${currentDir}/target///notfound")) + .setCC(IndexedSeq("Sheet1" -> Option(null.asInstanceOf[VariousCell]))) // Error + } + c must throwA[java.util.NoSuchElementException] + + } + + "Get 0 row" in { val target = CsvMapper.by[CSV_Data] .withLocation(Location_4_CSV.ao_a1_a2_startRow_as_0) From 0b4f7e4c53aa4b6e0b4a0763812a0442a864c882 Mon Sep 17 00:00:00 2001 From: axt Date: Thu, 6 Jun 2019 07:38:00 +0900 Subject: [PATCH 4/4] ver fix --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 9aa5645..5c71bc0 100644 --- a/build.sbt +++ b/build.sbt @@ -11,7 +11,7 @@ licenses += ("Apache-2.0", url("http://www.apache.org/licenses/LICENSE-2.0")) publishMavenStyle := true name := "asta4e" -version := "0.0.15-SNAPSHOT" +version := "0.0.15" scalaVersion := "2.11.12" crossScalaVersions := Seq("2.11.12", "2.12.8")