OSDN Git Service

47a6452b7ced8dad88eeb6fdbed1fbb7697b3958
[opentween/open-tween.git] / OpenTween / DateTimeUtc.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2018 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
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)
10 // any later version.
11 //
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
15 // for more details.
16 //
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.
21
22 using System;
23
24 namespace OpenTween
25 {
26     /// <summary>
27     /// <see cref="DateTimeKind.Utc"/> に固定された <see cref="DateTime"/> を扱うための構造体
28     /// </summary>
29     public struct DateTimeUtc : IComparable<DateTimeUtc>, IEquatable<DateTimeUtc>
30     {
31         public static DateTimeUtc MinValue { get; }
32             = new DateTimeUtc(DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc));
33
34         public static DateTimeUtc MaxValue { get; }
35             = new DateTimeUtc(DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc));
36
37         public static DateTimeUtc UnixEpoch { get; }
38             = new DateTimeUtc(1970, 1, 1, 0, 0, 0);
39
40         public static DateTimeUtc Now
41             => new DateTimeUtc(DateTime.UtcNow);
42
43         private readonly DateTime datetime;
44
45         public DateTimeUtc(int year, int month, int day)
46             : this(year, month, day, hour: 0, minute: 0, second: 0)
47         {
48         }
49
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)
52         {
53         }
54
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))
57         {
58         }
59
60         public DateTimeUtc(DateTimeOffset datetimeOffset)
61             : this(datetimeOffset.UtcDateTime)
62         {
63         }
64
65         public DateTimeUtc(DateTime datetime)
66         {
67             if (datetime.Kind != DateTimeKind.Utc)
68                 throw new ArgumentException("datetime には UTC に変換された時刻が必須です", nameof(datetime));
69
70             this.datetime = datetime;
71         }
72
73         public long ToUnixTime()
74             => (long)((this - UnixEpoch).TotalSeconds);
75
76         public DateTimeOffset ToDateTimeOffset()
77             => new DateTimeOffset(this.datetime);
78
79         public DateTime ToDateTimeUnsafe()
80             => this.datetime;
81
82         public int CompareTo(DateTimeUtc other)
83             => this.datetime.CompareTo(other.datetime);
84
85         public bool Equals(DateTimeUtc other)
86             => this == other;
87
88         public override bool Equals(object obj)
89             => obj is DateTimeUtc other && this.Equals(other);
90
91         public override int GetHashCode()
92             => this.datetime.GetHashCode();
93
94         public static DateTimeUtc operator +(DateTimeUtc a, TimeSpan b)
95             => new DateTimeUtc(a.datetime + b);
96
97         public static DateTimeUtc operator -(DateTimeUtc a, TimeSpan b)
98             => new DateTimeUtc(a.datetime - b);
99
100         public static TimeSpan operator -(DateTimeUtc a, DateTimeUtc b)
101             => a.datetime - b.datetime;
102
103         public static bool operator ==(DateTimeUtc a, DateTimeUtc b)
104             => a.datetime == b.datetime;
105
106         public static bool operator !=(DateTimeUtc a, DateTimeUtc b)
107             => a.datetime != b.datetime;
108
109         public static bool operator <(DateTimeUtc a, DateTimeUtc b)
110             => a.datetime < b.datetime;
111
112         public static bool operator <=(DateTimeUtc a, DateTimeUtc b)
113             => a.datetime <= b.datetime;
114
115         public static bool operator >(DateTimeUtc a, DateTimeUtc b)
116             => a.datetime > b.datetime;
117
118         public static bool operator >=(DateTimeUtc a, DateTimeUtc b)
119             => a.datetime >= b.datetime;
120
121         public static DateTimeUtc FromUnixTime(long unixTime)
122             => UnixEpoch + TimeSpan.FromTicks(unixTime * TimeSpan.TicksPerSecond);
123     }
124 }