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.
86 lines
2.7 KiB
86 lines
2.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Container.Common
|
|
{
|
|
/// <summary>
|
|
/// 公共类库
|
|
/// </summary>
|
|
public class CommonLib
|
|
{
|
|
|
|
/// <summary>
|
|
/// 日期转换为时间戳(时间戳单位秒)
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
/// <returns></returns>
|
|
public static long ConvertToTimeStamp(DateTime time)
|
|
{
|
|
DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
return (long)(time.AddHours(-8) - Jan1st1970).TotalSeconds;
|
|
}
|
|
/// <summary>
|
|
/// 获取当前时间戳
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static long CurrentTime()
|
|
{
|
|
DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
return (long)(DateTime.Now.AddHours(-8) - Jan1st1970).TotalSeconds;
|
|
}
|
|
/// <summary>
|
|
/// 时间戳转换为日期(时间戳/单位秒)
|
|
/// </summary>
|
|
/// <param name="timeStamp"></param>
|
|
/// <returns></returns>
|
|
public static DateTime ConvertToDateTime(long timeStamp)
|
|
{
|
|
var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
return start.AddMilliseconds(timeStamp).AddHours(8);
|
|
}
|
|
/// <summary>
|
|
/// 生成随机数字
|
|
/// </summary>
|
|
/// <param name="Length">生成长度</param>
|
|
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
|
|
public static string Number(int Length, bool Sleep)
|
|
{
|
|
if (Sleep) System.Threading.Thread.Sleep(3);
|
|
string result = "";
|
|
System.Random random = new Random();
|
|
for (int i = 0; i < Length; i++)
|
|
{
|
|
result += random.Next(10).ToString();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密(反转再base64解码),常规配置,例如密码
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static string StringDecode(string str)
|
|
{
|
|
string result = string.Empty;
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(str))
|
|
{
|
|
char[] arr = str.ToCharArray();
|
|
Array.Reverse(arr);
|
|
result = Encoding.UTF8.GetString(Convert.FromBase64String(new string(arr)));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|
|
}
|
|
|