using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Container.Common { /// /// 公共类库 /// public class CommonLib { /// /// 日期转换为时间戳(时间戳单位秒) /// /// /// 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; } /// /// 获取当前时间戳 /// /// public static long CurrentTime() { DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return (long)(DateTime.Now.AddHours(-8) - Jan1st1970).TotalSeconds; } /// /// 时间戳转换为日期(时间戳/单位秒) /// /// /// public static DateTime ConvertToDateTime(long timeStamp) { var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return start.AddMilliseconds(timeStamp).AddHours(8); } /// /// 生成随机数字 /// /// 生成长度 /// 是否要在生成前将当前线程阻止以避免重复 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; } /// /// 解密(反转再base64解码),常规配置,例如密码 /// /// /// 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; } } }