Skip to content

Commit b3e1ad9

Browse files
author
linzhijun
committed
fix
1 parent 565f5d2 commit b3e1ad9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

csharp/ToolGood.Algorithm/MyDate.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ public MyDate(int year, int month, int day, int hour, int minute, int second)
6161
Minute = minute;
6262
Second = second;
6363
}
64+
/// <summary>
65+
/// 构造函数
66+
/// </summary>
67+
/// <param name="year">年</param>
68+
/// <param name="month">月</param>
69+
/// <param name="day">日</param>
70+
/// <param name="hour">时</param>
71+
/// <param name="minute">分</param>
72+
/// <param name="second">秒</param>
73+
public MyDate(int? year, int? month, int? day, int hour, int minute, int second)
74+
{
75+
Year = year;
76+
Month = month;
77+
Day = day;
78+
Hour = hour;
79+
Minute = minute;
80+
Second = second;
81+
}
6482

6583
/// <summary>
6684
/// 构造函数
@@ -331,6 +349,10 @@ public MyDate AddDays(int day)
331349
/// <returns></returns>
332350
public MyDate AddHours(int hour)
333351
{
352+
var t = this.Hour + hour;
353+
if (t >= 0 && t < 24) {
354+
return new MyDate(Year, Month, Day, t, Minute, Second);
355+
}
334356
if (Year != null && Year > 1900) {
335357
return new MyDate(ToDateTime().AddHours(hour));
336358
}
@@ -344,6 +366,10 @@ public MyDate AddHours(int hour)
344366
/// <returns></returns>
345367
public MyDate AddMinutes(int minute)
346368
{
369+
var t = this.Minute + minute;
370+
if (t >= 0 && t <= 59) {
371+
return new MyDate(Year, Month, Day, Hour, t, Second);
372+
}
347373
if (Year != null && Year > 1900) {
348374
return new MyDate(ToDateTime().AddMinutes(minute));
349375
}
@@ -357,6 +383,10 @@ public MyDate AddMinutes(int minute)
357383
/// <returns></returns>
358384
public MyDate AddSeconds(int second)
359385
{
386+
var t = this.Second + second;
387+
if (t >= 0 && t <= 59) {
388+
return new MyDate(Year, Month, Day, Hour, Minute, t);
389+
}
360390
if (Year != null && Year > 1900) {
361391
return new MyDate(ToDateTime().AddSeconds(second));
362392
}

0 commit comments

Comments
 (0)