SDateTime.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace LYFZ
  5. {
  6. /// <summary>
  7. /// 服务器时间类
  8. /// </summary>
  9. public class SDateTime
  10. {
  11. public SDateTime()
  12. {
  13. }
  14. static bool isAutomaticUpdate = false;
  15. /// <summary>
  16. /// 是否有程序在自动更新本时间类
  17. /// </summary>
  18. public static bool IsAutomaticUpdate
  19. {
  20. get { return SDateTime.isAutomaticUpdate; }
  21. set { SDateTime.isAutomaticUpdate = value; }
  22. }
  23. static DateTime _now = DateTime.Now;
  24. /// <summary>
  25. /// 服务器当前时间
  26. /// </summary>
  27. public static DateTime Now
  28. {
  29. get
  30. {
  31. if (IsAutomaticUpdate && SDateTime._now != null)
  32. {
  33. return SDateTime._now;
  34. }
  35. else
  36. {
  37. return DateTime.Now;
  38. }
  39. }
  40. // set { SDateTime._now = value; }
  41. }
  42. /// <summary>
  43. /// 服务器当前时间转Long
  44. /// </summary>
  45. public static long NowToLong
  46. {
  47. get {
  48. return SDateTime.DateTimeToLong(SDateTime.Now);
  49. }
  50. }
  51. /// <summary>
  52. /// 设置服务器端时间
  53. /// </summary>
  54. /// <param name="t"></param>
  55. public static void SetNow(DateTime t)
  56. {
  57. SDateTime._now = t;
  58. }
  59. /// <summary>
  60. /// 转 20161003160647 格式long为时间类型
  61. /// </summary>
  62. /// <param name="dtLong">20161003160647 格式long</param>
  63. /// <returns></returns>
  64. public static DateTime ConvertLongToDateTime(long dtLong)
  65. {
  66. return LYFZ.WinAPI.CustomPublicMethod.StringToDateTime(dtLong);
  67. }
  68. /// <summary>
  69. /// 转DateTime 为 yyyyMMddHHmmss 格式的 long
  70. /// </summary>
  71. /// <param name="t"></param>
  72. /// <returns></returns>
  73. public static long DateTimeToLong(DateTime t)
  74. {
  75. return LYFZ.WinAPI.CustomPublicMethod.DateTimeToLong(t);
  76. }
  77. }
  78. }