Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added regex expression to handle date conversion #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions source/OFXSharp.Tests/OFXSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CanParser.cs" />
<Compile Include="OFXHelperMethodUnitTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -62,6 +63,9 @@
<Name>OFXSharp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
141 changes: 81 additions & 60 deletions source/OFXSharp/OFXHelperMethods.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,82 @@
using System;
using System.IO;
using System.Xml;

namespace OFXSharp
{
public static class OFXHelperMethods
{
/// <summary>
/// Converts string representation of AccountInfo to enum AccountInfo
/// </summary>
/// <param name="bankAccountType">representation of AccountInfo</param>
/// <returns>AccountInfo</returns>
public static BankAccountType GetBankAccountType(this string bankAccountType)
{
return (BankAccountType)Enum.Parse(typeof(BankAccountType), bankAccountType);
}

/// <summary>
/// Flips date from YYYYMMDD to DDMMYYYY
/// </summary>
/// <param name="date">Date in YYYYMMDD format</param>
/// <returns>Date in format DDMMYYYY</returns>
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");
}
}

/// <summary>
/// Returns value of specified node
/// </summary>
/// <param name="node">Node to look for specified node</param>
/// <param name="xpath">XPath for node you want</param>
/// <returns></returns>
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
{
/// <summary>
/// Converts string representation of AccountInfo to enum AccountInfo
/// </summary>
/// <param name="bankAccountType">representation of AccountInfo</param>
/// <returns>AccountInfo</returns>
public static BankAccountType GetBankAccountType(this string bankAccountType)
{
return (BankAccountType)Enum.Parse(typeof(BankAccountType), bankAccountType);
}

/// <summary>
/// Flips date from YYYYMMDD to DDMMYYYY
/// </summary>
/// <param name="date">Date in YYYYMMDD format</param>
/// <returns>Date in format DDMMYYYY</returns>
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");
}
}

/// <summary>
/// Check if date this in YYYYMMDD format.
/// </summary>
/// <param name="date">String containing date.</param>
/// <returns>Returns true if format is valid.</returns>
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;
}

/// <summary>
/// Returns value of specified node
/// </summary>
/// <param name="node">Node to look for specified node</param>
/// <param name="xpath">XPath for node you want</param>
/// <returns></returns>
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 : "";
}
}
}