From: Kimura Youichi Date: Tue, 8 May 2018 17:50:30 +0000 (+0900) Subject: MyCommon.DateTimeParseの返り値をDateTimeUtcに変更 X-Git-Tag: OpenTween_v1.4.2~10^2~2 X-Git-Url: http://git.osdn.net/view?p=opentween%2Fopen-tween.git;a=commitdiff_plain;h=be8343a170aefde66f1326afc93e4487a9d92dcc MyCommon.DateTimeParseの返り値をDateTimeUtcに変更 --- diff --git a/OpenTween.Tests/DateTimeUtcTest.cs b/OpenTween.Tests/DateTimeUtcTest.cs index da4c9fdc..ec223976 100644 --- a/OpenTween.Tests/DateTimeUtcTest.cs +++ b/OpenTween.Tests/DateTimeUtcTest.cs @@ -302,5 +302,24 @@ namespace OpenTween Assert.Equal(expectedParsed, parsed); Assert.Equal(expectedResult, result); } + + public static TheoryData TryParseExact_Test_Fixtures = new TheoryData + { + { "2018-05-06 11:22:33.111", "yyyy-MM-dd HH:mm:ss.fff", true, new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111) }, + { "2018-05-06 11:22:33.111 +00:00", "yyyy-MM-dd HH:mm:ss.fff zzz", true, new DateTimeUtc(2018, 5, 6, 11, 22, 33, 111) }, + { "2018-05-06 11:22:33.111 +09:00", "yyyy-MM-dd HH:mm:ss.fff zzz", true, new DateTimeUtc(2018, 5, 6, 2, 22, 33, 111) }, + { "2018-05-06 11:22:33.111", "yyyy/MM/dd HH:mm:ss", false, DateTimeUtc.MinValue }, + { "### INVALID ###", "yyyy-MM-dd HH:mm:ss.fff", false, DateTimeUtc.MinValue }, + }; + + [Theory] + [MemberData(nameof(TryParseExact_Test_Fixtures))] + public void TryParseExact_Test(string input, string format, bool expectedParsed, DateTimeUtc expectedResult) + { + var parsed = DateTimeUtc.TryParseExact(input, new[] { format }, DateTimeFormatInfo.InvariantInfo, out var result); + + Assert.Equal(expectedParsed, parsed); + Assert.Equal(expectedResult, result); + } } } diff --git a/OpenTween.Tests/MyCommonTest.cs b/OpenTween.Tests/MyCommonTest.cs index cffd7a92..8f7a65f8 100644 --- a/OpenTween.Tests/MyCommonTest.cs +++ b/OpenTween.Tests/MyCommonTest.cs @@ -122,27 +122,16 @@ namespace OpenTween Assert.Equal(expected, MyCommon.IsAnimatedGif(filename)); } - public static IEnumerable DateTimeParse_TestCase + public static TheoryData DateTimeParse_TestCase = new TheoryData { - get - { - yield return new object[] { - "Sun Nov 25 06:10:00 +00:00 2012", - new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc), - }; - yield return new object[] { - "Sun, 25 Nov 2012 06:10:00 +00:00", - new DateTime(2012, 11, 25, 6, 10, 0, DateTimeKind.Utc), - }; - } - } + { "Sun Nov 25 06:10:00 +00:00 2012", new DateTimeUtc(2012, 11, 25, 6, 10, 0) }, + { "Sun, 25 Nov 2012 06:10:00 +00:00", new DateTimeUtc(2012, 11, 25, 6, 10, 0) }, + }; [Theory] [MemberData(nameof(DateTimeParse_TestCase))] - public void DateTimeParseTest(string date, DateTime excepted) - { - Assert.Equal(excepted, MyCommon.DateTimeParse(date).ToUniversalTime()); - } + public void DateTimeParseTest(string date, DateTimeUtc excepted) + => Assert.Equal(excepted, MyCommon.DateTimeParse(date)); [DataContract] public struct JsonData diff --git a/OpenTween/DateTimeUtc.cs b/OpenTween/DateTimeUtc.cs index a527b8c2..38d5727a 100644 --- a/OpenTween/DateTimeUtc.cs +++ b/OpenTween/DateTimeUtc.cs @@ -151,5 +151,17 @@ namespace OpenTween result = MinValue; return false; } + + public static bool TryParseExact(string input, string[] formats, IFormatProvider formatProvider, out DateTimeUtc result) + { + if (DateTimeOffset.TryParseExact(input, formats, formatProvider, DateTimeStyles.AssumeUniversal, out var datetimeOffset)) + { + result = new DateTimeUtc(datetimeOffset); + return true; + } + + result = MinValue; + return false; + } } } diff --git a/OpenTween/MyCommon.cs b/OpenTween/MyCommon.cs index 1ed1596b..86bc8d40 100644 --- a/OpenTween/MyCommon.cs +++ b/OpenTween/MyCommon.cs @@ -812,29 +812,19 @@ namespace OpenTween } } - public static DateTime DateTimeParse(string input) + public static DateTimeUtc DateTimeParse(string input) { - string[] format = { + var formats = new[] { "ddd MMM dd HH:mm:ss zzzz yyyy", "ddd, d MMM yyyy HH:mm:ss zzzz", }; - foreach (var fmt in format) - { - if (DateTime.TryParseExact(input, - fmt, - DateTimeFormatInfo.InvariantInfo, - DateTimeStyles.None, - out var rslt)) - { - return rslt; - } - else - { - continue; - } - } + + if (DateTimeUtc.TryParseExact(input, formats, DateTimeFormatInfo.InvariantInfo, out var result)) + return result; + TraceOut("Parse Error(DateTimeFormat) : " + input); - return new DateTime(); + + return DateTimeUtc.Now; } public static T CreateDataFromJson(string content) diff --git a/OpenTween/Twitter.cs b/OpenTween/Twitter.cs index 80fd1747..a6640645 100644 --- a/OpenTween/Twitter.cs +++ b/OpenTween/Twitter.cs @@ -718,7 +718,7 @@ namespace OpenTween { var retweeted = status.RetweetedStatus; - post.CreatedAt = new DateTimeUtc(MyCommon.DateTimeParse(retweeted.CreatedAt).ToUniversalTime()); + post.CreatedAt = MyCommon.DateTimeParse(retweeted.CreatedAt); //Id post.RetweetedId = retweeted.Id; @@ -777,7 +777,7 @@ namespace OpenTween } else { - post.CreatedAt = new DateTimeUtc(MyCommon.DateTimeParse(status.CreatedAt).ToUniversalTime()); + post.CreatedAt = MyCommon.DateTimeParse(status.CreatedAt); //本文 post.TextFromApi = status.FullText; entities = status.MergedEntities; @@ -1177,7 +1177,7 @@ namespace OpenTween } //sender_id //recipient_id - post.CreatedAt = new DateTimeUtc(MyCommon.DateTimeParse(message.CreatedAt).ToUniversalTime()); + post.CreatedAt = MyCommon.DateTimeParse(message.CreatedAt); //本文 var textFromApi = message.Text; //HTMLに整形 @@ -2045,7 +2045,7 @@ namespace OpenTween { Eventtype = MyCommon.EVENTTYPE.Retweet, Event = "retweet", - CreatedAt = new DateTimeUtc(MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value).ToUniversalTime()), + CreatedAt = MyCommon.DateTimeParse(xElm.XPathSelectElement("/created_at").Value), IsMe = xElm.XPathSelectElement("/user/id_str").Value == this.UserId.ToString(), Username = xElm.XPathSelectElement("/user/screen_name").Value, Target = string.Format("@{0}:{1}", new[] @@ -2074,7 +2074,7 @@ namespace OpenTween } var evt = new FormattedEvent(); - evt.CreatedAt = new DateTimeUtc(MyCommon.DateTimeParse(eventData.CreatedAt).ToUniversalTime()); + evt.CreatedAt = MyCommon.DateTimeParse(eventData.CreatedAt); evt.Event = eventData.Event; evt.Username = eventData.Source.ScreenName; evt.IsMe = evt.Username.ToLowerInvariant().Equals(this.Username.ToLowerInvariant()); diff --git a/OpenTween/UserInfo.cs b/OpenTween/UserInfo.cs index a026174f..e6fa174b 100644 --- a/OpenTween/UserInfo.cs +++ b/OpenTween/UserInfo.cs @@ -55,13 +55,13 @@ namespace OpenTween this.Protect = user.Protected; this.FriendsCount = user.FriendsCount; this.FollowersCount = user.FollowersCount; - this.CreatedAt = new DateTimeUtc(MyCommon.DateTimeParse(user.CreatedAt).ToUniversalTime()); + this.CreatedAt = MyCommon.DateTimeParse(user.CreatedAt); this.StatusesCount = user.StatusesCount; this.Verified = user.Verified; if (user.Status != null) { this.RecentPost = user.Status.FullText; - this.PostCreatedAt = new DateTimeUtc(MyCommon.DateTimeParse(user.Status.CreatedAt).ToUniversalTime()); + this.PostCreatedAt = MyCommon.DateTimeParse(user.Status.CreatedAt); this.PostSource = user.Status.Source; } } diff --git a/OpenTween/UserInfoDialog.cs b/OpenTween/UserInfoDialog.cs index c2ee6dc5..a87603d4 100644 --- a/OpenTween/UserInfoDialog.cs +++ b/OpenTween/UserInfoDialog.cs @@ -101,7 +101,7 @@ namespace OpenTween this.LabelScreenName.Text = user.ScreenName; this.LabelName.Text = user.Name; this.LabelLocation.Text = user.Location ?? ""; - this.LabelCreatedAt.Text = MyCommon.DateTimeParse(user.CreatedAt).ToString(); + this.LabelCreatedAt.Text = MyCommon.DateTimeParse(user.CreatedAt).ToLocalTimeString(); if (user.Protected) this.LabelIsProtected.Text = Properties.Resources.Yes; @@ -249,7 +249,7 @@ namespace OpenTween var html = TweetFormatter.AutoLinkHtml(status.FullText, entities); html = this.mainForm.createDetailHtml(html + - " Posted at " + MyCommon.DateTimeParse(status.CreatedAt) + + " Posted at " + MyCommon.DateTimeParse(status.CreatedAt).ToLocalTimeString() + " via " + status.Source); if (cancellationToken.IsCancellationRequested)