//================================================================================ //================================================================================ #region DateTime conversions public static TimeSpan ToTimeSpan(object o) { TimeSpan value; try { string time = o.ToString(); if (!time.Contains(":")) { int hours = SafeConvert.ToInt(o); if (hours > 23) { time = String.Format("{0:0}:{1:0}:{2:0}", Math.Floor(hours / 24.0), hours % 24, 0); } else { time += ":0"; } } value = TimeSpan.Parse(time); } catch { value = new TimeSpan(0); } return(value); }
public static Int64 SetFlag(this System.Enum type, Int64 flags, bool on = true) { Int64 bit = 1 << (SafeConvert.ToInt(type) - 1); if (on) { return(flags | bit); } else { return(flags | ~bit); } }
/* * public static bool Has<T>(this System.Enum type, T value) * { * try { return (((int)(object)type & (int)(object)value) == (int)(object)value); } * catch { return false; } * } * public static bool Is<T>(this System.Enum type, T value) * { * try { return (int)(object)type == (int)(object)value; } * catch { return false; } * } * public static T Add<T>(this System.Enum type, T value) * { * try { return (T)(object)(((int)(object)type | (int)(object)value)); } * catch (Exception ex) { throw new ArgumentException(String.Format("Could not append value from enumerated type '{0}'.",typeof(T).Name), ex); } * } * public static T Remove<T>(this System.Enum type, T value) * { * try { return (T)(object)(((int)(object)type & ~(int)(object)value)); } * catch (Exception ex) { throw new ArgumentException(String.Format("Could not remove value from enumerated type '{0}'.", typeof(T).Name), ex); } * }*/ public static bool IsFlagSet(this System.Enum type, Int64 flags) { Int64 bit = 1 << (SafeConvert.ToInt(type) - 1); return(bit == (flags & bit)); }