UtilTools.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Net;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. #if !COMBAT_SERVER
  8. using UnityEngine;
  9. #endif
  10. using System.IO;
  11. using System.Runtime.Serialization.Formatters.Binary;
  12. #if !COMBAT_SERVER
  13. using UnityEngine.Profiling;
  14. #endif
  15. using System.Security.Cryptography;
  16. using Fort23.UTool;
  17. namespace Utility
  18. {
  19. public static class UtilTools
  20. {
  21. #if !COMBAT_SERVER
  22. public static List<GameObject> allDontDestroyOnLoad = new List<GameObject>();
  23. #endif
  24. private static int m_seed = 0;
  25. private static StringBuilder sb;
  26. public static long oneDay = 86400000;
  27. public static int GenerateSeed()
  28. {
  29. m_seed++;
  30. return m_seed;
  31. }
  32. public static float FrameToDuration(int frame)
  33. {
  34. return frame * 0.02f;
  35. }
  36. public static string GetGUID()
  37. {
  38. System.Guid guid = new Guid();
  39. guid = Guid.NewGuid();
  40. string str = guid.ToString();
  41. return str;
  42. }
  43. #if !COMBAT_SERVER
  44. public static void SetDontDestroyOnLoad(GameObject go)
  45. {
  46. if (allDontDestroyOnLoad.Contains(go))
  47. {
  48. return;
  49. }
  50. GameObject.DontDestroyOnLoad(go);
  51. allDontDestroyOnLoad.Add(go);
  52. }
  53. public static int DurationToFrame(float duration)
  54. {
  55. return (int)(duration / Time.fixedDeltaTime);
  56. }
  57. #endif
  58. // public static int RoundToInt(float value)
  59. // {
  60. // return (int)System.Math.Round(value);
  61. // }
  62. #if !COMBAT_SERVER
  63. public static string CombineString(params string[] strs)
  64. {
  65. StringBuilder sb = new StringBuilder();
  66. for (int i = 0; i < strs.Length; i++)
  67. {
  68. sb.Append(strs[i]);
  69. }
  70. return sb.ToString();
  71. }
  72. public static string CombineString(params object[] strs)
  73. {
  74. if (sb == null)
  75. {
  76. sb = new StringBuilder();
  77. }
  78. sb.Clear();
  79. //StringBuilder sb = new StringBuilder();
  80. for (int i = 0; i < strs.Length; i++)
  81. {
  82. sb.Append(strs[i]);
  83. }
  84. return sb.ToString();
  85. }
  86. // /// <summary>
  87. // /// 获取描述文本
  88. // /// </summary>
  89. // /// <param name="a"></param>
  90. // /// <param name="b"></param>
  91. // /// <returns></returns>
  92. // public static string GetDscString(string a, float[] b)
  93. // {
  94. // if (a == "")
  95. // return "";
  96. // for (int i = 0; i < b?.Length; i++)
  97. // {
  98. // string d = b[i].ToString();
  99. // string c = "{" + i + "}";
  100. // a = a.Replace(c, d);
  101. // }
  102. //
  103. // return a;
  104. // }
  105. /// <summary>
  106. /// 获取描述文本
  107. /// </summary>
  108. /// <param name="a"></param>
  109. /// <param name="b"></param>
  110. /// <returns></returns>
  111. public static string GetDscString(string a, float[] b)
  112. {
  113. if (a == "")
  114. return "";
  115. for (int i = 0; i < b?.Length; i++)
  116. {
  117. string d = b[i].ToString("0.##");
  118. string c = "{" + i + "}";
  119. a = a.Replace(c, d);
  120. }
  121. return a;
  122. }
  123. /// <summary>
  124. /// 获取描述文本
  125. /// </summary>
  126. /// <param name="a"></param>
  127. /// <param name="b"></param>
  128. /// <returns></returns>
  129. public static string GetDscString(string a, string[] b)
  130. {
  131. if (a == "")
  132. return "";
  133. for (int i = 0; i < b?.Length; i++)
  134. {
  135. string d = b[i].ToString();
  136. string c = "{" + i + "}";
  137. a = a.Replace(c, d);
  138. }
  139. return a;
  140. }
  141. public static string GetSignaturesInfo()
  142. {
  143. #if UNITY_ANDROID&&!UNITY_EDITOR
  144. AndroidJavaClass contextClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  145. AndroidJavaObject context = contextClass.GetStatic<AndroidJavaObject>("currentActivity");
  146. AndroidJavaObject packageManager = context.Call<AndroidJavaObject>("getPackageManager");
  147. string packageName = context.Call<string>("getPackageName");
  148. int flags = packageManager.GetStatic<int>("GET_SIGNATURES");
  149. AndroidJavaObject packageInfo =
  150. packageManager.Call<AndroidJavaObject>("getPackageInfo", packageName, flags);
  151. AndroidJavaObject[] signatures = packageInfo.Get<AndroidJavaObject[]>("signatures");
  152. // Get the first signature (assuming only one signature)
  153. AndroidJavaObject signature = signatures[0];
  154. // Convert signature to bytes
  155. byte[] signatureBytes = signature.Call<byte[]>("toByteArray");
  156. // Compute SHA256 hash
  157. SHA256 sha256 = SHA256.Create();
  158. byte[] hashBytes = sha256.ComputeHash(signatureBytes);
  159. // Convert bytes to hex string
  160. StringBuilder builder = new StringBuilder();
  161. for (int i = 0; i < hashBytes.Length; i++)
  162. {
  163. // Debug.Log(hashBytes[i].ToString("x2").ToUpperInvariant());
  164. builder.Append(hashBytes[i].ToString("X2"));
  165. if (i < hashBytes.Length - 1)
  166. {
  167. builder.Append(":");
  168. }
  169. }
  170. string sha256Signature = builder.ToString();
  171. return sha256Signature;
  172. #else
  173. return "";
  174. #endif
  175. }
  176. /// <summary>
  177. /// 正则匹配
  178. /// </summary>
  179. /// <param name="str"></param>
  180. /// <param name="data">"mydata|mydata|"</param>
  181. public static bool MatchingMouna(ref string str, string data)
  182. {
  183. MatchCollection m = Regex.Matches(str, "(" + data + ")", RegexOptions.Multiline);
  184. for (int i = 0; i < m.Count; i++)
  185. {
  186. string m1 = Regex.Replace(m[i].ToString(), "\\$", "\\$");
  187. m1 = Regex.Replace(m1, "\\^", "\\^");
  188. string xingxing = "";
  189. for (int j = 0; j < m1.Length; j++)
  190. xingxing += "*";
  191. str = Regex.Replace(str, m1, xingxing);
  192. }
  193. if (m.Count > 0)
  194. {
  195. return true;
  196. }
  197. else
  198. {
  199. return false;
  200. }
  201. //LogManager.Instance.Log(str + " " + data);
  202. }
  203. /// <summary>
  204. /// 设置对象的层
  205. /// </summary>
  206. /// <param name="layerName"></param>
  207. /// <param name="obj"></param>
  208. /// <param name="isChild"></param>
  209. public static void SetGameObjctLayer(string layerName, GameObject obj, bool isChild = true)
  210. {
  211. int layerIdx = LayerMask.NameToLayer(layerName);
  212. if (isChild)
  213. SetChildLayer(layerIdx, obj.transform);
  214. else
  215. {
  216. obj.layer = layerIdx;
  217. }
  218. }
  219. /// <summary>
  220. /// 设置对象的层
  221. /// </summary>
  222. /// <param name="layerName"></param>
  223. /// <param name="obj"></param>
  224. /// <param name="isChild"></param>
  225. public static void SetGameObjctLayer(int layer, GameObject obj, bool isChild = true)
  226. {
  227. //int layerIdx = LayerMask.NameToLayer(layerName);
  228. if (isChild)
  229. SetChildLayer(layer, obj.transform);
  230. else
  231. {
  232. obj.layer = layer;
  233. }
  234. }
  235. private static void SetChildLayer(int layerIdx, Transform obj)
  236. {
  237. obj.gameObject.layer = layerIdx;
  238. for (int i = 0; i < obj.childCount; i++)
  239. {
  240. SetChildLayer(layerIdx, obj.GetChild(i));
  241. }
  242. }
  243. public static IPEndPoint CombineIPAddress(string host, int port)
  244. {
  245. return new IPEndPoint(IPAddress.Parse(host), port);
  246. }
  247. public static bool IsStringLetterAndNumber(string content)
  248. {
  249. if (content.Length == 0)
  250. {
  251. return false;
  252. }
  253. int asciiCount = Regex.Matches(content, "[a-zA-Z0-9]").Count;
  254. return (content.Length > asciiCount) ? false : true;
  255. }
  256. public static bool IsArrayValid(object array)
  257. {
  258. if (array == null || !(array is Array) || (array as Array).Length == 0)
  259. {
  260. return false;
  261. }
  262. return true;
  263. }
  264. public static Texture2D GetTexture(string path)
  265. {
  266. if (string.IsNullOrEmpty(path))
  267. return null;
  268. if (!File.Exists(path))
  269. return null;
  270. byte[] data = File.ReadAllBytes(path);
  271. Texture2D textrue = new Texture2D(512, 512);
  272. textrue.LoadImage(data);
  273. return textrue;
  274. }
  275. private static string m_bannedWorlds;
  276. public static bool IsBannedWord(string rolename)
  277. {
  278. return false;
  279. }
  280. #if UNITY_EDITOR
  281. /// <summary>
  282. /// 获取场景中全部对象
  283. /// </summary>
  284. /// <typeparam name="T"></typeparam>
  285. /// <returns></returns>
  286. public static List<T> GetAllSceneObj<T>()
  287. {
  288. List<T> ts = new List<T>();
  289. //HierarchyProperty.
  290. //EditorSceneManager.
  291. GameObject[] go = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().GetRootGameObjects();
  292. //LogTool.Log(go.Length);
  293. AddObjcet(ts, go);
  294. go = Utility.UtilTools.allDontDestroyOnLoad.ToArray();
  295. AddObjcet(ts, go);
  296. return ts;
  297. }
  298. //public static string GetModelName(GlobalConfig.LevelType _type, MapUnit unit)
  299. //{
  300. // string str =FixationString.empty;
  301. // switch (unit)
  302. // {
  303. // case MapUnit.Wall:
  304. // str = _type.SplitToString() + "_Wall_";
  305. // break;
  306. // case MapUnit.Coin:
  307. // return _type.SplitToString() + "_Seed";
  308. // case MapUnit.Gate:
  309. // return _type.SplitToString() + "_Gate";
  310. // case MapUnit.Player:
  311. // return "Player";
  312. // case MapUnit.WallVisible:
  313. // return _type.SplitToString() + "_WallInvisible";
  314. // case MapUnit.CoinVisible:
  315. // return _type.SplitToString() + "SeedInvisible";
  316. // case MapUnit.BlockLight:
  317. // case MapUnit.BlockBalance:
  318. // case MapUnit.BlockNormal:
  319. // case MapUnit.BlockSleep:
  320. // case MapUnit.BlockGravity:
  321. // return "Block" + _type.SplitToString();
  322. // case MapUnit.Wall_DestroyAble:
  323. // return "Wall_DestroyAble";
  324. // }
  325. // return str;
  326. //}
  327. /// <summary>
  328. private static void AddObjcet<T>(List<T> ts, GameObject[] go)
  329. {
  330. for (int i = 0; i < go.Length; i++)
  331. {
  332. if (!go[i])
  333. continue;
  334. //if (!go[i].gameObject)
  335. // continue;
  336. T[] objs = go[i].transform.GetComponentsInChildren<T>(true);
  337. for (int j = 0; j < objs.Length; j++)
  338. {
  339. ts.Add(objs[j]);
  340. }
  341. }
  342. }
  343. #endif
  344. public static string GetMD5(string inStr)
  345. {
  346. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  347. byte[] InBytes = Encoding.UTF8.GetBytes(inStr);
  348. byte[] OutBytes = md5.ComputeHash(InBytes);
  349. string OutString = "";
  350. for (int i = 0; i < OutBytes.Length; i++)
  351. {
  352. OutString += OutBytes[i].ToString("x2");
  353. }
  354. //LogTool.Log(OutString);
  355. if (OutString.Length > 8) OutString = OutString.Substring(0, 8);
  356. return OutString;
  357. }
  358. // DES加密
  359. public static string DESEncrypt(string pToEncrypt, string sKey)
  360. {
  361. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  362. byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
  363. // if (sKey.Length > 8) sKey = sKey.Substring(0, 8);
  364. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  365. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  366. MemoryStream ms = new MemoryStream();
  367. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  368. cs.Write(inputByteArray, 0, inputByteArray.Length);
  369. cs.FlushFinalBlock();
  370. StringBuilder ret = new StringBuilder();
  371. foreach (byte b in ms.ToArray())
  372. {
  373. ret.AppendFormat("{0:X2}", b);
  374. }
  375. ret.ToString();
  376. //LogTool.Log(inputByteArray.Length + " 加密后" + ret.SplitToString().Length);
  377. return ret.ToString();
  378. }
  379. // DES解密
  380. public static string DESDecrypt(string pToDecrypt, string sKey)
  381. {
  382. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  383. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  384. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  385. {
  386. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  387. inputByteArray[x] = (byte)i;
  388. }
  389. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  390. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  391. MemoryStream ms = new MemoryStream();
  392. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  393. cs.Write(inputByteArray, 0, inputByteArray.Length);
  394. cs.FlushFinalBlock();
  395. StringBuilder ret = new StringBuilder();
  396. return System.Text.Encoding.UTF8.GetString(ms.ToArray());
  397. }
  398. public static string BASE64_Decode(string data)
  399. {
  400. byte[] bytes = Convert.FromBase64String(data);
  401. return Encoding.UTF8.GetString(bytes);
  402. }
  403. public static string GetMD5HashFromFile(byte[] data)
  404. {
  405. try
  406. {
  407. // FileStream file = new FileStream(filename,FileMode.Open);
  408. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  409. byte[] retVal = md5.ComputeHash(data);
  410. StringBuilder sb = new StringBuilder();
  411. for (int i = 0; i < retVal.Length; i++)
  412. {
  413. sb.Append(retVal[i].ToString("x2"));
  414. }
  415. return sb.ToString();
  416. }
  417. catch (System.Exception ex)
  418. {
  419. LogTool.Log("错误信息:" + ex);
  420. return null;
  421. }
  422. }
  423. public static string FormatTwoTime(int totalSeconds)
  424. {
  425. int minutes = totalSeconds / 60;
  426. string mm = minutes < 10f ? "0" + minutes : minutes.ToString();
  427. int seconds = (totalSeconds - (minutes * 60));
  428. string ss = seconds < 10 ? "0" + seconds : seconds.ToString();
  429. return string.Format("{0}:{1}", mm, ss);
  430. }
  431. // public static string ToStringEx(this long i)
  432. // {
  433. // // if (isShowLongEx)
  434. // {
  435. // if (i >= 10000000000)
  436. // return (i / 1000000000) + "B";
  437. // else if (i >= 10000000)
  438. // return (i / 1000000) + "M";
  439. // else if (i >= 10000)
  440. // return (i / 1000) + "K";
  441. // }
  442. //
  443. // return i.ToString();
  444. // }
  445. /// <summary>
  446. /// 获取目标小数位的str
  447. /// </summary>
  448. /// <param name="n"></param>
  449. /// <returns></returns>
  450. public static string GetTargetDecimalPlaceStr(int n)
  451. {
  452. string str = "";
  453. if (n >= 100) //整数大于两位数保留一位小数
  454. {
  455. int n1 = (int)(n * 10);
  456. n = n1 / 10;
  457. str = n.ToString(CultureInfo.InvariantCulture);
  458. }
  459. else
  460. {
  461. //保留两位小数
  462. int n1 = (int)(n * 100);
  463. n = n1 / 100;
  464. str = n.ToString(CultureInfo.InvariantCulture);
  465. }
  466. return str;
  467. }
  468. public static long Round(this float number)
  469. {
  470. return (long)Math.Ceiling(number);
  471. }
  472. /// <summary>
  473. /// 四舍五入到整数
  474. /// </summary>
  475. /// <param name="value"></param>
  476. /// <returns></returns>
  477. public static int RoundToInt(this float value)
  478. {
  479. // float tempValue = value.Round(0);
  480. // int finalValue = Mathf.FloorToInt(tempValue);
  481. Math.Ceiling(value);
  482. return (int)Math.Ceiling(value);
  483. ;
  484. }
  485. public static string ToStringEx(this long number)
  486. {
  487. // if (number > 1000000000000)
  488. // {
  489. // float n = number / 1000000000000f;
  490. // string str = "";
  491. // str = GetTargetDecimalPlaceStr((int)n);
  492. // return str + "t";
  493. // }
  494. //
  495. // if (number > 1000000000)
  496. // {
  497. // float n = number / 1000000000f;
  498. // string str = "";
  499. // str = GetTargetDecimalPlaceStr((int)n);
  500. // return str + "b";
  501. // }
  502. if (number > 1000000)
  503. {
  504. float n = number / 10000f;
  505. string str = "";
  506. str = GetTargetDecimalPlaceStr((int)n);
  507. return str + "m";
  508. }
  509. if (number > 100000)
  510. {
  511. float n = number / 1000f;
  512. string str = "";
  513. str = GetTargetDecimalPlaceStr((int)n);
  514. return str + "k";
  515. }
  516. return number.ToString();
  517. }
  518. public static string ToStringEx(this int number)
  519. {
  520. // if (number > 1000000000000)
  521. // {
  522. // float n = number / 1000000000000f;
  523. // string str = "";
  524. // str = GetTargetDecimalPlaceStr((int)n);
  525. // return str + "t";
  526. // }
  527. //
  528. // if (number > 1000000000)
  529. // {
  530. // float n = number / 1000000000f;
  531. // string str = "";
  532. // str = GetTargetDecimalPlaceStr((int)n);
  533. // return str + "b";
  534. // }
  535. if (number > 1000000)
  536. {
  537. float n = number / 10000f;
  538. string str = "";
  539. str = GetTargetDecimalPlaceStr((int)n);
  540. return str + "m";
  541. }
  542. if (number > 100000)
  543. {
  544. float n = number / 1000f;
  545. string str = "";
  546. str = GetTargetDecimalPlaceStr((int)n);
  547. return str + "k";
  548. }
  549. return number.ToString();
  550. // // if (isShowLongEx)
  551. // {
  552. // if (i >= 10000000000)
  553. // return (i / 1000000000) + "B";
  554. // else if (i >= 10000000)
  555. // return (i / 1000000) + "M";
  556. // else if (i >= 10000)
  557. // return (i / 1000) + "K";
  558. // }
  559. //
  560. // return i.ToString();
  561. }
  562. #endif
  563. /// <summary>
  564. /// 时间(秒)转换位小时分钟秒
  565. /// </summary>
  566. /// <param name="miao"></param>
  567. /// <returns></returns>
  568. public static string TimeToHSM(long miao)
  569. {
  570. long fen = miao / 60;
  571. miao = miao % 60;
  572. long xiaoShi = fen / 60;
  573. long tian = xiaoShi / 24;
  574. xiaoShi = xiaoShi % 24;
  575. fen = fen % 60;
  576. if (tian > 0)
  577. {
  578. return $"{tian}天{xiaoShi}小时";
  579. }
  580. else
  581. {
  582. if (xiaoShi <= 0 && fen <= 59)
  583. {
  584. return $"{fen}分{miao}秒";
  585. }
  586. else
  587. {
  588. return $"{xiaoShi}小时{fen}分";
  589. }
  590. }
  591. }
  592. /// <summary>
  593. /// 时间(秒)转换位小时分钟秒
  594. /// </summary>
  595. /// <param name="miao"></param>
  596. /// <returns></returns>
  597. public static string TimeToHSM1(long miao)
  598. {
  599. long fen = miao / 60;
  600. miao = miao % 60;
  601. long xiaoShi = fen / 60;
  602. long tian = xiaoShi / 24;
  603. xiaoShi = xiaoShi % 24;
  604. fen = fen % 60;
  605. if (tian <= 0 && xiaoShi <= 0)
  606. {
  607. return $"{fen}分钟";
  608. }
  609. else if (tian <= 0 && xiaoShi > 0)
  610. {
  611. return $"{xiaoShi}小时";
  612. }
  613. else if (tian >= 1)
  614. {
  615. return $"{tian}天";
  616. }
  617. return "";
  618. }
  619. public static string GeiLxTime(long miao)
  620. {
  621. long fen = miao / 60;
  622. miao = miao % 60;
  623. long xiaoShi = fen / 60;
  624. long tian = xiaoShi / 24;
  625. xiaoShi = xiaoShi % 24;
  626. fen = fen % 60;
  627. if (tian <= 0 && xiaoShi <= 0)
  628. {
  629. return $"离线{fen}分钟";
  630. }
  631. else if (tian <= 0 && xiaoShi > 0)
  632. {
  633. return $"离线{xiaoShi}小时";
  634. }
  635. else if (tian >= 1 && tian <= 7)
  636. {
  637. return $"离线{tian}天";
  638. }
  639. else if (tian > 7)
  640. {
  641. return "离线七天以上";
  642. }
  643. return "";
  644. }
  645. /// <summary>
  646. /// 类型1 :小于1天后显示分和秒
  647. /// </summary>
  648. /// <param name="miao"></param>
  649. /// <param name="type1"></param>
  650. /// <returns></returns>
  651. public static string TimeToHSM(long miao, int type1)
  652. {
  653. long fen = miao / 60;
  654. miao = miao % 60;
  655. long xiaoShi = fen / 60;
  656. long tian = xiaoShi / 24;
  657. xiaoShi = xiaoShi % 24;
  658. fen = fen % 60;
  659. if (tian > 0)
  660. {
  661. return $"{tian}天{xiaoShi}小时";
  662. }
  663. else
  664. {
  665. string key = "";
  666. switch (type1)
  667. {
  668. case 1:
  669. return $"{fen}分{miao}秒";
  670. break;
  671. default:
  672. break;
  673. }
  674. return key;
  675. }
  676. }
  677. /// <summary>获取秒级别时间戳(10位)</summary>
  678. public static long GetTimestampToSeconds()
  679. {
  680. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  681. return Convert.ToInt64(ts.TotalSeconds);
  682. }
  683. /// <summary>获取毫秒级别时间戳(13位)</summary>
  684. public static long GetTimeStampToMilliseconds()
  685. {
  686. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  687. return Convert.ToInt64(ts.TotalMilliseconds);
  688. }
  689. public static long GetTimestampToSeconds(int value)
  690. {
  691. DateTime dateTime = new DateTime(value, DateTimeKind.Utc);
  692. TimeSpan ts = dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  693. return Convert.ToInt64(ts.TotalSeconds);
  694. }
  695. /// <summary>
  696. /// 时间戳转时间结构 秒
  697. /// </summary>
  698. /// <param name="unixTimestamp"></param>
  699. /// <returns></returns>
  700. public static DateTime ConvertUnixTimestamp(double unixTimestamp)
  701. {
  702. DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  703. DateTime convertedTime = unixStartTime.AddSeconds(unixTimestamp).ToLocalTime();
  704. return convertedTime;
  705. }
  706. /// <summary>
  707. ///时间戳转时间结构 毫秒
  708. /// </summary>
  709. /// <param name="unixTimestamp"></param>
  710. /// <returns></returns>
  711. public static DateTime ConvertUnixTimestampForMillis(double unixTimestamp)
  712. {
  713. DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  714. DateTime convertedTime = unixStartTime.AddMilliseconds(unixTimestamp).ToLocalTime();
  715. return convertedTime;
  716. }
  717. public static string NumberToArab(int number)
  718. {
  719. switch (number)
  720. {
  721. case 0: return "0";
  722. case 1: return "I";
  723. case 2: return "II";
  724. case 3: return "III";
  725. case 4: return "IV";
  726. case 5: return "V";
  727. case 6: return "VI";
  728. }
  729. return "";
  730. }
  731. /// <summary>
  732. /// 获取凌晨时间戳 单位s
  733. /// </summary>
  734. /// <param name="kind"></param>
  735. /// <returns></returns>
  736. public static long GetMidnightTimestamp(DateTimeKind kind = DateTimeKind.Local)
  737. {
  738. DateTime now = DateTime.Now;
  739. DateTime midnight = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0, kind);
  740. DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  741. TimeSpan timeSpan = midnight.ToUniversalTime() - epoch;
  742. return (long)timeSpan.TotalSeconds;
  743. }
  744. public static T DeepCopy<T>(T obj)
  745. {
  746. using (var memoryStream = new MemoryStream())
  747. {
  748. var formatter = new BinaryFormatter();
  749. formatter.Serialize(memoryStream, obj);
  750. memoryStream.Seek(0, SeekOrigin.Begin);
  751. return (T)formatter.Deserialize(memoryStream);
  752. }
  753. }
  754. }
  755. //UtilTools
  756. }