[banshee] [Hyena] Add invariant DateTime string methods



commit e781bb8fb6e38fe2459fb0080d0879337ccb7129
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Thu Feb 18 15:25:25 2010 -0800

    [Hyena] Add invariant DateTime string methods
    
    DateTimeUtil.ToInvariantString and TryParseInvariant methods.  Add unit
    tests for the new methods, and for the existing To/FromTimeT methods.

 .../Hyena/Hyena.Query/TimeSpanQueryValue.cs        |    2 +-
 src/Libraries/Hyena/Hyena.csproj                   |    1 +
 src/Libraries/Hyena/Hyena/DateTimeUtil.cs          |   14 ++++-
 .../Hyena/Hyena/Tests/DateTimeUtilTests.cs         |   77 ++++++++++++++++++++
 src/Libraries/Hyena/Makefile.am                    |    1 +
 5 files changed, 93 insertions(+), 2 deletions(-)
---
diff --git a/src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs b/src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs
index 3e3d52d..108b1e9 100644
--- a/src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs
+++ b/src/Libraries/Hyena/Hyena.Query/TimeSpanQueryValue.cs
@@ -201,4 +201,4 @@ namespace Hyena.Query
                 count, false, translate ? NumberFormatInfo.CurrentInfo : NumberFormatInfo.InvariantInfo));
         }
     }
-}
+}
\ No newline at end of file
diff --git a/src/Libraries/Hyena/Hyena.csproj b/src/Libraries/Hyena/Hyena.csproj
index 873433a..61d2619 100644
--- a/src/Libraries/Hyena/Hyena.csproj
+++ b/src/Libraries/Hyena/Hyena.csproj
@@ -173,6 +173,7 @@
     <Compile Include="Hyena.Metrics\MemorySampleStore.cs" />
     <Compile Include="Hyena.Json\Tests\SerializerTests.cs" />
     <Compile Include="Hyena.Json\ExtensionMethods.cs" />
+    <Compile Include="Hyena\Tests\DateTimeUtilTests.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="Mono.Posix">
diff --git a/src/Libraries/Hyena/Hyena/DateTimeUtil.cs b/src/Libraries/Hyena/Hyena/DateTimeUtil.cs
index ae1bff1..4b4b46a 100644
--- a/src/Libraries/Hyena/Hyena/DateTimeUtil.cs
+++ b/src/Libraries/Hyena/Hyena/DateTimeUtil.cs
@@ -27,6 +27,7 @@
 //
 
 using System;
+using System.Globalization;
 
 namespace Hyena
 {
@@ -71,5 +72,16 @@ namespace Hyena
                     String.Format ("{0}:{1:00}:{2:00}", hours, minutes, seconds) :
                     String.Format ("{0}:{1:00}", minutes, seconds));
         }
+
+        const string INVARIANT_FMT = "yyyy-MM-dd HH:mm:ss.fff zzz";
+        public static string ToInvariantString (DateTime dt)
+        {
+            return dt.ToString (INVARIANT_FMT, CultureInfo.InvariantCulture);
+        }
+
+        public static bool TryParseInvariant (string str, out DateTime dt)
+        {
+            return DateTime.TryParseExact (str, INVARIANT_FMT, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
+        }
     }
-}
+}
\ No newline at end of file
diff --git a/src/Libraries/Hyena/Hyena/Tests/DateTimeUtilTests.cs b/src/Libraries/Hyena/Hyena/Tests/DateTimeUtilTests.cs
new file mode 100644
index 0000000..b016202
--- /dev/null
+++ b/src/Libraries/Hyena/Hyena/Tests/DateTimeUtilTests.cs
@@ -0,0 +1,77 @@
+//
+// DateTimeUtilTests.cs
+//
+// Author:
+//   Gabriel Burt <gabriel burt gmail com>
+//
+// Copyright (c) 2010 Novell, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#if ENABLE_TESTS
+
+using System;
+using NUnit.Framework;
+using Hyena;
+
+namespace Hyena.Tests
+{
+    [TestFixture]
+    public class DateTimeUtilTests
+    {
+        [Test]
+        public void InvariantString ()
+        {
+            // Tests are run in Chicago timezone, UTC -6 in the winter, -5 in the summer
+            TestInv ("2010-02-18 02:41:00.000 -06:00", new DateTime (2010, 2, 18, 2, 41, 0, 0));
+            TestInv ("2010-02-18 02:41:50.123 -06:00", new DateTime (2010, 2, 18, 2, 41, 50, 123));
+            TestInv ("2010-10-18 02:01:00.000 -05:00", new DateTime (2010, 10, 18, 2, 1, 0, 0));
+        }
+
+        private void TestInv (string inv_string, DateTime dt)
+        {
+            // Make sure we can generate the given string from the DateTime
+            Assert.AreEqual (inv_string, DateTimeUtil.ToInvariantString (dt));
+
+            // And vice versa
+            DateTime parsed_dt;
+            if (DateTimeUtil.TryParseInvariant (inv_string, out parsed_dt))
+                Assert.AreEqual (dt, parsed_dt);
+            else
+                Assert.Fail (String.Format ("TryParseInvariant failed on {0}", inv_string));
+        }
+
+        [Test]
+        public void FromToSymmetry ()
+        {
+            // ToTimeT only has precision to the second; so strip off the remainding ticks
+            DateTime now = DateTime.Now;
+            now = now.Subtract (TimeSpan.FromTicks (now.Ticks % TimeSpan.TicksPerSecond));
+
+            long time_t = DateTimeUtil.ToTimeT (now);
+            DateTime now_t = DateTimeUtil.FromTimeT (time_t);
+
+            Assert.AreEqual (DateTimeKind.Local, now.Kind);
+            Assert.AreEqual (DateTimeKind.Local, now_t.Kind);
+            Assert.AreEqual (now, now_t);
+        }
+    }
+}
+
+#endif
diff --git a/src/Libraries/Hyena/Makefile.am b/src/Libraries/Hyena/Makefile.am
index f4961e8..428804f 100644
--- a/src/Libraries/Hyena/Makefile.am
+++ b/src/Libraries/Hyena/Makefile.am
@@ -128,6 +128,7 @@ SOURCES =  \
 	Hyena/PlatformDetection.cs \
 	Hyena/StringUtil.cs \
 	Hyena/Tests/CryptoUtilTests.cs \
+	Hyena/Tests/DateTimeUtilTests.cs \
 	Hyena/Tests/StringUtilTests.cs \
 	Hyena/Tests/TestBase.cs \
 	Hyena/ThreadAssist.cs \



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