You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Snowflake.cs 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. namespace Discord.Net
  3. {
  4. /// <summary>
  5. /// Represents a discord snowflake.
  6. /// </summary>
  7. public struct Snowflake
  8. {
  9. private const ulong DiscordEpoch = 1420070400000UL;
  10. /// <summary>
  11. /// Gets the raw value of this snowflake.
  12. /// </summary>
  13. /// <returns>A <see cref="ulong"/> with the rae value.</returns>
  14. public ulong RawValue { get; }
  15. /// <summary>
  16. /// Creates a <see cref="Snowflake"/> based on the <paramref name="value"/> provided.
  17. /// </summary>
  18. /// <param name="value">Raw value of the snowflake.</param>
  19. public Snowflake(ulong value)
  20. {
  21. RawValue = value;
  22. }
  23. /// <summary>
  24. /// Creates a <see cref="Snowflake"/> based on the <paramref name="dateTimeOffset"/> provided.
  25. /// </summary>
  26. /// <param name="dateTimeOffset">DateTimeOffset of this snowflake.</param>
  27. public Snowflake(DateTimeOffset dateTimeOffset)
  28. {
  29. RawValue = ((ulong)dateTimeOffset.ToUniversalTime().ToUnixTimeMilliseconds() - DiscordEpoch) << 22;
  30. }
  31. /// <summary>
  32. /// Creates a <see cref="Snowflake"/> based on the <paramref name="dateTime"/> provided.
  33. /// </summary>
  34. /// <param name="dateTime">DateTime of this snowflake.</param>
  35. public Snowflake(DateTime dateTime)
  36. : this(new DateTimeOffset(dateTime)) { }
  37. /// <summary>
  38. /// Converts this <see cref="Snowflake"/> to a <see cref="DateTimeOffset"/>.
  39. /// </summary>
  40. /// <returns>A <see cref="DateTimeOffset"/> of this snowflake.</returns>
  41. public DateTimeOffset ToDateTimeOffset() => DateTimeOffset.FromUnixTimeMilliseconds((long)((RawValue >> 22) + DiscordEpoch));
  42. /// <summary>
  43. /// Converts this <see cref="Snowflake"/> to a <see cref="DateTime"/>.
  44. /// </summary>
  45. /// <returns>A <see cref="DateTime"/> of this snowflake.</returns>
  46. public DateTimeOffset ToDateTime() => ToDateTimeOffset().UtcDateTime;
  47. /// <summary>
  48. /// Converts this <see cref="Snowflake"/> to a <see cref="ulong"/>.
  49. /// </summary>
  50. /// <param name="snowflake">Value that will be converted</param>
  51. /// <returns>A <see cref="ulong"/> with the raw value.</returns>
  52. public static implicit operator ulong(Snowflake snowflake) => snowflake.RawValue;
  53. /// <summary>
  54. /// Converts this <see cref="ulong"/> to a <see cref="Snowflake"/>.
  55. /// </summary>
  56. /// <param name="value">Value that will be converted</param>
  57. /// <returns>A <see cref="Snowflake"/> with <paramref name="value"/> as the raw value.</returns>
  58. public static implicit operator Snowflake(ulong value) => new Snowflake(value);
  59. /// <summary>
  60. /// Returns the raw value as <see cref="string"/>.
  61. /// </summary>
  62. /// <returns>A <see cref="string"/> that is the raw value.</returns>
  63. public override string ToString()
  64. => RawValue.ToString();
  65. }
  66. }

No Description