Skip to content

Commit

Permalink
Fix deserailisation of dates in format of 'milliseconds since epoch',…
Browse files Browse the repository at this point in the history
… being used in direct messaging events.
  • Loading branch information
Troy Willmot committed Oct 28, 2018
1 parent 8d34089 commit c9be341
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 11 deletions.
6 changes: 3 additions & 3 deletions TweetMoaSharp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>TweetMoaSharp</id>
<version>5.1.0</version>
<version>5.2.0</version>
<dependencies>
<group>
<dependency id="Newtonsoft.Json" version="11.0.2" />
Expand All @@ -18,9 +18,9 @@
<iconUrl>https://raw.githubusercontent.com/Yortw/tweetmoasharp/master/logo_tweetmoasharp.png</iconUrl>
<tags>twitter tweetmoasharp tweetsharp web api http rest wrapper json winrt uwp netcore netstandard10 netstandard20</tags>
<releaseNotes>
New in 5.1.0
New in 5.2.0
----------
- Async Direct Message methods now use the new API (and actually work)
- CreatedAt now set properly on direct message events.
</releaseNotes>
</metadata>
<files>
Expand Down
1 change: 1 addition & 0 deletions src/TweetSharp.Tests/TwitterServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,7 @@ public async Task Can_Send_DirectMessageWithMedia()
Assert.IsNotNull(result);
Assert.IsNotNull(result.Event);
Assert.NotZero(result.Event.Id);
Assert.AreNotEqual(DateTime.MinValue, result.Event.CreatedAt);

var rate = service.Response.RateLimitStatus;
Assert.IsNotNull(rate);
Expand Down
4 changes: 2 additions & 2 deletions src/TweetSharp/Properties/AssemblyInfoVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
using System.Reflection;
[assembly: AssemblyVersion("5.1.0.0")]
[assembly: AssemblyFileVersion("5.1.0.0")]
[assembly: AssemblyVersion("5.2.0.0")]
[assembly: AssemblyFileVersion("5.2.0.0")]
6 changes: 5 additions & 1 deletion src/TweetSharp/TwitterDateFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public enum TwitterDateFormat
/// TrendsWeekly
/// </summary>
[Description("yyyy-MM-dd")]
TrendsWeekly
TrendsWeekly,
/// <summary>
/// Used by the new direct messaging/event API.
/// </summary>
MillisecondsSinceEpoch
}
}
32 changes: 29 additions & 3 deletions src/TweetSharp/TwitterDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class TwitterDateTime : ITwitterModel
private static readonly IDictionary<string, string> _map =
new Dictionary<string, string>();

private static readonly DateTime Epoch = new DateTime(1970, 01, 01, 0, 0, 0, DateTimeKind.Utc);

/// <summary>
/// Gets or sets the Twitter-based date format.
/// </summary>
Expand Down Expand Up @@ -76,6 +78,9 @@ public static string ConvertFromDateTime(DateTime input, TwitterDateFormat forma
{
EnsureDateFormatsAreMapped();

if (format == TwitterDateFormat.MillisecondsSinceEpoch)
return input.Subtract(Epoch).TotalMilliseconds.ToString();

#if !SILVERLIGHT && !Smartphone
var name = Enum.GetName(typeof(TwitterDateFormat), format);
#else
Expand Down Expand Up @@ -120,6 +125,15 @@ public static DateTime ConvertToDateTime(string input)
}
}

if (Int64.TryParse(input, out var millisecondsSinceEpoch))
{
return new DateTime
(
Epoch.Add(TimeSpan.FromMilliseconds(millisecondsSinceEpoch)).Ticks,
DateTimeKind.Utc
);
}

return default(DateTime);
}

Expand Down Expand Up @@ -152,6 +166,15 @@ public static TwitterDateTime ConvertToTwitterDateTime(string input)
}
}

if (Int64.TryParse(input, out var millisecondsSinceEpoch))
{
return new TwitterDateTime
(
new DateTime(Epoch.Add(TimeSpan.FromMilliseconds(millisecondsSinceEpoch)).Ticks, DateTimeKind.Utc),
TwitterDateFormat.MillisecondsSinceEpoch
);
}

return default(TwitterDateTime);
}
finally
Expand Down Expand Up @@ -188,9 +211,12 @@ private static void EnsureDateFormatsAreMapped()
var fi = typeof(TwitterDateFormat).GetTypeInfo().GetDeclaredField(name);
var attributes = System.Linq.Enumerable.ToArray(fi.GetCustomAttributes(typeof(DescriptionAttribute), false));
#endif
var format = (DescriptionAttribute)attributes[0];

_map.Add(name, format.Description);
if (attributes.Length > 0)
{
var format = (DescriptionAttribute)attributes[0];
if (!String.IsNullOrEmpty(format.Description))
_map.Add(name, format.Description);
}
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/TweetSharp/TwitterDirectMessageEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public virtual string Type
#if !Smartphone && !NET20
[DataMember]
#endif
[JsonProperty("created_at")]
[JsonProperty("created_timestamp")]
public virtual DateTime CreatedAt
{
get { return _createdAt; }
Expand Down
2 changes: 1 addition & 1 deletion tweetmoasharp-push-nuget.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
".nuget\NuGet.exe" push TweetMoaSharp.5.1.0.nupkg -Source https://api.nuget.org/v3/index.json
".nuget\NuGet.exe" push TweetMoaSharp.5.2.0.nupkg -Source https://api.nuget.org/v3/index.json

0 comments on commit c9be341

Please sign in to comment.