Skip to content

Commit

Permalink
Merge pull request #5 from axtstar/0.0.15
Browse files Browse the repository at this point in the history
0.0.15
  • Loading branch information
axtstar committed Jun 5, 2019
2 parents 0beb760 + 0b4f7e4 commit ce4f130
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Expand Up @@ -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"

scalaVersion := "2.11.12"
crossScalaVersions := Seq("2.11.12", "2.12.8")
Expand Down
16 changes: 9 additions & 7 deletions src/main/scala/com/axtstar/asta4e/basic/CsvBasic.scala
@@ -1,16 +1,18 @@
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}

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"
Expand Down Expand Up @@ -43,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()
Expand Down Expand Up @@ -83,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()
Expand Down Expand Up @@ -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 _ =>
Expand All @@ -129,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()
Expand Down Expand Up @@ -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 _ =>
Expand All @@ -176,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()
Expand Down
10 changes: 6 additions & 4 deletions 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, ZoneId}
import java.util.Date

import shapeless.PolyDefns.Case
import shapeless.{::, HList, HNil, LabelledGeneric, Lazy, Poly0, Poly1, Typeable, Witness, ops}
import shapeless.labelled.{FieldType, field}

Expand Down Expand Up @@ -285,7 +284,10 @@ object CC {
case xx:Date =>
xx
case xx:String =>
Try(Config.DateReadParse.parse(xx)).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)
})
Expand Down
7 changes: 2 additions & 5 deletions 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
}
68 changes: 47 additions & 21 deletions src/test/scala/com/axtstar/asta4e/csv/CsvTest.scala
Expand Up @@ -18,12 +18,37 @@ 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")
"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]

}

val dateFormatFull = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")

"CSV" should {
"Get 0 row" in {
val target = CsvMapper.by[CSV_Data]
.withLocation(Location_4_CSV.ao_a1_a2_startRow_as_0)
Expand All @@ -43,14 +68,15 @@ 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")
println(ff.getAbsolutePath)

CsvMapper.by[VariousCell]
.withLocation(VariousCell.getLocation())
Expand All @@ -67,7 +93,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設定なし
}

Expand All @@ -76,11 +102,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")
Expand Down Expand Up @@ -118,11 +144,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
Expand All @@ -147,7 +173,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設定なし
}

Expand Down Expand Up @@ -180,20 +206,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")
)):_*
)

Expand Down

0 comments on commit ce4f130

Please sign in to comment.