wxr
2021-07-01 43b0d5870d528f23ecd6aeceb6cfd4325188b46f
HDL_ON/Common/Utlis.cs
@@ -2,15 +2,13 @@
using Shared;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections.Generic;
using System.Collections;
namespace HDL_ON
{
    /// <summary>
    /// 常用工具类
    /// </summary>
    public class Utlis
    public static class Utlis
    {
      /// <summary>
      /// 全局打印
@@ -44,6 +42,7 @@
         {
            Text = mes,
            CloseTime = closeTime,
            MaxWidth = Application.GetRealWidth(300),
            Direction = AMPopTipDirection.None
         };
         tip.Show(MainPage.BaseView);
@@ -242,19 +241,18 @@
         return abc;
      }
        /// <summary>
        ///
        /// </summary>
        /// <param name="unixTimeStamp"></param>
        /// <returns></returns>
        public static DateTime UnixToDateTime(long unixTimeStamp)
      /// <summary>
      /// 获取时间戳(ss)
      /// </summary>
      /// <param name="unixTimeStamp"></param>
      /// <returns></returns>
      public static DateTime UnixToDateTime(long unixTimeStamp)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
            return startTime.AddSeconds(unixTimeStamp);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="unixTimeStamp"></param>
        /// <returns></returns>
@@ -276,7 +274,7 @@
      }
      /// <summary>
        ///
        /// 时间戳转时间字符串
        /// </summary>
        /// <param name="unixTimeStamp"></param>
        /// <param name="format"></param>
@@ -305,15 +303,16 @@
      }
      /// <summary>
      /// 获取当前时间戳值
      /// 获取时间戳(s)
      /// </summary>
      /// <param name="isMilliseconds">是否微秒</param>
      /// <returns></returns>
      public static string GetTimestamp(bool isMilliseconds = true)
      {
         System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
         if (isMilliseconds)
         {
            return ((long)(DateTime.Now - startTime).TotalMilliseconds).ToString(); // 相差秒数
            return ((long)(DateTime.Now - startTime).TotalMilliseconds).ToString(); // 相差微秒数
         }
         else
         {
@@ -329,6 +328,101 @@
         return Language.CurrentLanguage == "Chinese" ? LanguageTypeEnum.CHINESE.ToString() : LanguageTypeEnum.ENGLISH.ToString();
      }
      #region 时间格式转换
      private const int Second = 1;
      private const int Minute = 60 * Second;
      private const int Hour = 60 * Minute;
      private const int Day = 24 * Hour;
      private const int Month = 30 * Day;
      ///// <summary>
      ///// 时间转换
      ///// 少于1天  显示 时分
      ///// 少于一年 显示 月日
      ///// 大于一年 显示 年
      ///// </summary>
      ///// <param name="dateTime"></param>
      ///// <returns></returns>
      //public static string ToFriendlyDisplay(this DateTime dateTime)
      //{
      //   var ts = DateTime.Now - dateTime;
      //   var delta = ts.TotalSeconds;
      //   if (delta < Day)
      //   {
      //      //显示 时:分
      //      return dateTime.ToString("HH:mm");
      //   }
      //   else if (delta < 12 * Month)
      //   {
      //      //显示 月:日
      //      return dateTime.ToString("MM/dd");
      //   }
      //   else
      //   {   //显示 年
      //      return dateTime.ToString("yyyy");
      //   }
      //}
      /// <summary>
      /// 时间转换
      /// 少于1天  显示 时分
      /// 少于一年 显示 月日
      /// 大于一年 显示 年
      /// </summary>
      /// <param name="dateTime"></param>
      /// <returns></returns>
      public static string ToFriendlyDisplay(this DateTime dateTime)
      {
         var nowDateTime = DateTime.Now;
         var subYear = nowDateTime.Year - dateTime.Year;
         var subMonth = nowDateTime.Month - dateTime.Month;
         var subDay = nowDateTime.Day - dateTime.Day;
         //年份差值大于0
         if(subYear > 0)
            {
            //显示 年
            return dateTime.ToString("yyyy");
            }
            else
            {
            //相同年份,月份差值大于0
            if (subMonth > 0)
                {
               //显示 月:日
               return dateTime.ToString("MM/dd");
                }
                else
                {
               //相同月份,日期差值大于0
               if (subDay > 0)
               {
                  //显示 月:日
                  return dateTime.ToString("MM/dd");
               }
               else
               {
                  //同一天显示 时:分
                  return dateTime.ToString("HH:mm");
               }
            }
            }
      }
      /// <summary>
      /// 时间转换
      /// 少于1天  显示 时分
      /// 少于一年 显示 月日
      /// 大于一年 显示 年
      /// </summary>
      /// <param name="dateTime"></param>
      /// <returns></returns>
      public static string ToFriendlyDisplay(long unixTimeStamp)
      {
         return ToFriendlyDisplay(UnixToDateTimeMS(unixTimeStamp));
      }
      #endregion
      
   }