forked from Inumedia/SlackAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
29 lines (28 loc) · 919 Bytes
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SlackAPI
{
public static class Extensions
{
/// <summary>
/// Converts to a propert JavaScript timestamp interpretted by Slack. Also handles converting to UTC.
/// </summary>
/// <param name="that"></param>
/// <returns></returns>
public static string ToProperTimeStamp(this DateTime that, bool toUTC = true)
{
if (toUTC)
{
string result = ((that.ToUniversalTime().Ticks - 621355968000000000m) / 10000000m).ToString("G17");
if (result.Contains("."))
result = result.TrimEnd('0');
return result;
}
else
return that.Subtract(new DateTime(1970, 1, 1)).TotalSeconds.ToString();
}
}
}