using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.IO; using System.Reflection; using System.Text.RegularExpressions; namespace EC.Utils { public class CommonUtil { /// /// 返回UnixTime 单位秒 /// /// 单位秒 public static int NowUnixTime() { long longunixtime = DateUnit.UnixTime() / 1000; return FormatCom.ToInt(longunixtime.ToString()); } /// /// 将 DateTime 转换为 UnixTime 单位秒 /// /// /// public static int DateTimeToUnixTime(DateTime dateTime) { return (int)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds; } /// /// 将 UnixTime 转换为 DateTime 单位秒 /// /// /// public static DateTime UnixTimeToDateTime(int time) { if (time < 0) throw new ArgumentOutOfRangeException("time is out of range"); return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddSeconds(time); } /// /// 小数字符串去零 /// /// /// public static string Trim0(string str) { string result = str.TrimEnd('0'); if (result.EndsWith(".")) { result = result.Remove(result.Length - 1); } return result; } /// /// 判断车牌是否正确 /// /// /// public static bool IsSuccessPlate(string plate) { int len = plate.Length; if (len == 7) { if (plate[0] > 127) {//汉字 for (int i = 1; i < 7; i++) { if (plate[i] > 127) { return false; } } return true; } } return false; } /// 要舍入的字符串小数 public static decimal StrToDec(string str) { try { return !string.IsNullOrEmpty(str) ? Convert.ToDecimal(str.Trim()) : 0m; } catch (Exception) { return 0m; } } /// /// /// /// /// /// # or 0 /// public static string WeightDecToStr(decimal dec, int afterLimit = 3, char patch = '#') { return dec.ToString("0.".PadRight(afterLimit + 2, patch)); } /// 要舍入的字符串小数 /// 指定数字要舍入到的小数位数的值,范围从 0 到 28。 public static decimal WeightStrToDec(string str, int afterLimit = 3) { try { return Math.Round(Convert.ToDecimal(str), afterLimit); } catch (Exception) { return 0m; } } public static string Reverse(string original) { var arr = original.ToCharArray(); Array.Reverse(arr); return new string(arr); } public static bool IsNumber(string value) { return Regex.IsMatch(value, @"^[-+]?\d+(\.\d+)?$"); } public static bool IsInt(string value) { return Regex.IsMatch(value, @"^[-+]?\d+(\.\d+)?$"); } /// /// 在指定时间过后执行指定的表达式 /// /// 要执行的表达式 /// 时间(以毫秒为单位) /// 返回timer对象 public static void SetTimeout(Action action, double interval) { var timer = new System.Timers.Timer(interval); timer.Elapsed += (sender, e) => { timer.Enabled = false; action(); timer.Dispose(); timer.Close(); timer = null; }; timer.Enabled = true; } /// /// 在指定时间周期重复执行指定的表达式 /// /// 要执行的表达式 /// 时间(以毫秒为单位) public static void SetInterval(Action action, double interval) { var timer = new System.Timers.Timer(interval); timer.Elapsed += (sender, e) => { action(); }; timer.Enabled = true; } /// ///生成随机字符串 /// ///目标字符串的长度 ///是否包含数字,1=包含,默认为包含 ///是否包含小写字母,1=包含,默认为包含 ///是否包含大写字母,1=包含,默认为包含 ///是否包含特殊字符,1=包含,默认为不包含 ///指定长度的随机字符串 public static string GetRandomString(int length, bool useNum = true, bool useLow = true, bool useUpp = true, bool useSpe = true) { var b = new byte[4]; new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b); var r = new Random(BitConverter.ToInt32(b, 0)); string s = string.Empty, str = string.Empty; if (useNum == true) { str += "0123456789"; } if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; } if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; } for (int i = 0; i < length; i++) { s += str.Substring(r.Next(0, str.Length - 1), 1); } return s; } /// /// 读取JSON文件 /// public static JObject ReadFileJson(string jsonFile) { try { using (StreamReader file = File.OpenText(jsonFile)) { using (JsonTextReader reader = new JsonTextReader(file)) { JObject jObject = (JObject)JToken.ReadFrom(reader); return jObject; } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); return null; } } } }