From 2fcf671a2d20dd8e7f69d159cf7bdeaa4b8895aa Mon Sep 17 00:00:00 2001 From: gpiccin Date: Tue, 6 May 2014 22:27:38 -0400 Subject: [PATCH] Added regex expression to handle date conversion. --- source/OFXSharp.Tests/OFXSharp.Tests.csproj | 4 + source/OFXSharp/OFXHelperMethods.cs | 141 +++++++++++--------- 2 files changed, 85 insertions(+), 60 deletions(-) diff --git a/source/OFXSharp.Tests/OFXSharp.Tests.csproj b/source/OFXSharp.Tests/OFXSharp.Tests.csproj index 18e9e44..c1c302d 100644 --- a/source/OFXSharp.Tests/OFXSharp.Tests.csproj +++ b/source/OFXSharp.Tests/OFXSharp.Tests.csproj @@ -45,6 +45,7 @@ + @@ -62,6 +63,9 @@ OFXSharp + + + diff --git a/source/OFXSharp/OFXHelperMethods.cs b/source/OFXSharp/OFXHelperMethods.cs index 3b8b9f5..d1d6abc 100644 --- a/source/OFXSharp/OFXHelperMethods.cs +++ b/source/OFXSharp/OFXHelperMethods.cs @@ -1,61 +1,82 @@ -using System; -using System.IO; -using System.Xml; - -namespace OFXSharp -{ - public static class OFXHelperMethods - { - /// - /// Converts string representation of AccountInfo to enum AccountInfo - /// - /// representation of AccountInfo - /// AccountInfo - public static BankAccountType GetBankAccountType(this string bankAccountType) - { - return (BankAccountType)Enum.Parse(typeof(BankAccountType), bankAccountType); - } - - /// - /// Flips date from YYYYMMDD to DDMMYYYY - /// - /// Date in YYYYMMDD format - /// Date in format DDMMYYYY - public static DateTime ToDate(this string date) - { - try - { - if (date.Length < 8) - { - return new DateTime(); - } - - var dd = Int32.Parse(date.Substring(6, 2)); - var mm = Int32.Parse(date.Substring(4, 2)); - var yyyy = Int32.Parse(date.Substring(0, 4)); - - return new DateTime(yyyy, mm, dd); - } - catch - { - throw new OFXParseException("Unable to parse date"); - } - } - - /// - /// Returns value of specified node - /// - /// Node to look for specified node - /// XPath for node you want - /// - public static string GetValue(this XmlNode node, string xpath) - { - // workaround to search values on root node - var fixedNode = new XmlDocument(); - fixedNode.Load(new StringReader(node.OuterXml)); - - var tempNode = fixedNode.SelectSingleNode(xpath); - return tempNode != null ? tempNode.FirstChild.Value : ""; - } - } +using System; +using System.IO; +using System.Text.RegularExpressions; +using System.Xml; + +namespace OFXSharp +{ + public static class OFXHelperMethods + { + /// + /// Converts string representation of AccountInfo to enum AccountInfo + /// + /// representation of AccountInfo + /// AccountInfo + public static BankAccountType GetBankAccountType(this string bankAccountType) + { + return (BankAccountType)Enum.Parse(typeof(BankAccountType), bankAccountType); + } + + /// + /// Flips date from YYYYMMDD to DDMMYYYY + /// + /// Date in YYYYMMDD format + /// Date in format DDMMYYYY + public static DateTime ToDate(this string date) + { + try + { + string resultDate = null; + + if (IsDateInOFXDefaultEspecification(date, out resultDate)) + { + var dd = Int32.Parse(resultDate.Substring(6, 2)); + var mm = Int32.Parse(resultDate.Substring(4, 2)); + var yyyy = Int32.Parse(resultDate.Substring(0, 4)); + + return new DateTime(yyyy, mm, dd); + } + + return new DateTime(); + } + catch + { + throw new OFXParseException("Unable to parse date"); + } + } + + /// + /// Check if date this in YYYYMMDD format. + /// + /// String containing date. + /// Returns true if format is valid. + public static bool IsDateInOFXDefaultEspecification(string date, out string result) + { + Regex regex = new Regex(@"^\d{4}((0\d)|(1[012]))(([012]\d)|3[01])"); + bool isDateInOFXFormat = regex.IsMatch(date); + + if (isDateInOFXFormat) + result = regex.Match(date).Value; + else + result = null; + + return isDateInOFXFormat; + } + + /// + /// Returns value of specified node + /// + /// Node to look for specified node + /// XPath for node you want + /// + public static string GetValue(this XmlNode node, string xpath) + { + // workaround to search values on root node + var fixedNode = new XmlDocument(); + fixedNode.Load(new StringReader(node.OuterXml)); + + var tempNode = fixedNode.SelectSingleNode(xpath); + return tempNode != null ? tempNode.FirstChild.Value : ""; + } + } } \ No newline at end of file