Esempio n. 1
0
        /// <summary>
        /// 获取Unix时间戳
        /// </summary>
        /// <param name="time">时间</param>
        public static long GetUnixTimestamp(DateTime time)
        {
            var  start = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.Local);
            long ticks = (time - start.Add(new TimeSpan(8, 0, 0))).Ticks;

            return(H_Convert.ToLong(ticks / TimeSpan.TicksPerSecond));
        }
Esempio n. 2
0
        /// <summary>
        /// 通用泛型转换
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="input">输入值</param>
        public static T To <T>(object value)
        {
            if (value == null)
            {
                return(default(T));
            }
            if (value is string && string.IsNullOrWhiteSpace(value.ToString()))
            {
                return(default(T));
            }
            Type type     = H_Common.GetType <T>();
            var  typeName = type.Name.ToLower();

            try
            {
                if (typeName == "string")
                {
                    return((T)(object)value.ToString());
                }
                if (typeName == "guid")
                {
                    return((T)(object)new Guid(value.ToString()));
                }
                if (type.IsEnum)
                {
                    return(H_Convert.ToEnum <T>(value.ToString()));
                }
                if (value is IConvertible)
                {
                    return((T)Convert.ChangeType(value, type));
                }
                return((T)value);
            }
            catch
            {
                return(default(T));
            }
        }