using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using Sog; namespace Operation { public static class ExComm { public static int Toint32(this string str, int Defaultvalue = 0) { int i; return int.TryParse(str, out i) ? i : Defaultvalue; } public static long Tolong(this string str, long Defaultvalue = 0) { long i; return long.TryParse(str, out i) ? i : Defaultvalue; } public static double ToDouble(this string str, double Defaultvalue = 0) { double i; return double.TryParse(str, out i) ? i : Defaultvalue; } public static float ToFloat(this string str, float Defaultvalue = 0) { float i; return float.TryParse(str, out i) ? i : Defaultvalue; } public static T ToJson(this string str) where T : new() { try { return JsonConfig.parse(str); } catch (Exception ex) { TraceLog.Debug("ExComm.ToJson err msg {0}", ex.Message); return new T(); } } public static string ToJsonstr(this object obj) { try { return JsonConfig.stringify(obj); } catch (Exception ex) { TraceLog.Debug("ExComm.ToJson err msg {0}", ex.Message); return ""; } } //字符串只有字母数字 public static bool AdjustString(string str, int minLen, int maxLen) { if(str.Length < minLen || str.Length > maxLen) { return false; } string pattern = @"^[a-zA-Z0-9]*$"; if(!Regex.IsMatch(str, pattern)) { return false; } return true; } public static int CheckDateTime(DateTime startTime, DateTime endTime) { var TimeSecond = OperationServerUtils.GetTimeStamp(DateTime.Now); var startTimetamp = OperationServerUtils.GetTimeStamp(startTime); var endTimetamp = OperationServerUtils.GetTimeStamp(endTime); if (endTimetamp < 0) { return 0; } if (TimeSecond < startTimetamp) { return -1; } else if (TimeSecond > endTimetamp) { return 1; } return 0; } } }