Simple utility funcions for working with dates
authorGeorgios Gousios <gousiosg@gmail.com>
Fri, 18 Nov 2011 15:01:20 +0000 (17:01 +0200)
committerGeorgios Gousios <gousiosg@gmail.com>
Fri, 18 Nov 2011 15:01:20 +0000 (17:01 +0200)
logic/src/main/scala/gr/grnet/aquarium/util/DateUtils.scala [new file with mode: 0644]
logic/src/test/scala/gr/grnet/aquarium/logic/test/DateUtilsTest.scala [new file with mode: 0644]

diff --git a/logic/src/main/scala/gr/grnet/aquarium/util/DateUtils.scala b/logic/src/main/scala/gr/grnet/aquarium/util/DateUtils.scala
new file mode 100644 (file)
index 0000000..8d64aa5
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.aquarium.util
+
+import scala.collection.mutable
+import java.util.{Calendar, Date, GregorianCalendar}
+
+/**
+ * Various utils for manipulating dates, with special
+ * emphasis on credit DSL requirements.
+ *
+ * @author Georgios Gousios <gousiosg@gmail.com>
+ */
+trait DateUtils {
+
+  def findDays(from: Date, to: Date, f: Calendar => Boolean) : List[Date] = {
+    val c = new GregorianCalendar()
+    val result = new mutable.ListBuffer[Date]()
+
+    c.setTime(from)
+
+    while (c.getTime().getTime <= to.getTime) {
+      if (f(c))
+        result += new Date(c.getTime.getTime)
+      c.add(Calendar.DAY_OF_YEAR, 1)
+    }
+
+    result.toList
+  }
+
+  /**
+   * Adjust time in the provided date to the provided values
+   */
+  def adjustToTime(d: Date, h: Int,  m: Int): Date = {
+
+    assert((0 <= h) && (h <= 23))
+    assert((0 <= m) && (m <= 59))
+
+    val c = new GregorianCalendar()
+    c.setTime(d)
+    c.roll(Calendar.MINUTE, m - c.get(Calendar.MINUTE))
+    c.roll(Calendar.HOUR_OF_DAY, h - c.get(Calendar.HOUR_OF_DAY))
+    c.getTime
+  }
+}
\ No newline at end of file
diff --git a/logic/src/test/scala/gr/grnet/aquarium/logic/test/DateUtilsTest.scala b/logic/src/test/scala/gr/grnet/aquarium/logic/test/DateUtilsTest.scala
new file mode 100644 (file)
index 0000000..ed66003
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2011 GRNET S.A. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions and the following
+ *      disclaimer in the documentation and/or other materials
+ *      provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and
+ * documentation are those of the authors and should not be
+ * interpreted as representing official policies, either expressed
+ * or implied, of GRNET S.A.
+ */
+
+package gr.grnet.aquarium.logic.test
+
+import org.junit.Test
+import org.junit.Assert._
+import gr.grnet.aquarium.util.DateUtils
+import java.util.{Calendar, Date}
+
+class DateUtilsTest extends DateUtils with TestMethods {
+
+  @Test
+  def testFindDays() = {
+    var start = new Date(1321530829000L) // 17/11/2011 13:54:02
+    var end = new Date(1353160515000L)   // 17/11/2012 13:55:15
+
+    var result = findDays(start, end, {
+      c =>
+        c.get(Calendar.DAY_OF_WEEK) == 5}
+    )
+    assertEquals(53, result.size)
+  }
+
+  @Test
+  def testAdjustTime() = {
+    var d = new Date(1321615962000L)        // 18/11/2011 13:32:42
+    var target = new Date(1321573542000L)   // 18/11/2011 01:45:42
+
+    val result = adjustToTime(d, 1, 45)
+    assertEquals(target, result)
+
+    assertThrows(adjustToTime(d, 1, 62))
+  }
+}
\ No newline at end of file