You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.2 KiB

3 years ago
using System;
using System.Text.RegularExpressions;
namespace EC.Utils
{
public class CommonUtil
{
/// <summary>
/// 返回UnixTime 单位秒
/// </summary>
/// <returns>单位秒</returns>
public static int NowUnixTime()
{
long longunixtime = DateUnit.UnixTime() / 1000;
return FormatCom.ToInt(longunixtime.ToString());
}
/// <summary>
/// 将 DateTime 转换为 UnixTime 单位秒
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static int DateTimeToUnixTime(DateTime dateTime)
{
return (int)(dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1))).TotalSeconds;
}
/// <summary>
/// 将 UnixTime 转换为 DateTime 单位秒
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 小数字符串去零
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Trim0(string str)
{
string result = str.TrimEnd('0');
if (result.EndsWith("."))
{
result = result.Remove(result.Length - 1);
}
return result;
}
/// <summary>
/// 判断车牌是否正确
/// </summary>
/// <param name="plate"></param>
/// <returns></returns>
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;
}
/// <param name="str">要舍入的字符串小数</param>
public static decimal StrToDec(string str)
{
try
{
return !string.IsNullOrEmpty(str) ? Convert.ToDecimal(str.Trim()) : 0m;
}
catch (Exception)
{
return 0m;
}
}
/// <summary>
///
/// </summary>
/// <param name="dec"></param>
/// <param name="afterLimit"></param>
/// <param name="patch"># or 0</param>
/// <returns></returns>
public static string WeightDecToStr(decimal dec, int afterLimit = 3, char patch = '#')
{
return dec.ToString("0.".PadRight(afterLimit + 2, patch));
}
/// <param name="str">要舍入的字符串小数</param>
/// <param name="afterLimit">指定数字要舍入到的小数位数的值,范围从 0 到 28。</param>
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+)?$");
}
/// <summary>
/// 在指定时间过后执行指定的表达式
/// </summary>
/// <param name="action">要执行的表达式</param>
/// <param name="interval">时间(以毫秒为单位)</param>
/// <return>返回timer对象</return>
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;
}
/// <summary>
/// 在指定时间周期重复执行指定的表达式
/// </summary>
/// <param name="action">要执行的表达式</param>
/// <param name="interval">时间(以毫秒为单位)</param>
public static void SetInterval(Action action, double interval)
{
var timer = new System.Timers.Timer(interval);
timer.Elapsed += (sender, e) => { action(); };
timer.Enabled = true;
}
///<summary>
///生成随机字符串
///</summary>
///<param name="length">目标字符串的长度</param>
///<param name="useNum">是否包含数字,1=包含,默认为包含</param>
///<param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
///<param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
///<param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
///<returns>指定长度的随机字符串</returns>
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;
}
}
}