/*
* 作者:吴隆健
* 日期: 2019-04-23
* 时间: 13:22
*
*/
using System;
namespace MrWu.Time {
///
///
///
public enum TimeStyle {
///
/// 年月日时分秒
///
yyyyMMddhhmmss,
///
/// 年月日时分秒毫秒
///
yyyyMMddhhmmssffff,
///
/// 年月日
///
yyyyMMdd,
///
/// 月日时分秒
///
MMddhhmmss,
///
/// 月日时分秒毫秒
///
MMddhhmmssfff,
///
/// 时分秒
///
hhmmss,
///
/// 毫秒值
///
fff
}
///
/// 自定义时间结构_为了结构体对齐
///
public struct CustomTime {
///
/// 年份的时间基数
///
public const int weight = 1000000;
/// 年 今年第几天*24 + 今天的小时
/// 小时部分 = tm.year % 100 * 100 0000 + tm.DayOfYear* 1440 + tm.hour
///
public int hours;
///
/// 分钟部分
///
public byte minute;
///
/// 秒钟部分
///
public byte second;
///
/// 毫秒部分
///
public ushort millisecond;
///
///
///
///
///
///
///
public CustomTime(int hours, byte minute = 0, byte second = 0, ushort millisecond = 0) {
this.hours = hours;
this.minute = minute;
this.second = second;
this.millisecond = millisecond;
}
///
/// 哪年
///
public int year {
get {
return hours / weight + 2000;
}
}
///
/// 这一天是这一年当中的第几天
///
public int dayofyear {
get {
return hours % weight / 24;
}
}
///
/// 几月
///
public int month {
get {
return TimeConvert.GetMonth(year, dayofyear);
}
}
///
/// 几号
///
public int day {
get {
return TimeConvert.GetDay(year, dayofyear);
}
}
///
/// 几点
///
public int hour {
get {
return hours % weight % 24;
}
}
}
///
/// 时间转换
///
public static class TimeConvert {
///
/// 闰年个个月的天数
///
private readonly static byte[] _leapMonthDay = new byte[] {
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
///
/// 普通年个个月的天数
///
private readonly static byte[] _noleapMonthDay = new byte[] {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
///
/// 一年中每月的天数,润年在2月+1天
///
public static byte[] MonthDay(int year) {
bool isleap = IsLeapYear(year);
if (isleap)
return _leapMonthDay;
return _noleapMonthDay;
}
///
/// 获取这一天是这一年的几月
///
/// 哪年
/// 这一年的第几天
///
public static int GetMonth(int year, int dayofyear) {
bool isleap = IsLeapYear(year);
byte[] bts = MonthDay(year);
int allday = 0;
int len = bts.Length;
for (int i = 0; i < len; i++) {
allday += bts[i];
if (dayofyear <= allday)
return i + 1;
}
throw new ArgumentOutOfRangeException("dayofyear < 1 or dayofyear > 365 or 366");
}
///
/// 获取这一天是这年的几日
///
///
///
///
public static int GetDay(int year, int dayofyear) {
//Console.WriteLine(year + "," + dayofyear);
int month = GetMonth(year, dayofyear);
byte[] bts = MonthDay(year);
int allday = 0;
for (int i = 1; i < month; i++) {
allday += bts[i - 1];
}
return dayofyear - allday;
}
///
/// 某年是否是闰年
///
///
///
public static bool IsLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
///
/// 时间戳起始时间
///
private static readonly DateTime _startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
///
/// 时间转13位时间戳
///
/// 时间
/// true 表示10位时间戳 false 表示13位时间戳
/// 时间戳
public static long DateTimeToTimeStamp(DateTime time, bool bflag = true) {
TimeSpan ts = time - _startTime;
if (bflag)
return Convert.ToInt64(ts.TotalSeconds);
else
return Convert.ToInt64(ts.TotalMilliseconds);
}
///
/// 时间戳转时间
///
/// 时间戳字符串
/// 时间
public static DateTime TimeStampToDateTime(string timeStamp) {
if (timeStamp.Length == 10)
timeStamp += "0000000";
else if (timeStamp.Length == 13)
timeStamp += "0000";
long ltime = long.Parse(timeStamp);
//Console.WriteLine(ltime);
TimeSpan toNow = new TimeSpan(ltime);
return _startTime.Add(toNow);
}
///
/// 自定义时间格式转DateTime
///
/// 自定义时间格式
///
public static DateTime CustomTimeToDateTime(CustomTime ct) {
int year = ct.year;
int month = ct.month;
int day = ct.day;
int hour = ct.hour;
return new DateTime(year, month, day, hour, ct.minute, ct.second, ct.millisecond);
}
///
/// 时间 转自定义时间格式
///
/// 时间
///
public static CustomTime DateTimeToCustomTime(DateTime time) {
int year = time.Year;
int dayofyear = time.DayOfYear;
int hour = time.Hour;
byte minute = (byte)time.Minute;
byte second = (byte)time.Second;
ushort millisecond = (ushort)time.Millisecond;
hour = year % 100 * CustomTime.weight + dayofyear * 24 + hour;
return new CustomTime(hour, minute, second, millisecond);
}
///
/// 时间转字符串
///
/// 时间
/// 字符串格式
/// 年月日以什么字符串链接
/// 时分秒以什么字符串链接
/// 日期部分与时间部分以什么字符链接
///
public static string TimeToString(this DateTime time, TimeStyle style = TimeStyle.yyyyMMddhhmmss, string dateChar = "-", string timeChar = ":", string datetimelink = " ") {
string format = string.Empty;
string dateCharlink = string.Empty;
if (dateChar != string.Empty)
dateCharlink += dateChar;
string timeCharlink = string.Empty;
if (timeChar != string.Empty)
timeCharlink += timeChar;
string dtlink = string.Empty;
if (datetimelink != string.Empty)
dtlink += datetimelink;
switch (style) {
case TimeStyle.yyyyMMddhhmmssffff:
format = string.Format("yyyy{0}MM{0}dd{2}HH{1}mm{1}ss{1}fff", dateCharlink, timeCharlink, dtlink);
break;
case TimeStyle.yyyyMMddhhmmss:
format = string.Format("yyyy{0}MM{0}dd{2}HH{1}mm{1}ss", dateCharlink, timeCharlink, dtlink);
break;
case TimeStyle.yyyyMMdd:
format = string.Format("yyyy{0}MM{0}dd", dateCharlink);
break;
case TimeStyle.MMddhhmmss:
format = string.Format("MM{0}dd{1}HH{2}mm{2}ss", dateCharlink, dtlink, timeCharlink);
break;
case TimeStyle.MMddhhmmssfff:
format = string.Format("MM{0}dd{1}HH{2}mm{2}ss{2}fff", dateCharlink, dtlink, timeCharlink);
break;
case TimeStyle.hhmmss:
format = string.Format("HH{0}mm{0}ss", timeCharlink);
break;
case TimeStyle.fff:
format = string.Format("fff");
break;
}
return time.ToString(format);
}
///
/// 获取到第二天的时间差
///
///
public static TimeSpan GetTomorrowDifference()
{
var tom = DateTime.Now.Date.AddDays(1);
return tom - DateTime.Now;
}
}
}