Init
This commit is contained in:
57
MarketDataLib/Utility/CSVLineHelper.cs
Normal file
57
MarketDataLib/Utility/CSVLineHelper.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
public class CSVLineHelper
|
||||
{
|
||||
public CSVLineHelper()
|
||||
{
|
||||
}
|
||||
public static String[] ParseLine(String strLine)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<String> items = new List<String>();
|
||||
int length = strLine.Length;
|
||||
for (int index = 0; index < length; index++)
|
||||
{
|
||||
char ch = strLine[index];
|
||||
if (ch == '"') items.Add(GetQuotedItem(strLine, ref index, length));
|
||||
else items.Add(GetItem(strLine, ref index, length));
|
||||
}
|
||||
return items.ToArray();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private static String GetQuotedItem(String strLine,ref int index,int length)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char ch = '\0';
|
||||
while (index<length)
|
||||
{
|
||||
ch = strLine[++index];
|
||||
if (ch == '"') { index++; break; }
|
||||
if (ch != ',') sb.Append(ch);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
private static String GetItem(String strLine, ref int index, int length)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
char ch='\0';
|
||||
while (ch != ',' && index<length)
|
||||
{
|
||||
ch = strLine[index++];
|
||||
if (ch != ',') sb.Append(ch);
|
||||
}
|
||||
index--;
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
344
MarketDataLib/Utility/DateGenerator.cs
Normal file
344
MarketDataLib/Utility/DateGenerator.cs
Normal file
@@ -0,0 +1,344 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MarketData.Utils;
|
||||
|
||||
// Filename: DateGenerator.cs
|
||||
// Author:Sean Kessler
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
/// <summary>DateGenerator - Generate Historical Dates</summary>
|
||||
[Serializable]
|
||||
public class DateGenerator
|
||||
{
|
||||
private Hashtable holidays=null;
|
||||
public DateGenerator()
|
||||
{
|
||||
}
|
||||
public DateTime GetPrevMonthStart(DateTime dateTime)
|
||||
{
|
||||
DateTime startDate = new DateTime(dateTime.Year, dateTime.Month, 1);
|
||||
startDate = startDate.AddMonths(-1);
|
||||
startDate = GetNextBusinessDay(startDate);
|
||||
return startDate;
|
||||
}
|
||||
public DateTime GetCurrMonthStart(DateTime dateTime)
|
||||
{
|
||||
DateTime startDate = new DateTime(dateTime.Year, dateTime.Month, 1);
|
||||
startDate = GetNextBusinessDay(startDate);
|
||||
return startDate;
|
||||
}
|
||||
public DateGenerator(Hashtable holidays)
|
||||
{
|
||||
this.holidays=holidays;
|
||||
}
|
||||
public static int GetPrevMonth(int month)
|
||||
{
|
||||
if(1==month)return 12;
|
||||
return month-1;
|
||||
}
|
||||
/// <summary>FindPrevBusinessDay - Finds previous business day</summary>
|
||||
/// <returns>DateTime</returns>
|
||||
public DateTime FindPrevBusinessDay(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(1,0,0,0);
|
||||
asOf=asOf.Subtract(oneDay);
|
||||
while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Subtract(oneDay);
|
||||
return asOf;
|
||||
}
|
||||
/// <summary>FindNextBusinessDays - Finds following business day</summary>
|
||||
/// <returns>DateTime</returns>
|
||||
public DateTime FindForwardBusinessDay(DateTime asOf,int daysForward)
|
||||
{
|
||||
for (int index = 0; index < daysForward; index++)
|
||||
{
|
||||
asOf = FindNextBusinessDay(asOf);
|
||||
}
|
||||
return asOf;
|
||||
}
|
||||
/// <summary>FindNextBusinessDay - Finds following business day</summary>
|
||||
/// <returns>DateTime</returns>
|
||||
public DateTime FindNextBusinessDay(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(1,0,0,0);
|
||||
asOf=asOf.Add(oneDay);
|
||||
while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Add(oneDay);
|
||||
return asOf;
|
||||
}
|
||||
/// <summary>GetPrevBusinessDay - Gets previous business day</summary>
|
||||
/// <note>If the given date is a business day then this method will return the given date</note>
|
||||
/// <returns>None</returns>
|
||||
public DateTime GetPrevBusinessDay(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(1,0,0,0);
|
||||
while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Subtract(oneDay);
|
||||
return asOf;
|
||||
}
|
||||
/// <summary>GetNextBusinessDay - Gets next business day</summary>
|
||||
/// <note>If the given date is a business day then this method will return the given date</note>
|
||||
/// <returns>None</returns>
|
||||
public DateTime GetNextBusinessDay(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(1,0,0,0);
|
||||
while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Add(oneDay);
|
||||
return asOf;
|
||||
}
|
||||
/// <summary>GetNextDay - Gets next business day</summary>
|
||||
/// <note>If the given date is a business day then this method will return the given date</note>
|
||||
/// <returns>None</returns>
|
||||
public DateTime GetNextDay(DateTime asOf)
|
||||
{
|
||||
return asOf.Add(Utility.OneDay);
|
||||
}
|
||||
/// <summary>GetPrevFriday - Gets date of prior friday</summary>
|
||||
/// <note>Get the date of the previous friday - if previous Friday is holiday will seek previous business day</note>
|
||||
/// <returns>None</returns>
|
||||
public DateTime GetPrevFriday(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(1,0,0,0);
|
||||
List<DateTime> historicalDates=null;
|
||||
int daysToFetch=7;
|
||||
|
||||
if(DayOfWeek.Friday==asOf.DayOfWeek)
|
||||
{
|
||||
asOf=asOf.Subtract(oneDay);
|
||||
daysToFetch--;
|
||||
}
|
||||
historicalDates=GenerateHistoricalDates(asOf,daysToFetch);
|
||||
for(int index=0;index<historicalDates.Count;index++)
|
||||
{
|
||||
DateTime date=(DateTime)historicalDates[index];
|
||||
if(DayOfWeek.Friday==date.DayOfWeek)return GetPrevBusinessDay(date);
|
||||
}
|
||||
throw new Exception("The week no longer contains Friday?");
|
||||
}
|
||||
public DateTime GetCurrentMonthEnd(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(-1,0,0,0);
|
||||
DateTime date=new DateTime(asOf.Year,asOf.Month,asOf.Day);
|
||||
date=new DateTime(date.Year,date.Month,DateTime.DaysInMonth(asOf.Year,asOf.Month));
|
||||
while(IsWeekend(date)||IsHoliday(asOf))date=date.Add(oneDay);
|
||||
return date;
|
||||
}
|
||||
public DateTime GetNextMonthEnd(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(-1,0,0,0);
|
||||
DateTime date=new DateTime(asOf.Year,asOf.Month,asOf.Day);
|
||||
date=date.AddMonths(1);
|
||||
date=new DateTime(date.Year,date.Month,DateTime.DaysInMonth(date.Year,date.Month));
|
||||
while(IsWeekend(date)||IsHoliday(asOf))date=date.Add(oneDay);
|
||||
return date;
|
||||
}
|
||||
public DateTime GetPrevMonthEnd(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(-1,0,0,0);
|
||||
DateTime date=new DateTime(asOf.Year,asOf.Month,asOf.Day);
|
||||
date=date.AddMonths(-1);
|
||||
date=new DateTime(date.Year,date.Month,DateTime.DaysInMonth(date.Year,date.Month));
|
||||
while(IsWeekend(date)||IsHoliday(asOf))date=date.Add(oneDay);
|
||||
return date;
|
||||
}
|
||||
public DateTime GetPrevMonthEnd(DateTime asOf,int count)
|
||||
{
|
||||
for(int index=0;index<count;index++)
|
||||
{
|
||||
asOf=GetPrevMonthEnd(asOf);
|
||||
}
|
||||
return asOf;
|
||||
}
|
||||
public DateTime EnsureWeekday(DateTime asOf)
|
||||
{
|
||||
TimeSpan oneDay=new TimeSpan(1,0,0,0);
|
||||
if(!IsWeekend(asOf))return asOf;
|
||||
while(IsWeekend(asOf=asOf.Add(oneDay)));
|
||||
return asOf;
|
||||
}
|
||||
public DateTime GenerateHistoricalDate(DateTime startDate,int dayCount)
|
||||
{
|
||||
DateTime histDate;
|
||||
int datedDates=0;
|
||||
TimeSpan singleDay;
|
||||
|
||||
startDate = startDate.Date;
|
||||
histDate=startDate;
|
||||
if(dayCount<0)singleDay=new TimeSpan(-1,0,0,0);
|
||||
else singleDay=new TimeSpan(1,0,0,0);
|
||||
dayCount=dayCount<0?-dayCount:dayCount;
|
||||
while(datedDates<dayCount)
|
||||
{
|
||||
if(IsMarketOpen(ref histDate))datedDates++;
|
||||
histDate=histDate.Subtract(singleDay);
|
||||
}
|
||||
return histDate.Add(singleDay);
|
||||
}
|
||||
// Does not account for weekends etc.,
|
||||
public DateTime GenerateFutureDate(DateTime startDate,int dayCount)
|
||||
{
|
||||
DateTime futureDate;
|
||||
int datedDates=0;
|
||||
TimeSpan singleDay;
|
||||
|
||||
startDate = startDate.Date;
|
||||
futureDate=startDate;
|
||||
singleDay=new TimeSpan(1,0,0,0);
|
||||
while(datedDates<dayCount)
|
||||
{
|
||||
futureDate=futureDate.Add(singleDay);
|
||||
datedDates++;
|
||||
}
|
||||
return futureDate.Add(singleDay);
|
||||
}
|
||||
// Accounts for weekends
|
||||
public DateTime GenerateFutureBusinessDate(DateTime startDate,int dayCount)
|
||||
{
|
||||
DateTime futureDate;
|
||||
int datedDates=0;
|
||||
TimeSpan singleDay;
|
||||
|
||||
startDate = startDate.Date;
|
||||
futureDate=startDate;
|
||||
singleDay=new TimeSpan(1,0,0,0);
|
||||
while(datedDates<dayCount)
|
||||
{
|
||||
if(IsMarketOpen(ref futureDate))datedDates++;
|
||||
futureDate=futureDate.Add(singleDay);
|
||||
}
|
||||
return futureDate.Add(singleDay);
|
||||
}
|
||||
public int DaysBetweenActual(DateTime historicalDate)
|
||||
{
|
||||
DateTime today = DateTime.Now;
|
||||
TimeSpan timeSpan = historicalDate.Date - today;
|
||||
return (int)timeSpan.TotalDays;
|
||||
}
|
||||
public int DaysBetweenActual(DateTime historicalDate,DateTime startingDate)
|
||||
{
|
||||
TimeSpan timeSpan = historicalDate.Date-startingDate;
|
||||
int totalDays = (int)timeSpan.TotalDays;
|
||||
if (historicalDate < startingDate) totalDays = (Math.Abs(totalDays) * -1);
|
||||
else totalDays = Math.Abs(totalDays);
|
||||
return totalDays;
|
||||
}
|
||||
public DateTime DaysAddActual(DateTime date, int daysActual)
|
||||
{
|
||||
TimeSpan days = new TimeSpan(daysActual, 0, 0, 0);
|
||||
return date + days;
|
||||
}
|
||||
public int DaysBetween(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
if (startDate > endDate)
|
||||
{
|
||||
TimeSpan timeSpan = startDate.Date - endDate.Date;
|
||||
return (int)timeSpan.TotalDays;
|
||||
}
|
||||
else
|
||||
{
|
||||
TimeSpan timeSpan = endDate.Date - startDate.Date;
|
||||
return (int)timeSpan.TotalDays;
|
||||
}
|
||||
}
|
||||
public static List<int> GenerateHistoricalYear(int startYear,int years)
|
||||
{
|
||||
List<int> yearsList = new List<int>();
|
||||
for (int index = 0; index < years; index++)
|
||||
{
|
||||
yearsList.Add(startYear);
|
||||
startYear--;
|
||||
}
|
||||
return yearsList;
|
||||
}
|
||||
public List<DateTime> GenerateHistoricalDates(DateTime startDate, int dayCount)
|
||||
{
|
||||
List<DateTime> histDates=new List<DateTime>();
|
||||
DateTime histDate;
|
||||
TimeSpan singleDay;
|
||||
|
||||
startDate = startDate.Date;
|
||||
histDate=startDate;
|
||||
if(dayCount<0)singleDay=new TimeSpan(-1,0,0,0);
|
||||
else singleDay=new TimeSpan(1,0,0,0);
|
||||
dayCount=dayCount<0?-dayCount:dayCount;
|
||||
while(histDates.Count<dayCount)
|
||||
{
|
||||
if(IsMarketOpen(ref histDate))histDates.Add(histDate);
|
||||
histDate=histDate.Subtract(singleDay);
|
||||
}
|
||||
return histDates;
|
||||
}
|
||||
// The function will figure out which date is the most recent and which one is the historical date.
|
||||
// Generally, make startDate the most recent and endDate the historical date.
|
||||
public List<DateTime> GenerateHistoricalDates(DateTime startDate,DateTime endDate)
|
||||
{
|
||||
if (Utility.Epoch.Equals(startDate)||Utility.Epoch.Equals(endDate)) return null;
|
||||
startDate = startDate.Date;
|
||||
endDate = endDate.Date;
|
||||
List<DateTime> histDates = new List<DateTime>();
|
||||
DateTime histDate;
|
||||
TimeSpan singleDay;
|
||||
|
||||
bool reverse = false;
|
||||
if (endDate > startDate)
|
||||
{
|
||||
reverse = true;
|
||||
DateTime swap = endDate;
|
||||
endDate = startDate;
|
||||
startDate = swap;
|
||||
}
|
||||
histDate=startDate;
|
||||
singleDay=new TimeSpan(1,0,0,0);
|
||||
while(histDate>=endDate)
|
||||
{
|
||||
if(IsMarketOpen(ref histDate))histDates.Add(histDate);
|
||||
histDate=histDate.Subtract(singleDay);
|
||||
}
|
||||
if (reverse) histDates.Reverse();
|
||||
return histDates;
|
||||
}
|
||||
// The function will figure out which date is the most recent and which one is the historical date.
|
||||
// Generally, make startDate the most recent and endDate the historical date.
|
||||
public List<DateTime> GenerateHistoricalDatesActual(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
if (Utility.Epoch.Equals(startDate) || Utility.Epoch.Equals(endDate)) return null;
|
||||
startDate = startDate.Date;
|
||||
endDate = endDate.Date;
|
||||
List<DateTime> histDates = new List<DateTime>();
|
||||
DateTime histDate;
|
||||
TimeSpan singleDay;
|
||||
|
||||
bool reverse = false;
|
||||
if (endDate > startDate)
|
||||
{
|
||||
reverse = true;
|
||||
DateTime swap = endDate;
|
||||
endDate = startDate;
|
||||
startDate = swap;
|
||||
}
|
||||
histDate = startDate;
|
||||
singleDay = new TimeSpan(1, 0, 0, 0);
|
||||
while (histDate >= endDate)
|
||||
{
|
||||
histDates.Add(histDate);
|
||||
histDate = histDate.Subtract(singleDay);
|
||||
}
|
||||
if (reverse) histDates.Reverse();
|
||||
return histDates;
|
||||
}
|
||||
private bool IsMarketOpen(ref DateTime dateTime)
|
||||
{
|
||||
if(IsWeekend(dateTime)||IsHoliday(dateTime))return false;
|
||||
return true;
|
||||
}
|
||||
public bool IsWeekend(DateTime dateTime)
|
||||
{
|
||||
if(DayOfWeek.Sunday==dateTime.DayOfWeek || DayOfWeek.Saturday==dateTime.DayOfWeek)return true;
|
||||
return false;
|
||||
}
|
||||
public bool IsHoliday(DateTime dateTime)
|
||||
{
|
||||
if(null==holidays)return false;
|
||||
return holidays.Contains(dateTime.Date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
28
MarketDataLib/Utility/DateRange.cs
Normal file
28
MarketDataLib/Utility/DateRange.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
public class DateRange : List<DateTime>
|
||||
{
|
||||
public DateRange(DateTime startDate,DateTime endDate)
|
||||
{
|
||||
startDate = startDate.Date;
|
||||
endDate=endDate.Date;
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
List<DateTime> dates= dateGenerator.GenerateHistoricalDates(startDate, endDate);
|
||||
foreach (DateTime date in dates) Add(date);
|
||||
Sort(); // ensure that no matter how we set this up, the oldest date winds up being at the lowest index of the list
|
||||
}
|
||||
public DateTime StartDate
|
||||
{
|
||||
get { return this[0]; }
|
||||
}
|
||||
public DateTime EndDate
|
||||
{
|
||||
get { return this[Count-1]; }
|
||||
}
|
||||
}
|
||||
}
|
||||
159
MarketDataLib/Utility/MDTrace.cs
Normal file
159
MarketDataLib/Utility/MDTrace.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
using System.Net;
|
||||
//using System.Runtime.Remoting.Messaging;
|
||||
|
||||
// Filename: MarketDataTrace.cs
|
||||
// Author:Sean Kessler
|
||||
// Date:11/2005
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
public enum LogLevel : uint
|
||||
{
|
||||
NONE=0x0000,
|
||||
INFO=0x0002,
|
||||
DEBUG=0x0004,
|
||||
VERBOSE=0x0008
|
||||
};
|
||||
/// <summary>MarketDataTrace - Utility for .</summary>
|
||||
public class MDTrace
|
||||
{
|
||||
private static LogLevel logLevel=LogLevel.DEBUG;
|
||||
|
||||
/// <summary>MarketDataTrace - Private constructor prevents instantiation.</summary>
|
||||
/// <returns>none</returns>
|
||||
private MDTrace()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>LogLevel - Get/Set Log level.</summary>
|
||||
/// <param name="logLevel">The log level.</param>
|
||||
/// <returns>LogLevel</returns>
|
||||
public static LogLevel LogLevel
|
||||
{
|
||||
get{return logLevel;}
|
||||
set{logLevel=value;}
|
||||
}
|
||||
/// <summary>WriteLine - Writes a line of text to trace log.</summary>
|
||||
/// <param name="message">string content of message to write.</param>
|
||||
/// <returns>void</returns>
|
||||
// [Conditional("TRACE")]
|
||||
public static void Write(LogLevel logLevel,string message)
|
||||
{
|
||||
if(MDTrace.logLevel<logLevel)return;
|
||||
Trace.Write(GetCallerIP()+GetThreadRep()+GetLogLevelRep()+"["+DateTime.Now.ToString()+"]"+GetMethodInfo()+message);
|
||||
Console.Write(message);
|
||||
Flush();
|
||||
}
|
||||
|
||||
/// <summary>WriteLine - Writes a line of text to trace log.</summary>
|
||||
/// <param name="message">string content of message to write.</param>
|
||||
/// <returns>void</returns>
|
||||
// [Conditional("TRACE")]
|
||||
public static void WriteLine(string message)
|
||||
{
|
||||
WriteLine(LogLevel.DEBUG,GetCallerIP()+GetThreadRep()+GetLogLevelRep()+"["+DateTime.Now.ToString()+"]"+" "+GetMethodInfo()+message);
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
/// <summary>WriteLine - Writes a line of text to trace log.</summary>
|
||||
/// <param name="message">string content of message to write.</param>
|
||||
/// <returns>void</returns>
|
||||
// [Conditional("TRACE")]
|
||||
public static void WriteLine(LogLevel logLevel,Exception exception)
|
||||
{
|
||||
if(MDTrace.logLevel<logLevel)return;
|
||||
Trace.WriteLine(GetCallerIP()+GetThreadRep()+GetLogLevelRep()+"["+DateTime.Now.ToString()+"]"+" "+GetMethodInfo()+exception.ToString());
|
||||
Console.WriteLine(exception);
|
||||
Flush();
|
||||
}
|
||||
/// <summary>WriteLine - Writes a line of text to trace log.</summary>
|
||||
/// <param name="message">string content of message to write.</param>
|
||||
/// <returns>void</returns>
|
||||
// [Conditional("TRACE")]
|
||||
public static void WriteLine(LogLevel logLevel,string message)
|
||||
{
|
||||
if(MDTrace.logLevel<logLevel)return;
|
||||
Trace.WriteLine(GetCallerIP()+GetThreadRep()+GetLogLevelRep()+"["+DateTime.Now.ToString()+"]"+" "+GetMethodInfo()+message);
|
||||
Console.WriteLine(message);
|
||||
Flush();
|
||||
}
|
||||
/// <summary>Indent - set trace log indentation.</summary>
|
||||
/// <returns>void</returns>
|
||||
// [Conditional("TRACE")]
|
||||
public static void Indent()
|
||||
{
|
||||
Trace.Indent();
|
||||
}
|
||||
/// <summary>Unindent - set trace log indentation back.</summary>
|
||||
/// <returns>void</returns>
|
||||
// [Conditional("TRACE")]
|
||||
public static void Unindent()
|
||||
{
|
||||
Trace.Unindent();
|
||||
}
|
||||
/// <summary>Flush - Flush trace log buffers to disk.</summary>
|
||||
/// <returns>void</returns>
|
||||
// [Conditional("TRACE")]
|
||||
public static void Flush()
|
||||
{
|
||||
Trace.Flush();
|
||||
}
|
||||
/// <summary>GetLogLevel - Return current log level.</summary>
|
||||
/// <returns>LogLevel</returns>
|
||||
public static LogLevel GetLogLevel(String strLogLevel)
|
||||
{
|
||||
if(strLogLevel.Equals("debug"))return LogLevel.DEBUG;
|
||||
else if(strLogLevel.Equals("verbose"))return LogLevel.VERBOSE;
|
||||
else if(strLogLevel.Equals("info"))return LogLevel.INFO;
|
||||
else return LogLevel.NONE;
|
||||
}
|
||||
|
||||
/// <summary>GetLogLevel - Return current log level.</summary>
|
||||
/// <returns>LogLevel</returns>
|
||||
private static string GetLogLevelRep()
|
||||
{
|
||||
if (MDTrace.logLevel == LogLevel.DEBUG) return "[TRACE.DEBUG]";
|
||||
else if (MDTrace.logLevel == LogLevel.VERBOSE) return "[TRACE.VERBOSE]";
|
||||
else if (MDTrace.logLevel == LogLevel.INFO) return "[TRACE.INFO]";
|
||||
else return "[TRACE.NONE]";
|
||||
}
|
||||
/// <summary>GetThreadRep - Return threading information.</summary>
|
||||
/// <returns>LogLevel</returns>
|
||||
private static string GetThreadRep()
|
||||
{
|
||||
return "[Thread="+Thread.CurrentThread.GetHashCode()+"]";
|
||||
}
|
||||
/// <summary>GetMethodInfo - Returns information about the calling method 2 frames up.</summary>
|
||||
/// <returns>String</returns>
|
||||
private static String GetMethodInfo()
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
StackFrame frame=new StackFrame(2,true);
|
||||
MethodBase methodBase=frame.GetMethod();
|
||||
ParameterInfo[] parameters=methodBase.GetParameters();
|
||||
sb.Append("[").Append(methodBase.DeclaringType.FullName).Append("::").Append(methodBase.Name).Append("(");
|
||||
for(int index=0;index<parameters.Length;index++)
|
||||
{
|
||||
ParameterInfo parameter=(ParameterInfo)parameters[index];
|
||||
sb.Append(parameter.Name);
|
||||
if(!(index==parameters.Length-1))sb.Append(",");
|
||||
}
|
||||
sb.Append(")]");
|
||||
return sb.ToString();
|
||||
}
|
||||
/// <summary>GetCallerIP - Returns the calling methods IP address.</summary>
|
||||
/// <returns>String</returns>
|
||||
private static String GetCallerIP()
|
||||
{
|
||||
String hostName = Dns.GetHostName();
|
||||
String hostAddress = Dns.GetHostAddresses(hostName).ToString();
|
||||
if(null!=hostName)hostName=hostName.Split('.')[0];
|
||||
if(null==hostName && null==hostAddress)return "[LOCAL]";
|
||||
return "["+hostAddress+"->"+hostName+"]";
|
||||
}
|
||||
}
|
||||
}
|
||||
99
MarketDataLib/Utility/NVP.cs
Normal file
99
MarketDataLib/Utility/NVP.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
public class NVPDictionary : Dictionary<String,NVP>
|
||||
{
|
||||
public NVPDictionary()
|
||||
{
|
||||
}
|
||||
}
|
||||
public class NVPCollections : List<NVPCollection>
|
||||
{
|
||||
public NVPCollections()
|
||||
{
|
||||
}
|
||||
public NVPCollections(List<String> nvpCollections)
|
||||
{
|
||||
foreach(String nvpCollectionString in nvpCollections)Add(new NVPCollection(nvpCollectionString));
|
||||
}
|
||||
public List<String> ToList()
|
||||
{
|
||||
List<String> nvpCollections=new List<String>();
|
||||
foreach(NVPCollection nvpCollection in this)nvpCollections.Add(nvpCollection.ToString());
|
||||
return nvpCollections;
|
||||
}
|
||||
}
|
||||
public class NVPCollection : List<NVP>
|
||||
{
|
||||
public NVPCollection()
|
||||
{
|
||||
}
|
||||
public NVPCollection(String nvpCollectionString)
|
||||
{
|
||||
if(null==nvpCollectionString)return;
|
||||
String[] nvpItems=nvpCollectionString.Split('|');
|
||||
if(null==nvpItems)return;
|
||||
for(int index=0;index<nvpItems.Length;index++)
|
||||
{
|
||||
Add(new NVP(nvpItems[index]));
|
||||
}
|
||||
}
|
||||
public NVPDictionary ToDictionary()
|
||||
{
|
||||
NVPDictionary dict=new NVPDictionary();
|
||||
foreach(NVP nvp in this)dict.Add(nvp.Name,nvp);
|
||||
return dict;
|
||||
}
|
||||
public static NVPCollection FromString(String strNVPCollection)
|
||||
{
|
||||
return new NVPCollection(strNVPCollection);
|
||||
}
|
||||
public override String ToString()
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
for(int index=0;index<Count;index++)
|
||||
{
|
||||
NVP nvp=this[index];
|
||||
sb.Append(nvp.ToString());
|
||||
if(index<Count-1)sb.Append("|");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
public class NVP
|
||||
{
|
||||
public NVP(String name,String value)
|
||||
{
|
||||
Name=name;
|
||||
Value=value;
|
||||
}
|
||||
public NVP(String nvpString)
|
||||
{
|
||||
if(null==nvpString)return;
|
||||
String[] nvps=nvpString.Split('=');
|
||||
if(2!=nvps.Length)return;
|
||||
Name=nvps[0].Trim();
|
||||
Value=nvps[1].Trim();
|
||||
}
|
||||
public T Get<T>()
|
||||
{
|
||||
T result=default(T);
|
||||
try {result = (T)Convert.ChangeType(Value, typeof(T));}
|
||||
catch {result = default(T);}
|
||||
return result;
|
||||
}
|
||||
public String Name{get;set;}
|
||||
public String Value{get;set;}
|
||||
public override String ToString()
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append(Name).Append("=").Append(Value);
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
43
MarketDataLib/Utility/Profiler.cs
Normal file
43
MarketDataLib/Utility/Profiler.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
// Filename: Profiler.cs
|
||||
// Author:Sean Kessler
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
/// <summary>Profiler - Profiler utility class</summary>
|
||||
public class Profiler
|
||||
{
|
||||
[DllImport("kernel32.dll")]
|
||||
static extern uint GetTickCount();
|
||||
private uint elapsedTime;
|
||||
private uint totalTime;
|
||||
|
||||
public Profiler()
|
||||
{
|
||||
totalTime = GetTickCount();
|
||||
Start();
|
||||
}
|
||||
public void Reset()
|
||||
{
|
||||
totalTime = GetTickCount();
|
||||
Start();
|
||||
}
|
||||
public void Start()
|
||||
{
|
||||
elapsedTime = GetTickCount();
|
||||
}
|
||||
public uint Stop()
|
||||
{
|
||||
return elapsedTime = GetTickCount() - elapsedTime;
|
||||
}
|
||||
public uint End()
|
||||
{
|
||||
return totalTime = GetTickCount() - totalTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
93
MarketDataLib/Utility/UpdateManager.cs
Normal file
93
MarketDataLib/Utility/UpdateManager.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
public class UpdateManager
|
||||
{
|
||||
private StreamWriter streamWriter=null;
|
||||
private FileStream fileStream=null;
|
||||
private Dictionary<String,String> entries=new Dictionary<String,String>();
|
||||
|
||||
public UpdateManager()
|
||||
{
|
||||
}
|
||||
public bool Prepare(String strPathFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
String currentWorkingDirectory=Directory.GetCurrentDirectory();
|
||||
if(!File.Exists(strPathFileName)||IsExpired(strPathFileName))
|
||||
{
|
||||
if(File.Exists(strPathFileName))File.Delete(strPathFileName);
|
||||
fileStream=new FileStream(strPathFileName,FileMode.Create);
|
||||
streamWriter=new StreamWriter(fileStream);
|
||||
Console.WriteLine(String.Format("Creating session file:{0}",strPathFileName));
|
||||
}
|
||||
else
|
||||
{
|
||||
FileStream fileStream=new FileStream(strPathFileName,FileMode.Open);
|
||||
StreamReader streamReader=new StreamReader(fileStream);
|
||||
String strLine=null;
|
||||
while(null!=(strLine=streamReader.ReadLine()))
|
||||
{
|
||||
if(!entries.ContainsKey(strLine))entries.Add(strLine,strLine);
|
||||
}
|
||||
Console.WriteLine(String.Format("Loaded {0} entries from session file:{1}",entries.Count,strPathFileName));
|
||||
streamReader.Close();
|
||||
streamReader.Dispose();
|
||||
fileStream.Close();
|
||||
fileStream.Dispose();
|
||||
fileStream=new FileStream(strPathFileName,FileMode.Append);
|
||||
streamWriter=new StreamWriter(fileStream);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
Console.WriteLine(String.Format("Exception:{0}",exception));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private bool IsExpired(String strPathFileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
DateTime creationTime=File.GetCreationTime(strPathFileName);
|
||||
int daysElapsed=Math.Abs(dateGenerator.DaysBetweenActual(creationTime,DateTime.Now));
|
||||
if(daysElapsed>5)
|
||||
{
|
||||
Console.WriteLine(String.Format("{0} is expired. {1} days old.",strPathFileName,daysElapsed));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
Console.WriteLine(String.Format("Exception:{0}",exception));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public List<String> Entries
|
||||
{
|
||||
get{return new List<String>(entries.Keys);}
|
||||
}
|
||||
public void Add(String entry)
|
||||
{
|
||||
if(null==streamWriter)return;
|
||||
streamWriter.WriteLine(entry);
|
||||
streamWriter.Flush();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
if(null!=streamWriter){streamWriter.Close();streamWriter.Dispose();}
|
||||
if(null!=fileStream){fileStream.Close();fileStream.Dispose();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
561
MarketDataLib/Utility/Utility.cs
Normal file
561
MarketDataLib/Utility/Utility.cs
Normal file
@@ -0,0 +1,561 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.IO.Compression;
|
||||
using System.IO;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.Win32;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
using MarketData.MarketDataModel;
|
||||
|
||||
namespace MarketData.Utils
|
||||
{
|
||||
public class Utility
|
||||
{
|
||||
private static DateTime epoch = DateTime.Parse("01-01-0001");
|
||||
private static TimeSpan oneDay=new TimeSpan(1,0,0,0);
|
||||
public static String Pad(string str, char filler, int length)
|
||||
{
|
||||
int stringLength = str.Length;
|
||||
if (stringLength >= length) return str;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (stringLength < length)
|
||||
{
|
||||
sb.Append(filler);
|
||||
stringLength++;
|
||||
}
|
||||
return sb.ToString() + str;
|
||||
}
|
||||
public static String RemoveHtml(String strItem)
|
||||
{
|
||||
String[] codes = { "'","»" };
|
||||
if(null==strItem)return strItem;
|
||||
foreach (String code in codes)
|
||||
{
|
||||
strItem = strItem.Replace(code,"'");
|
||||
}
|
||||
return strItem;
|
||||
// String str=strItem.Replace("'","'");
|
||||
// return str;
|
||||
}
|
||||
public static String RemoveDivs(String strItem)
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
bool inDiv=false;
|
||||
if(null==strItem)return strItem;
|
||||
for(int index=0;index<strItem.Length;index++)
|
||||
{
|
||||
char ch=strItem[index];
|
||||
if(ch.Equals('<'))inDiv=true;
|
||||
else if(ch.Equals('>'))inDiv=false;
|
||||
else if(!inDiv)sb.Append(ch);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String BetweenString(String strItem, String strBegin, String strEnd)
|
||||
{
|
||||
if (null == strItem) return null;
|
||||
int index=-1;
|
||||
if(null==strBegin)index=0;
|
||||
else index = strItem.IndexOf(strBegin);
|
||||
if (-1 == index) return null;
|
||||
String str = null;
|
||||
if(null!=strBegin)str=strItem.Substring(index + strBegin.Length);
|
||||
else str=strItem;
|
||||
if(null==strEnd)return str;
|
||||
index = str.IndexOf(strEnd);
|
||||
if (-1 == index) return null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int strIndex = 0; strIndex < str.Length; strIndex++)
|
||||
{
|
||||
if (index == strIndex) break;
|
||||
sb.Append(str[strIndex]);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String RemoveAfter(String strItem, char charItem)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int index = 0; index < strItem.Length; index++)
|
||||
{
|
||||
char ch = strItem[index];
|
||||
if (ch.Equals(charItem)) break;
|
||||
sb.Append(ch);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
public static bool OutOfRange(double value)
|
||||
{
|
||||
return value > 100000000000000000000.00 || value< -99999999999999999999.99;
|
||||
}
|
||||
public static String RemoveControlChars(String strItem)
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
for(int index=0;index<strItem.Length;index++)
|
||||
{
|
||||
char ch=strItem[index];
|
||||
if(!char.IsControl(ch))sb.Append(ch);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String GetPath(String strPathFileName)
|
||||
{
|
||||
int index=strPathFileName.LastIndexOf('\\');
|
||||
if (-1 == index) return null;
|
||||
String strPath = strPathFileName.Substring(0, index);
|
||||
return strPath;
|
||||
}
|
||||
public static String KeepBefore(String strItem,String strKeepBefore)
|
||||
{
|
||||
int startPos=strItem.IndexOf(strKeepBefore);
|
||||
if(-1==startPos)return null;
|
||||
return strItem.Substring(0,startPos);
|
||||
}
|
||||
public static String KeepAfter(String strItem,String strKeepAfter)
|
||||
{
|
||||
int startPos=strItem.IndexOf(strKeepAfter);
|
||||
if(-1==startPos)return null;
|
||||
return strItem.Substring(startPos+strKeepAfter.Length);
|
||||
}
|
||||
public static String KeepAfterLast(String strItem,String strKeepAfter)
|
||||
{
|
||||
if(null==strItem)return null;
|
||||
int startPos=strItem.LastIndexOf(strKeepAfter);
|
||||
if(-1==startPos)return null;
|
||||
return strItem.Substring(startPos+strKeepAfter.Length);
|
||||
}
|
||||
public static String Find(String strItem,String search,char delimeter)
|
||||
{
|
||||
if(null==strItem)return null;
|
||||
bool foundDelimeter=false;
|
||||
StringBuilder sb=new StringBuilder();
|
||||
int startPos=strItem.LastIndexOf(search);
|
||||
if(-1==startPos)return null;
|
||||
startPos+=search.Length;
|
||||
for(;startPos<strItem.Length;startPos++)
|
||||
{
|
||||
char ch=strItem[startPos];
|
||||
if(ch.Equals(delimeter))
|
||||
{
|
||||
foundDelimeter=true;
|
||||
break;
|
||||
}
|
||||
sb.Append(ch);
|
||||
}
|
||||
if(!foundDelimeter)return null;
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String FindFirst(String strItem,String search,char delimeter)
|
||||
{
|
||||
if(null==strItem)return null;
|
||||
bool foundDelimeter=false;
|
||||
StringBuilder sb=new StringBuilder();
|
||||
int startPos=strItem.IndexOf(search);
|
||||
if(-1==startPos)return null;
|
||||
startPos+=search.Length;
|
||||
for(;startPos<strItem.Length;startPos++)
|
||||
{
|
||||
char ch=strItem[startPos];
|
||||
if(ch.Equals(delimeter))
|
||||
{
|
||||
foundDelimeter=true;
|
||||
break;
|
||||
}
|
||||
sb.Append(ch);
|
||||
}
|
||||
if(!foundDelimeter)return null;
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String AddQuotes(String item)
|
||||
{
|
||||
return "\"" + item + "\"";
|
||||
}
|
||||
public static long DateToLong(DateTime date)
|
||||
{
|
||||
int year = date.Year;
|
||||
int month = date.Month;
|
||||
int day = date.Day;
|
||||
return (year * 10000) + (month * 100) + day;
|
||||
}
|
||||
public static DateTime LongToDate(long longDate)
|
||||
{
|
||||
int year = (int)(longDate / 10000);
|
||||
int month = ((int)longDate / 100) - year * 100;
|
||||
int day = (int)(longDate - ((int)(longDate / 100)) * 100);
|
||||
return new DateTime(year, month, day);
|
||||
}
|
||||
public static String DateTimeToStringHHMMSS(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("HH:mm:ss");
|
||||
}
|
||||
public static String DateTimeToStringMMSDDSYYYY(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("MM/dd/yyyy");
|
||||
}
|
||||
public static String DateTimeToStringMMSYYYY(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("MM/yyyy");
|
||||
}
|
||||
public static String DateTimeToStringMMSYY(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("MM/yy");
|
||||
}
|
||||
public static String DateTimeToStringMMSDDSYYYYHHMMSS(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("MM/dd/yyyy hh:mm:ss");
|
||||
}
|
||||
public static String DateTimeToStringMMHDDHYYYY(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("MM-dd-yyyy");
|
||||
}
|
||||
public static String DateTimeToStringYYYYHMMHDD(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("yyyy-MM-dd");
|
||||
}
|
||||
public static String DateTimeToStringYYYYHMMHDDHHMMSS(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("yyyy-MM-dd HH:MM:ss");
|
||||
}
|
||||
public static String DateTimeToStringYYYYMMDD(DateTime dateTime)
|
||||
{
|
||||
if (Utility.IsEpoch(dateTime)) return "";
|
||||
return dateTime.ToString("yyyyMMdd");
|
||||
}
|
||||
public static String AsteriskForString(String str)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int length = str.Length;
|
||||
for (int index = 0; index < length; index++) sb.Append("*");
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String FormatNumber(double number)
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
if (double.NaN.Equals(number))sb.Append("N/A");
|
||||
else sb.Append(String.Format("{0:0.000}", number));
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static String FormatNumberConstrain(double value,int places=1,bool commas=false)
|
||||
{
|
||||
String strValue=null;
|
||||
if(value>=1000)
|
||||
{
|
||||
value/=1000;
|
||||
strValue=Utility.FormatNumber(value,places,commas)+"K";
|
||||
}
|
||||
else if(value>=1000000)
|
||||
{
|
||||
value/=1000000;
|
||||
strValue=Utility.FormatNumber(value,places,commas)+"M";
|
||||
}
|
||||
else if(value>=1000000000)
|
||||
{
|
||||
value/=1000000000;
|
||||
strValue=Utility.FormatNumber(value,places,commas)+"B";
|
||||
}
|
||||
else
|
||||
{
|
||||
strValue=Utility.FormatNumber(value,places,commas);
|
||||
}
|
||||
return strValue;
|
||||
}
|
||||
public static String FormatNumber(double number,int places,bool commas=false)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
StringBuilder formatString=new StringBuilder();
|
||||
if (commas&&number>=1000.00) formatString.Append("{0:0,0.");
|
||||
else formatString.Append("{0:0.");
|
||||
for(int index=0;index<places;index++)formatString.Append("0");
|
||||
formatString.Append("}");
|
||||
if (double.NaN.Equals(number)) sb.Append("N/A");
|
||||
else sb.Append(String.Format(formatString.ToString(), number));
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String FormatCurrency(double number)
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
if (double.NaN.Equals(number))sb.Append("N/A");
|
||||
else sb.Append(String.Format("{0:C}", number));
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String FormatCurrency(double number,int decimals)
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
String currencyFormat="{0:C"+decimals+"}";
|
||||
if (double.NaN.Equals(number))sb.Append("N/A");
|
||||
else sb.Append(String.Format(currencyFormat, number));
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String FormatPercent(double number)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (double.NaN.Equals(number)) sb.Append("N/A");
|
||||
else sb.Append(String.Format("{0:P}", number));
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String ConformDate(String strDate)
|
||||
{
|
||||
String[] elements=strDate.Split(' ');
|
||||
if(elements.Length<3)return strDate;
|
||||
return elements[0]+" "+elements[1]+" "+elements[2];
|
||||
}
|
||||
public static DateTime ParseDate(String strDate)
|
||||
{
|
||||
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-US");
|
||||
String[] formats = new[] {"dddd, MMMM dd","MMM dd yyyy","yyyy-MM","ddd, MMM. d","ddd, MMM. dd", "yyyy/MM/dd","M-d-yyyy", "dd-MM-yyyy", "MM-dd-yyyy", "M.d.yyyy", "dd.MM.yyyy", "MM.dd.yyyy","yyyyMMdd" }.Union(cultureInfo.DateTimeFormat.GetAllDateTimePatterns()).ToArray();
|
||||
strDate = strDate.Trim();
|
||||
DateTime dateTime=DateTime.ParseExact(strDate, formats, new System.Globalization.CultureInfo("en-US"), DateTimeStyles.AssumeLocal);
|
||||
return dateTime;
|
||||
}
|
||||
public static double ParsePercent(String strPercent)
|
||||
{
|
||||
if (null == strPercent) return double.NaN;
|
||||
strPercent = strPercent.Replace("%", null);
|
||||
try { return double.Parse(strPercent)/100.00; }
|
||||
catch (Exception) { return double.NaN; }
|
||||
}
|
||||
public static double ParseCurrency(String strNumber)
|
||||
{
|
||||
if (null == strNumber) return double.NaN;
|
||||
if(Constants.CONST_DASHES.Equals(strNumber))return double.NaN;
|
||||
strNumber = strNumber.Replace("$", null);
|
||||
strNumber = strNumber.Replace(",", null);
|
||||
strNumber = strNumber.Replace("(", "-");
|
||||
strNumber = strNumber.Replace(")", null);
|
||||
if (strNumber.Equals("")) return double.NaN;
|
||||
try { return double.Parse(strNumber); }
|
||||
catch (Exception) { return double.NaN; }
|
||||
}
|
||||
public static String FormatCurrencyWithQuotes(double number)
|
||||
{
|
||||
return AddQuotes(FormatCurrency(number));
|
||||
}
|
||||
public static String FormatDates(DateTime d1, DateTime d2)
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
sb.Append(Utility.DateTimeToStringMMSDDSYYYY(d1)).Append("-");
|
||||
sb.Append(Utility.DateTimeToStringMMSDDSYYYY(d2));
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String TrimToSpace(String strString)
|
||||
{
|
||||
if (null == strString) return strString;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int index = 0; index < strString.Length; index++)
|
||||
{
|
||||
char ch=strString[index];
|
||||
if (' '.Equals(ch)) break;
|
||||
sb.Append(ch);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
public static String BooleanToYesNoString(bool booleanValue)
|
||||
{
|
||||
return booleanValue ? "Yes" : "No";
|
||||
}
|
||||
public static bool IsEpoch(DateTime dateTime)
|
||||
{
|
||||
return dateTime.Equals(epoch);
|
||||
}
|
||||
public static DateTime Epoch
|
||||
{
|
||||
get { return epoch; }
|
||||
}
|
||||
public static TimeSpan OneDay
|
||||
{
|
||||
get{return oneDay;}
|
||||
}
|
||||
public static String ListToString(List<String> list,char separator=',')
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
if (null == list || 0 == list.Count) return null;
|
||||
for(int index=0;index<list.Count;index++)
|
||||
{
|
||||
sb.Append(list[index]);
|
||||
if(index<list.Count-1)sb.Append(separator);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
public static List<String> ToList(String items,char separator=',')
|
||||
{
|
||||
List<String> list = items.Split(separator).ToList<String>();
|
||||
list=(from String s in list select s.Trim()).ToList<String>();
|
||||
return list;
|
||||
}
|
||||
public static String FromList(List<String> items,String postFix=",")
|
||||
{
|
||||
StringBuilder sb=new StringBuilder();
|
||||
for(int index=0;index<items.Count;index++)
|
||||
{
|
||||
sb.Append(items[index]);
|
||||
if(index<items.Count-1)sb.Append(postFix);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
public static byte[] Compress(String strString)
|
||||
{
|
||||
MemoryStream outputStream = null;
|
||||
GZipStream compressionStream = null;
|
||||
try
|
||||
{
|
||||
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strString);
|
||||
outputStream = new MemoryStream();
|
||||
compressionStream = new GZipStream(outputStream, CompressionMode.Compress, true);
|
||||
compressionStream.Write(bytes, 0, bytes.Length);
|
||||
compressionStream.Close();
|
||||
compressionStream = null;
|
||||
byte[] outputBytes = outputStream.ToArray();
|
||||
outputStream.Close();
|
||||
outputStream = null;
|
||||
return outputBytes;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != compressionStream)
|
||||
{
|
||||
compressionStream.Close();
|
||||
compressionStream = null;
|
||||
}
|
||||
if (null != outputStream)
|
||||
{
|
||||
outputStream.Close();
|
||||
outputStream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static String Decompress(byte[] compressedBytes)
|
||||
{
|
||||
MemoryStream compressedStream = new MemoryStream(compressedBytes);
|
||||
GZipStream decompressionStream = new GZipStream(compressedStream, CompressionMode.Decompress,true);
|
||||
MemoryStream outputStream=new MemoryStream();
|
||||
try
|
||||
{
|
||||
byte[] decompressedBytesBuffer = new byte[4096];
|
||||
int count = 0;
|
||||
while (true)
|
||||
{
|
||||
count = decompressionStream.Read(decompressedBytesBuffer, 0, decompressedBytesBuffer.Length);
|
||||
if (count > 0) outputStream.Write(decompressedBytesBuffer, 0, count);
|
||||
else break;
|
||||
}
|
||||
decompressionStream.Close();
|
||||
compressedStream.Close();
|
||||
String strDecompressed = System.Text.Encoding.UTF8.GetString(outputStream.ToArray());
|
||||
outputStream.Close();
|
||||
outputStream = null;
|
||||
compressedStream = null;
|
||||
decompressionStream = null;
|
||||
return strDecompressed;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,exception);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != outputStream)
|
||||
{
|
||||
outputStream.Close();
|
||||
outputStream = null;
|
||||
}
|
||||
if (null != decompressionStream)
|
||||
{
|
||||
decompressionStream.Close();
|
||||
decompressionStream = null;
|
||||
}
|
||||
if (null != compressedStream)
|
||||
{
|
||||
compressedStream.Close();
|
||||
compressedStream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void LaunchBrowserSearch(String searchTerm)
|
||||
{
|
||||
Process.Start("https://www.google.com/search?q="+Uri.EscapeDataString(searchTerm)+"/");
|
||||
// OpenWebCommand = new Command(async () => await Browser.OpenAsync("https://www.google.com"));
|
||||
}
|
||||
public static bool IsZeroOrNaN(double value)
|
||||
{
|
||||
return IsNaN(value)||IsZero(value);
|
||||
}
|
||||
private static bool IsZero(double value)
|
||||
{
|
||||
if(value==0.00)return true;
|
||||
return false;
|
||||
}
|
||||
private static bool IsNaN(double value)
|
||||
{
|
||||
return double.IsNaN(value);
|
||||
}
|
||||
public static void DeleteFile(String pathFileName)
|
||||
{
|
||||
if(!File.Exists(pathFileName))return;
|
||||
try{File.Delete(pathFileName);}catch(Exception){;}
|
||||
}
|
||||
private static DateTime GetRunDate(String strPathFileName)
|
||||
{
|
||||
DateTime runDate=DateTime.Now.Date;
|
||||
DateGenerator dateGenerator=new DateGenerator();
|
||||
StreamWriter streamWriter=null;
|
||||
StreamReader streamReader=null;
|
||||
|
||||
try
|
||||
{
|
||||
if(!File.Exists(strPathFileName))
|
||||
{
|
||||
streamWriter=File.CreateText(strPathFileName);
|
||||
streamWriter.WriteLine(Utility.DateTimeToStringMMHDDHYYYY(runDate));
|
||||
streamWriter.Flush();
|
||||
streamWriter.Close();
|
||||
streamWriter=null;
|
||||
return runDate;
|
||||
}
|
||||
streamReader=File.OpenText(strPathFileName);
|
||||
String strLine=streamReader.ReadLine();
|
||||
streamReader.Close();
|
||||
streamReader=null;
|
||||
runDate=Utility.ParseDate(strLine);
|
||||
if(dateGenerator.DaysBetweenActual(runDate,DateTime.Now)>5)
|
||||
{
|
||||
File.Delete(strPathFileName);
|
||||
runDate=DateTime.Now.Date;
|
||||
streamWriter=File.CreateText(strPathFileName);
|
||||
streamWriter.WriteLine(Utility.DateTimeToStringMMHDDHYYYY(runDate));
|
||||
streamWriter.Flush();
|
||||
streamWriter.Close();
|
||||
streamWriter=null;
|
||||
return runDate;
|
||||
}
|
||||
return runDate;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("GetRunDate:{0}",exception.ToString()));
|
||||
return runDate;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=streamWriter)streamWriter.Close();
|
||||
if(null!=streamReader)streamReader.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user