Return-Path: X-Original-To: apmail-drill-commits-archive@www.apache.org Delivered-To: apmail-drill-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2F7BD17611 for ; Thu, 18 Jun 2015 07:24:36 +0000 (UTC) Received: (qmail 77574 invoked by uid 500); 18 Jun 2015 07:24:35 -0000 Delivered-To: apmail-drill-commits-archive@drill.apache.org Received: (qmail 77545 invoked by uid 500); 18 Jun 2015 07:24:35 -0000 Mailing-List: contact commits-help@drill.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: commits@drill.apache.org Delivered-To: mailing list commits@drill.apache.org Received: (qmail 77535 invoked by uid 99); 18 Jun 2015 07:24:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Jun 2015 07:24:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 708BADFAF3; Thu, 18 Jun 2015 07:24:35 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mehant@apache.org To: commits@drill.apache.org Date: Thu, 18 Jun 2015 07:24:36 -0000 Message-Id: <3d131a66f16d42e69a707b6aa07c1fa5@git.apache.org> In-Reply-To: <87a0d9314125491393ad5f4c04f37903@git.apache.org> References: <87a0d9314125491393ad5f4c04f37903@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/2] drill git commit: DRILL-2403: Print leading zeroes in TimePrintMillis DRILL-2403: Print leading zeroes in TimePrintMillis Project: http://git-wip-us.apache.org/repos/asf/drill/repo Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/c2a2377b Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/c2a2377b Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/c2a2377b Branch: refs/heads/master Commit: c2a2377bdc2acaf714c19c0cb509f62c8aeffd19 Parents: d32acdb Author: Mehant Baid Authored: Thu Jun 11 14:17:26 2015 -0700 Committer: Mehant Baid Committed: Wed Jun 17 14:43:02 2015 -0700 ---------------------------------------------------------------------- .../vector/accessor/sql/TimePrintMillis.java | 22 +++++++++-- .../vector/accessor/TestTimePrintMillis.java | 40 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/drill/blob/c2a2377b/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java b/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java index 349abd5..2611b86 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/vector/accessor/sql/TimePrintMillis.java @@ -21,8 +21,12 @@ import java.sql.Time; import org.apache.drill.exec.expr.fn.impl.DateUtility; - public class TimePrintMillis extends Time { + private static final String[] leadingZeroes = {"", "0", "00"}; + + // Desired length of the milli second portion should be 3 + private static final int DESIRED_MILLIS_LENGTH = 3; + public TimePrintMillis(long time) { super(time); } @@ -30,13 +34,23 @@ public class TimePrintMillis extends Time { @Override public String toString () { int millis = (int) (getTime() % DateUtility.secondsToMillis); - String baseTime = super.toString(); + StringBuilder time = new StringBuilder().append(super.toString()); if (millis > 0) { - baseTime = baseTime + "." + Integer.toString(millis); + String millisString = Integer.toString(millis); + + // dot to separate the fractional seconds + time.append("."); + + int millisLength = millisString.length(); + if (millisLength < DESIRED_MILLIS_LENGTH) { + // add necessary leading zeroes + time.append(leadingZeroes[DESIRED_MILLIS_LENGTH - millisLength]); + } + time.append(millisString); } - return baseTime; + return time.toString(); } } http://git-wip-us.apache.org/repos/asf/drill/blob/c2a2377b/exec/java-exec/src/test/java/org/apache/drill/exec/vector/accessor/TestTimePrintMillis.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/vector/accessor/TestTimePrintMillis.java b/exec/java-exec/src/test/java/org/apache/drill/exec/vector/accessor/TestTimePrintMillis.java new file mode 100644 index 0000000..df106f2 --- /dev/null +++ b/exec/java-exec/src/test/java/org/apache/drill/exec/vector/accessor/TestTimePrintMillis.java @@ -0,0 +1,40 @@ +/** + * 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.drill.exec.vector.accessor; + +import org.apache.drill.exec.vector.accessor.sql.TimePrintMillis; +import org.junit.Assert; +import org.junit.Test; + +public class TestTimePrintMillis { + + @Test + public void testPrintingMillis() { + // testing the regular case where the precision of the millisecond is 3 + TimePrintMillis time = new TimePrintMillis(999); + Assert.assertTrue(time.toString().endsWith("999")); + + // test case where one leading zero needs to be added + time = new TimePrintMillis(99); + Assert.assertTrue(time.toString().endsWith("099")); + + // test case where two leading zeroes needs to be added + time = new TimePrintMillis(1); + Assert.assertTrue(time.toString().endsWith("001")); + } +}