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_r189956939
 
 

 ##########
 File path: 
daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/PrimitivesDateTime1.scala
 ##########
 @@ -188,3 +189,83 @@ object TextCalendarConstants {
     }
   }
 }
+
+case class ConvertBinaryCalendarParser(erd: ElementRuntimeData,
+  xsdType: String,
+  prettyType: String,
+  pattern: String,
+  hasTZ: Boolean,
+  localeEv: CalendarLanguageEv,
+  calendarEv: CalendarEv,
+  infosetPattern: String,
+  firstDay: Int,
+  calendarDaysInFirstWeek: Int,
+  calendarCheckPolicy: Boolean,
+  calendarTz: Option[TimeZone],
+  tz: TimeZone,
+  binCalRep: BinaryCalendarRep,
+  binCalEpoch: String,
+  lengthInBits: Int)
+  extends ConvertTextCalendarProcessorBase(erd,
+    xsdType, prettyType, pattern, hasTZ, localeEv, calendarEv, infosetPattern, 
firstDay, calendarDaysInFirstWeek,
+    calendarCheckPolicy, calendarTz, tz)
+  with TextPrimParser {
+
+  override lazy val runtimeDependencies = List(localeEv, calendarEv)
+
+  def parse(start: PState): Unit = {
+
+    val dis = start.dataInputStream
+    val int: BigInt = dis.getSignedBigInt(lengthInBits, start)
+    val pos = new ParsePosition(0)
+    val locale: ULocale = localeEv.evaluate(start)
+    val calendar: Calendar = calendarEv.evaluate(start)
+
+    // This initialization is needed because the calendar object may have
+    // been persisted, and that computes/completes fields that are not yet 
completed,
+    // such as the Julian day, which freezes the year to 1970.
+    // We want a fresh start on all the fields that are filled in from a parse.
+
+    calendar.clear()
+
+    val df = tlDataFormatter(locale, calendar)
+    val cal = df.getCalendar.clone.asInstanceOf[Calendar]
+
+    df.parse(binCalEpoch, cal, pos)
+
+    // Use pos to verify all characters consumed & check for errors
+    if (pos.getIndex != binCalEpoch.length || pos.getErrorIndex >= 0) {
+      val errIndex = if (pos.getErrorIndex >= 0) pos.getErrorIndex else 
pos.getIndex
+      PE(start, "Parsing the binaryCalendarEpoch from String '%s' -  Failed to 
parse at index: %d.", binCalEpoch, errIndex)
+      return
+    }
+
+    if (binCalRep.eq(BinaryCalendarRep.BinarySeconds)) {
+      cal.add(Calendar.SECOND, int.intValue())
+    } else if (binCalRep.eq(BinaryCalendarRep.BinaryMilliseconds)) {
+      cal.add(Calendar.MILLISECOND, int.intValue())
+    }
+
+    // Unfortunately, there is no publicly available method for validating
+    // Calendar values are correct with respect to leniency. So instead, just
+    // try to calculate the time, which forces validation. This causes an
+    // exception to be thrown if a Calendar is not valid.
+    try {
+      cal.getTime
+    } catch {
+      case e: IllegalArgumentException => {
+        PE(start, "Convert to %s (for xs:%s): Failed to parse: %s.", 
prettyType, xsdType, e.getMessage())
+        return
+      }
+    }
+
+    val newCal = xsdType.toLowerCase() match {
+      case "time" => new DFDLTime(cal, hasTZ)
+      case "date" => new DFDLDate(cal, hasTZ)
+      case "datetime" => new DFDLDateTime(cal, hasTZ)
+      case _ => Assert.impossibleCase
+    }
 
 Review comment:
   time and date aren't valid, so I think this should just always create a 
DFDLDateTime.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to