Created
January 4, 2013 11:34
-
-
Save ChrisMcKee/4451931 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Helper | |
| { | |
| using System; | |
| using System.Configuration; | |
| using System.Globalization; | |
| using System.IO; | |
| using System.Text; | |
| /// <summary> | |
| /// General util method | |
| /// </summary> | |
| public static class Util | |
| { | |
| // Constants required for java date conversion | |
| private const long JavaOffset = 621355968000000000; | |
| private const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| private static readonly DateTime JavaMinimum = new DateTime(1970, 1, 1, 8, 0, 0); | |
| // Required for Random String Generator | |
| private static readonly Random _rng = new Random(); | |
| /// <summary> | |
| /// Resolve a date to its java millisecond equivalent | |
| /// </summary> | |
| /// <param name="dt"> </param> | |
| /// <returns> </returns> | |
| public static long ResolveToJavaMilliseconds(DateTime dt) | |
| { | |
| if (dt < JavaMinimum) | |
| { | |
| throw new ArgumentException(string.Format("DateTime instance cannot be less than {0}", JavaMinimum), "dt"); | |
| } | |
| return (dt.Ticks - JavaOffset)/TimeSpan.TicksPerMillisecond; | |
| } | |
| /// <summary> | |
| /// Get a c# DateTime instance from a java millisecond value | |
| /// </summary> | |
| /// <param name="javaMilliseconds"> </param> | |
| /// <returns> </returns> | |
| public static DateTime GenerateFromJavaMilliseconds(long javaMilliseconds) | |
| { | |
| return new DateTime((javaMilliseconds*TimeSpan.TicksPerMillisecond) + JavaOffset); | |
| } | |
| /// <summary> | |
| /// Get a guid id that can be used in a file system path (folder / file) | |
| /// </summary> | |
| /// <param name="id"> </param> | |
| /// <returns> </returns> | |
| public static string FileSystemSafeGuid(Guid id) | |
| { | |
| return id.ToString().Replace("{", string.Empty).Replace("}", string.Empty); | |
| } | |
| /// <summary> | |
| /// Convert to Int64, with option for of failValue | |
| /// </summary> | |
| /// <param name="valToConvert"> </param> | |
| /// <param name="failValue"> </param> | |
| /// <returns> </returns> | |
| public static long ConvertToInt64(object valToConvert, long failValue) | |
| { | |
| long returnValue = failValue; | |
| if (valToConvert == null) | |
| return returnValue; | |
| if (valToConvert.ToString().Length == 0) | |
| return returnValue; | |
| try | |
| { | |
| returnValue = Convert.ToInt64(valToConvert, CultureInfo.CurrentCulture); | |
| } | |
| catch | |
| { | |
| } | |
| return returnValue; | |
| } | |
| /// <summary> | |
| /// Convert to Int64 | |
| /// </summary> | |
| /// <param name="valToConvert"> </param> | |
| /// <returns> </returns> | |
| public static long ConvertToInt64(object valToConvert) | |
| { | |
| return ConvertToInt64(valToConvert, 0); | |
| } | |
| /// <summary> | |
| /// Prepare a datetime value as an international string format - {ts'2006-06-15 00:00:00.000'} | |
| /// </summary> | |
| /// <param name="value"> </param> | |
| /// <returns> </returns> | |
| public static string PrepareDatabaseDateTime(DateTime value) | |
| { | |
| return string.Format("{{ts'{0:yyyy}-{0:MM}-{0:dd} {0:HH}:{0:mm}:{0:ss}.{0:fff}'}}", value); | |
| } | |
| /// <summary> | |
| /// Prepare a string to be used in an SQL query | |
| /// </summary> | |
| /// <param name="value"> </param> | |
| /// <returns> </returns> | |
| public static string PrepareDatabaseString(string value) | |
| { | |
| return value == null ? string.Empty : value.Replace("'", "''"); | |
| } | |
| /// <summary> | |
| /// Convert sql string literal to database ready | |
| /// </summary> | |
| /// <param name="str"> </param> | |
| /// <returns> </returns> | |
| public static string SafeSqlLiteral(string value) | |
| { | |
| // value = value.Replace("'", "''"); | |
| value = value.Replace("‘", "''"); | |
| value = value.Replace("’", "''"); | |
| return value; | |
| } | |
| /// <summary> | |
| /// Used primarily by the finance app | |
| /// </summary> | |
| /// <param name="val"> </param> | |
| /// <param name="decimalPlaces"> </param> | |
| /// <returns> </returns> | |
| public static double MathFinanceRound(double val, int decimalPlaces) | |
| { | |
| return Math.Round(val, decimalPlaces, MidpointRounding.AwayFromZero); | |
| } | |
| /// <summary> | |
| /// Replace | |
| /// <code> | |
| /// <br /> | |
| /// </code> | |
| /// for line breaks | |
| /// </summary> | |
| /// <param name="value"> </param> | |
| /// <returns> </returns> | |
| public static string ReplaceBRForLineBreaks(string value) | |
| { | |
| return value.Replace("<br />", "\r\n"); | |
| } | |
| /// <summary> | |
| /// Replace line breaks for | |
| /// <code> | |
| /// <br /> | |
| /// </code> | |
| /// </summary> | |
| /// <param name="value"> </param> | |
| /// <returns> </returns> | |
| public static string ReplaceLineBreaksForBR(string value) | |
| { | |
| return value.Replace("\r\n", "<br />"); | |
| } | |
| /// <summary> | |
| /// Reads the Byte[] of a file | |
| /// </summary> | |
| /// <param name="fileLocation"> location of the file to read </param> | |
| /// <returns> file as byte[] </returns> | |
| public static Byte[] GetFileBytes(string fileLocation) | |
| { | |
| if (File.Exists(fileLocation)) | |
| { | |
| var fs = new FileStream(fileLocation, FileMode.Open, FileAccess.Read); | |
| var br = new BinaryReader(fs); | |
| byte[] retval = br.ReadBytes((int) fs.Length); | |
| br.Close(); | |
| fs.Close(); | |
| return retval; | |
| } | |
| return null; | |
| } | |
| /// <summary> | |
| /// Generates A Random String | |
| /// </summary> | |
| /// <param name="size"> Accepts Size Perameter </param> | |
| /// <returns> Random String of Requested Length </returns> | |
| public static string RandomString(int size) | |
| { | |
| var buffer = new char[size]; | |
| for (int i = 0; i < size; i++) | |
| { | |
| buffer[i] = Chars[_rng.Next(Chars.Length)]; | |
| } | |
| return new string(buffer); | |
| } | |
| public static T GetAppSetting<T>(string key, T Default) | |
| { | |
| if (key != null) | |
| { | |
| var value = ConfigurationManager.AppSettings[key]; | |
| if (string.IsNullOrEmpty(value)) | |
| return Default; | |
| if (typeof (T).BaseType == typeof (Enum)) | |
| return (T) Enum.Parse(typeof (T), value); | |
| return (T) Convert.ChangeType(value, typeof (T)); | |
| } | |
| return Default; | |
| } | |
| public static string EncodeTo64(string toEncode) | |
| { | |
| byte[] toEncodeAsBytes = Encoding.ASCII.GetBytes(toEncode); | |
| string returnValue = Convert.ToBase64String(toEncodeAsBytes); | |
| return returnValue; | |
| } | |
| public static string DecodeFrom64(string encodedData) | |
| { | |
| byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData); | |
| string returnValue = Encoding.ASCII.GetString(encodedDataAsBytes); | |
| return returnValue; | |
| } | |
| public static string RemoveAccent(this string s) | |
| { | |
| return Encoding.ASCII.GetString(Encoding.GetEncoding("Cyrillic").GetBytes(s)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment