1 // OpenTween - Client of Twitter
2 // Copyright (c) 2018 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
27 /// <see cref="DateTimeKind.Utc"/> に固定された <see cref="DateTime"/> を扱うための構造体
29 public struct DateTimeUtc : IComparable<DateTimeUtc>, IEquatable<DateTimeUtc>
31 public static DateTimeUtc MinValue { get; }
32 = new DateTimeUtc(DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc));
34 public static DateTimeUtc MaxValue { get; }
35 = new DateTimeUtc(DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc));
37 public static DateTimeUtc UnixEpoch { get; }
38 = new DateTimeUtc(1970, 1, 1, 0, 0, 0);
40 public static DateTimeUtc Now
41 => new DateTimeUtc(DateTime.UtcNow);
43 private readonly DateTime datetime;
45 public DateTimeUtc(int year, int month, int day)
46 : this(year, month, day, hour: 0, minute: 0, second: 0)
50 public DateTimeUtc(int year, int month, int day, int hour, int minute, int second)
51 : this(year, month, day, hour, minute, second, millisecond: 0)
55 public DateTimeUtc(int year, int month, int day, int hour, int minute, int second, int millisecond)
56 : this(new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Utc))
60 public DateTimeUtc(DateTimeOffset datetimeOffset)
61 : this(datetimeOffset.UtcDateTime)
65 public DateTimeUtc(DateTime datetime)
67 if (datetime.Kind != DateTimeKind.Utc)
68 throw new ArgumentException("datetime には UTC に変換された時刻が必須です", nameof(datetime));
70 this.datetime = datetime;
73 public long ToUnixTime()
74 => (long)((this - UnixEpoch).TotalSeconds);
76 public DateTimeOffset ToDateTimeOffset()
77 => new DateTimeOffset(this.datetime);
79 public DateTime ToDateTimeUnsafe()
82 public int CompareTo(DateTimeUtc other)
83 => this.datetime.CompareTo(other.datetime);
85 public bool Equals(DateTimeUtc other)
88 public override bool Equals(object obj)
89 => obj is DateTimeUtc other && this.Equals(other);
91 public override int GetHashCode()
92 => this.datetime.GetHashCode();
94 public static DateTimeUtc operator +(DateTimeUtc a, TimeSpan b)
95 => new DateTimeUtc(a.datetime + b);
97 public static DateTimeUtc operator -(DateTimeUtc a, TimeSpan b)
98 => new DateTimeUtc(a.datetime - b);
100 public static TimeSpan operator -(DateTimeUtc a, DateTimeUtc b)
101 => a.datetime - b.datetime;
103 public static bool operator ==(DateTimeUtc a, DateTimeUtc b)
104 => a.datetime == b.datetime;
106 public static bool operator !=(DateTimeUtc a, DateTimeUtc b)
107 => a.datetime != b.datetime;
109 public static bool operator <(DateTimeUtc a, DateTimeUtc b)
110 => a.datetime < b.datetime;
112 public static bool operator <=(DateTimeUtc a, DateTimeUtc b)
113 => a.datetime <= b.datetime;
115 public static bool operator >(DateTimeUtc a, DateTimeUtc b)
116 => a.datetime > b.datetime;
118 public static bool operator >=(DateTimeUtc a, DateTimeUtc b)
119 => a.datetime >= b.datetime;
121 public static DateTimeUtc FromUnixTime(long unixTime)
122 => UnixEpoch + TimeSpan.FromTicks(unixTime * TimeSpan.TicksPerSecond);