using JiShe.CollectBus.Common.Enums; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; namespace JiShe.CollectBus.Common.Extensions { public static class DateTimeExtensions { /// /// Converts a DateTime to a Unix Timestamp /// /// This DateTime /// [Description("将日期时间转换为Unix时间戳")] public static double ToUnixTimestamp(this DateTime target) { var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); var diff = target - origin; return Math.Floor(diff.TotalSeconds); } /// /// Converts a Unix Timestamp in to a DateTime /// /// This Unix Timestamp /// [Description("将Unix时间戳转换为日期时间")] public static DateTime FromUnixTimestamp(this double unixTime) { var epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0); return epoch.AddSeconds(unixTime); } /// /// Gets the value of the End of the day (23:59) /// /// /// [Description("获取一天结束的值(23:59)")] public static DateTime ToDayEnd(this DateTime target) { return target.Date.AddDays(1).AddMilliseconds(-1); } /// /// Gets the First Date of the week for the specified date /// /// this DateTime /// The Start Day of the Week (ie, Sunday/Monday) /// The First Date of the week [Description("获取指定日期的星期的第一个日期")] public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { var diff = dt.DayOfWeek - startOfWeek; if (diff < 0) diff += 7; return dt.AddDays(-1 * diff).Date; } /// /// Returns all the days of a month. /// /// The year. /// The month. /// [Description("获取一个月的所有日期")] public static IEnumerable DaysOfMonth(int year, int month) { return Enumerable.Range(0, DateTime.DaysInMonth(year, month)) .Select(day => new DateTime(year, month, day + 1)); } /// /// Determines the Nth instance of a Date's DayOfWeek in a month /// /// /// 11/29/2011 would return 5, because it is the 5th Tuesday of each month [Description("获取当前日期在一个月的第几个星期")] public static int WeekDayInstanceOfMonth(this DateTime dateTime) { var y = 0; return DaysOfMonth(dateTime.Year, dateTime.Month) .Where(date => dateTime.DayOfWeek.Equals(date.DayOfWeek)) .Select(x => new { n = ++y, date = x }) .Where(x => x.date.Equals(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day))) .Select(x => x.n).FirstOrDefault(); } /// /// Gets the total days in a month /// /// The date time. /// [Description("获取一个月内的总天数")] public static int TotalDaysInMonth(this DateTime dateTime) { return DaysOfMonth(dateTime.Year, dateTime.Month).Count(); } /// /// Get the first day in a month /// /// The date time. /// [Description("获取一个月内的第一天")] public static DateTime FirstInMonth(this DateTime dateTime) { return DateTime.Now.AddDays(1 - DateTime.Now.Day); } /// /// Get the Last Day in a Month 23:59:59 /// /// /// [Description("获取一个月内的最后一天 23:59:59")] public static DateTime LastInMonth(this DateTime dateTime) { return DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1); } /// /// Takes any date and returns it's value as an Unspecified DateTime /// /// /// [Description("获取Unspecified日期")] public static DateTime ToDateTimeUnspecified(this DateTime date) { if (date.Kind == DateTimeKind.Unspecified) { return date; } return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, DateTimeKind.Unspecified); } /// /// Trims the milliseconds off of a datetime /// /// /// [Description("将日期时间缩短毫秒")] public static DateTime TrimMilliseconds(this DateTime date) { return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind); } /// /// Clears the time. /// /// The date time. /// [Description("清除时间")] public static DateTime ClearTime(this DateTime dateTime) { return dateTime.Subtract( new TimeSpan( 0, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond ) ); } /// /// 获取数据表分片策略 /// /// /// public static string GetDataTableShardingStrategy(this DateTime dateTime, TableTimeStrategyEnum tableStrategy) { switch (tableStrategy) { case TableTimeStrategyEnum.MinuteShardingStrategy: return $"{dateTime:yyyyMMddHHmm}"; case TableTimeStrategyEnum.HourShardingStrategy: return $"{dateTime:yyyyMMddHH}"; case TableTimeStrategyEnum.DayShardingStrategy: return $"{dateTime:yyyyMMdd}"; case TableTimeStrategyEnum.MonthShardingStrategy: return $"{dateTime:yyyyMM}"; case TableTimeStrategyEnum.YearShardingStrategy: return $"{dateTime:yyyy}"; default: return $""; // 默认不分表 } } /// /// 采集时间节点计算 /// /// 待采集时间 /// /// public static DateTime CalculateNextCollectionTime(this DateTime referenceTime, int interval) { // 计算精确到分钟的基准时间 var baseTime = new DateTime( referenceTime.Year, referenceTime.Month, referenceTime.Day, referenceTime.Hour, referenceTime.Minute, 0); // 计算总分钟数和下一个间隔点 int totalMinutes = baseTime.Hour * 60 + baseTime.Minute; int nextTotalMinutes = ((totalMinutes / interval) + 1) * interval; // 处理跨天情况 int daysToAdd = nextTotalMinutes / (24 * 60); int remainingMinutes = nextTotalMinutes % (24 * 60); int hours = remainingMinutes / 60; int minutes = remainingMinutes % 60; return baseTime.Date .AddDays(daysToAdd) .AddHours(hours) .AddMinutes(minutes); } /// /// 格式化为微秒(μs) /// /// /// public static string ToMicrosecondString(this DateTime dt) { long microseconds = (dt.Ticks % TimeSpan.TicksPerSecond) / 10; // 1 Tick = 100ns → 0.1μs return $"{dt:yyyy-MM-dd HH:mm:ss.fffffff}".Remove(23) + $"{microseconds:D6}"; } /// /// 格式化为纳秒(ns) /// /// /// public static string ToNanosecondString(this DateTime dt) { long nanoseconds = (dt.Ticks % TimeSpan.TicksPerSecond) * 100; // 1 Tick = 100ns return $"{dt:yyyy-MM-dd HH:mm:ss.fffffff}".Remove(23) + $"{nanoseconds:D9}"; } /// /// 毫米、微秒、纳秒时间戳转DateTime /// /// /// /// public static DateTime ParseIntToDate(this long dateLong) { if (dateLong < 10000101 || dateLong > 99991231) { throw new ArgumentException("Date must be between 10000101 and 99991231."); } return DateTime.TryParseExact(dateLong.ToString(), "yyyyMMdd HHmmssZZ", null, System.Globalization.DateTimeStyles.None, out DateTime date) ? date : throw new ArgumentException("Date must be between 10000101 and 99991231."); } /// /// 取得某月的第一天 /// /// 要取得月份第一天的时间 /// public static DateTime FirstDayOfMonth(this DateTime datetime) { return datetime.AddDays(1 - datetime.Day); } /// /// 取得某月的最后一天 /// /// 要取得月份最后一天的时间 /// public static DateTime LastDayOfMonth(this DateTime datetime) { return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1); } /// /// 取得上个月第一天 /// /// 要取得上个月第一天的当前时间 /// public static DateTime FirstDayOfPreviousMonth(this DateTime datetime) { return datetime.AddDays(1 - datetime.Day).AddMonths(-1); } /// /// 取得上个月的最后一天 /// /// 要取得上个月最后一天的当前时间 /// public static DateTime LastDayOfPrdviousMonth(this DateTime datetime) { return datetime.AddDays(1 - datetime.Day).AddDays(-1); } /// /// 取得某月第一天0点以及最后一天的23:59:59时间范围 /// /// /// public static Tuple GetMonthDateRange(this DateTime datetime) { var lastDayOfMonthDate = LastDayOfMonth(datetime); return new Tuple(datetime.FirstDayOfMonth(), new DateTime(lastDayOfMonthDate.Year, lastDayOfMonthDate.Month, lastDayOfMonthDate.Day, 23, 59, 59)); } /// /// 取得某一天0点到当月最后一天的23:59:59时间范围 /// /// /// public static Tuple GetCurrentDateToLastDayRange(this DateTime datetime) { var lastDayOfMonthDate = LastDayOfMonth(datetime); return new Tuple(datetime.Date, new DateTime(lastDayOfMonthDate.Year, lastDayOfMonthDate.Month, lastDayOfMonthDate.Day, 23, 59, 59)); } /// /// 取得某一天0点到23:59:59时间范围 /// /// /// public static Tuple GetCurrentDateRange(this DateTime datetime) { return new Tuple(datetime.Date, new DateTime(datetime.Year, datetime.Month, datetime.Day, 23, 59, 59)); } } }