git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GitHub] stevedlawrence commented on a change in pull request #70: Adding property binaryCalendarRep with values "binarySeconds" and "bi…


stevedlawrence commented on a change in pull request #70: Adding property binaryCalendarRep with values "binarySeconds" and "bi…
URL: https://github.com/apache/incubator-daffodil/pull/70#discussion_r191400465
 
 

 ##########
 File path: daffodil-runtime1-unparser/src/main/scala/org/apache/daffodil/processors/unparsers/ConvertBinaryCalendarUnparser.scala
 ##########
 @@ -0,0 +1,69 @@
+/*
+ * 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.daffodil.processors.unparsers
+
+import org.apache.daffodil.calendar.DFDLCalendar
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.io.DataOutputStream
+import org.apache.daffodil.io.FormatInfo
+import org.apache.daffodil.processors.ElementRuntimeData
+import org.apache.daffodil.schema.annotation.props.gen.BinaryCalendarRep
+import org.apache.daffodil.util.Maybe.One
+import org.apache.daffodil.util.Misc
+
+case class ConvertBinaryCalendarUnparser(
+  override val context: ElementRuntimeData,
+  binCalRep: BinaryCalendarRep,
+  epochTimeMillis: Long,
+  lengthInBits: Int)
+  extends PrimUnparser {
+
+  /**
+   * Primitive unparsers must override runtimeDependencies
+   */
+  override lazy val runtimeDependencies = Nil
+
+  protected def putNumber(dos: DataOutputStream, value: Long, nBits: Int, finfo: FormatInfo): Boolean = {
+    dos.putLong(value, nBits, finfo)
+  }
+
+  def unparse(state: UState): Unit = {
+
+    val node = state.currentInfosetNode.asSimple
+
+    val calValue = node.dataValue match {
+      case dc: DFDLCalendar => dc.calendar
+      case x => Assert.invariantFailed("ConvertTextCalendar received unsupported type. %s of type %s.".format(x, Misc.getNameFromClass(x)))
+    }
+
+    val diff: Long = binCalRep match {
+      case BinaryCalendarRep.BinarySeconds => (calValue.getTimeInMillis - epochTimeMillis) / 1000
+      case BinaryCalendarRep.BinaryMilliseconds => (calValue.getTimeInMillis - epochTimeMillis)
+      case _ => Assert.impossibleCase
+    }
 
 Review comment:
   Yeah, you're right. I don't think overflow is an issue. The range of valid values is just too small to overflow to a valid date.
   
   I *think* we do the right thing for text parsers. It looks like the SimpleDateFormat#parse method doesn't throw an exception on failure. Instead, you just need to look at the parse position to determine if there was an error. I would assume that if the text value was out of range, then parse() would set the error position appropriately. And then calendar.getTime is called. We probably want to do something similar for the binaryCalendarEpoch and throw an SDE if there's an error. Here's the logic for that in the text date time parser:
   
   https://github.com/apache/incubator-daffodil/blob/master/daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/PrimitivesDateTime1.scala#L136-L157

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@xxxxxxxxxxxxxxxx


With regards,
Apache Git Services