ITimeSystem.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. namespace Jint.Runtime;
  2. /// <summary>
  3. /// Date related operations that can replaced with implementation that can handle also full IANA data as recommended
  4. /// by the JS spec. Jint comes with <see cref="DefaultTimeSystem"/> which is based on built-in data which might be incomplete.
  5. /// </summary>
  6. /// <remarks>
  7. /// This interface intentionally uses long instead of DateTime/DateTimeOffset as DateTime/DateTimeOffset cannot handle
  8. /// neither negative years nor the date range that JS can.
  9. /// </remarks>
  10. public interface ITimeSystem
  11. {
  12. /// <summary>
  13. /// Retrieves current UTC time.
  14. /// </summary>
  15. /// <returns>Current UTC time.</returns>
  16. DateTimeOffset GetUtcNow();
  17. /// <summary>
  18. /// Return the default time zone system is using. Usually <see cref="TimeZoneInfo.Local"/>, but can be altered via
  19. /// engine configuration, see <see cref="Options.TimeZone"/>.
  20. /// </summary>
  21. TimeZoneInfo DefaultTimeZone { get; }
  22. /// <summary>
  23. /// Tries to parse given time presentation string as JS date presentation based on epoch.
  24. /// </summary>
  25. /// <param name="date">Date/time to parse.</param>
  26. /// <param name="epochMilliseconds">The milliseconds since the UNIX epoch, can be negative for values before 1970.</param>
  27. /// <returns>true, if succeeded.</returns>
  28. bool TryParse(string date, out long epochMilliseconds);
  29. /// <summary>
  30. /// Retrieves UTC offset for given date presented as milliseconds since the Unix epoch.
  31. /// Defaults to using <see cref="TimeZoneInfo.GetUtcOffset(System.DateTimeOffset)"/> using the configured time zone.
  32. /// </summary>
  33. /// <param name="epochMilliseconds">Date as milliseconds since the Unix epoch, may be negative (for instants before the epoch).</param>
  34. /// <seealso cref="TimeZone"/>
  35. public TimeSpan GetUtcOffset(long epochMilliseconds);
  36. }