OSDN Git Service

MyCommon.DateTimeParseの返り値をDateTimeUtcに変更
[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 using System.Globalization;
24
25 namespace OpenTween
26 {
27     /// <summary>
28     /// <see cref="DateTimeKind.Utc"/> に固定された <see cref="DateTime"/> を扱うための構造体
29     /// </summary>
30     public struct DateTimeUtc : IComparable<DateTimeUtc>, IEquatable<DateTimeUtc>
31     {
32         public static DateTimeUtc MinValue { get; }
33             = new DateTimeUtc(DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc));
34
35         public static DateTimeUtc MaxValue { get; }
36             = new DateTimeUtc(DateTime.SpecifyKind(DateTime.MaxValue, DateTimeKind.Utc));
37
38         public static DateTimeUtc UnixEpoch { get; }
39             = new DateTimeUtc(1970, 1, 1, 0, 0, 0);
40
41         public static DateTimeUtc Now
42             => new DateTimeUtc(DateTime.UtcNow);
43
44         private readonly DateTime datetime;
45
46         public DateTimeUtc(int year, int month, int day)
47             : this(year, month, day, hour: 0, minute: 0, second: 0)
48         {
49         }
50
51         public DateTimeUtc(int year, int month, int day, int hour, int minute, int second)
52             : this(year, month, day, hour, minute, second, millisecond: 0)
53         {
54         }
55
56         public DateTimeUtc(int year, int month, int day, int hour, int minute, int second, int millisecond)
57             : this(new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Utc))
58         {
59         }
60
61         public DateTimeUtc(DateTimeOffset datetimeOffset)
62             : this(datetimeOffset.UtcDateTime)
63         {
64         }
65
66         public DateTimeUtc(DateTime datetime)
67         {
68             if (datetime.Kind != DateTimeKind.Utc)
69                 throw new ArgumentException("datetime には UTC に変換された時刻が必須です", nameof(datetime));
70
71             this.datetime = datetime;
72         }
73
74         public long ToUnixTime()
75             => (long)((this - UnixEpoch).TotalSeconds);
76
77         public DateTimeOffset ToDateTimeOffset()
78             => new DateTimeOffset(this.datetime);
79
80         public DateTimeOffset ToLocalTime()
81             => this.ToDateTimeOffset().ToLocalTime();
82
83         public DateTime ToDateTimeUnsafe()
84             => this.datetime;
85
86         public int CompareTo(DateTimeUtc other)
87             => this.datetime.CompareTo(other.datetime);
88
89         public bool Equals(DateTimeUtc other)
90             => this == other;
91
92         public override bool Equals(object obj)
93             => obj is DateTimeUtc other && this.Equals(other);
94
95         public override int GetHashCode()
96             => this.datetime.GetHashCode();
97
98         public override string ToString()
99             => this.ToString("G");
100
101         public string ToString(string format)
102             => this.ToDateTimeOffset().ToString(format);
103
104         public string ToLocalTimeString()
105             => this.ToLocalTimeString("G");
106
107         public string ToLocalTimeString(string format)
108             => this.ToLocalTime().ToString(format);
109
110         public static DateTimeUtc operator +(DateTimeUtc a, TimeSpan b)
111             => new DateTimeUtc(a.datetime + b);
112
113         public static DateTimeUtc operator -(DateTimeUtc a, TimeSpan b)
114             => new DateTimeUtc(a.datetime - b);
115
116         public static TimeSpan operator -(DateTimeUtc a, DateTimeUtc b)
117             => a.datetime - b.datetime;
118
119         public static bool operator ==(DateTimeUtc a, DateTimeUtc b)
120             => a.datetime == b.datetime;
121
122         public static bool operator !=(DateTimeUtc a, DateTimeUtc b)
123             => a.datetime != b.datetime;
124
125         public static bool operator <(DateTimeUtc a, DateTimeUtc b)
126             => a.datetime < b.datetime;
127
128         public static bool operator <=(DateTimeUtc a, DateTimeUtc b)
129             => a.datetime <= b.datetime;
130
131         public static bool operator >(DateTimeUtc a, DateTimeUtc b)
132             => a.datetime > b.datetime;
133
134         public static bool operator >=(DateTimeUtc a, DateTimeUtc b)
135             => a.datetime >= b.datetime;
136
137         public static DateTimeUtc FromUnixTime(long unixTime)
138             => UnixEpoch + TimeSpan.FromTicks(unixTime * TimeSpan.TicksPerSecond);
139
140         public static DateTimeUtc Parse(string input, IFormatProvider formatProvider)
141             => new DateTimeUtc(DateTimeOffset.Parse(input, formatProvider, DateTimeStyles.AssumeUniversal));
142
143         public static bool TryParse(string input, IFormatProvider formatProvider, out DateTimeUtc result)
144         {
145             if (DateTimeOffset.TryParse(input, formatProvider, DateTimeStyles.AssumeUniversal, out var datetimeOffset))
146             {
147                 result = new DateTimeUtc(datetimeOffset);
148                 return true;
149             }
150
151             result = MinValue;
152             return false;
153         }
154
155         public static bool TryParseExact(string input, string[] formats, IFormatProvider formatProvider, out DateTimeUtc result)
156         {
157             if (DateTimeOffset.TryParseExact(input, formats, formatProvider, DateTimeStyles.AssumeUniversal, out var datetimeOffset))
158             {
159                 result = new DateTimeUtc(datetimeOffset);
160                 return true;
161             }
162
163             result = MinValue;
164             return false;
165         }
166     }
167 }