InternalGlobalizationHelper.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Globalization
  5. {
  6. internal class InternalGlobalizationHelper
  7. {
  8. // Copied from the TimeSpan to be used inside the globalization code and avoid internal dependency on TimeSpan class
  9. internal static long TimeToTicks(int hour, int minute, int second)
  10. {
  11. // totalSeconds is bounded by 2^31 * 2^12 + 2^31 * 2^8 + 2^31,
  12. // which is less than 2^44, meaning we won't overflow totalSeconds.
  13. long totalSeconds = (long)hour * 3600 + (long)minute * 60 + (long)second;
  14. if (totalSeconds > MaxSeconds || totalSeconds < MinSeconds)
  15. throw new ArgumentOutOfRangeException(null, SR.Overflow_TimeSpanTooLong);
  16. return totalSeconds * TicksPerSecond;
  17. }
  18. //
  19. // Define needed constants so globalization code can be independant from any other types
  20. //
  21. internal const long TicksPerMillisecond = 10000;
  22. internal const long TicksPerTenthSecond = TicksPerMillisecond * 100;
  23. internal const long TicksPerSecond = TicksPerMillisecond * 1000; // 10,000,000
  24. internal const long MaxSeconds = long.MaxValue / TicksPerSecond;
  25. internal const long MinSeconds = long.MinValue / TicksPerSecond;
  26. private const int DaysPerYear = 365;
  27. private const int DaysPer4Years = DaysPerYear * 4 + 1; // 1461
  28. private const int DaysPer100Years = DaysPer4Years * 25 - 1; // 36524
  29. private const int DaysPer400Years = DaysPer100Years * 4 + 1; // 146097
  30. private const int DaysTo10000 = DaysPer400Years * 25 - 366; // 3652059
  31. private const long TicksPerMinute = TicksPerSecond * 60;
  32. private const long TicksPerHour = TicksPerMinute * 60;
  33. private const long TicksPerDay = TicksPerHour * 24;
  34. internal const long MaxTicks = DaysTo10000 * TicksPerDay - 1;
  35. internal const long MinTicks = 0;
  36. internal const long MaxMilliSeconds = long.MaxValue / TicksPerMillisecond;
  37. internal const long MinMilliSeconds = long.MinValue / TicksPerMillisecond;
  38. internal const int StringBuilderDefaultCapacity = 16;
  39. internal const long MaxOffset = TimeSpan.TicksPerHour * 14;
  40. internal const long MinOffset = -MaxOffset;
  41. }
  42. }