123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #region License, Terms and Conditions
- #endregion
- namespace Jayrock
- {
- #region Imports
- using System;
- #endregion
- public sealed class UnixTime
- {
- private static readonly int[] _days = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 };
- private static readonly int[] _leapDays = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
-
-
-
-
-
-
-
-
-
- public static DateTime ToDateTime(long time)
- {
- const long secondsPerDay = 24 * 60 * 60;
- const long secondsPerYear = (365 * secondsPerDay);
-
-
-
-
- int tmp = (int) (time / secondsPerYear) + 70;
- time -= ((tmp - 70) * secondsPerYear);
-
-
-
-
- time -= (ElapsedLeapYears(tmp) * secondsPerDay);
-
-
-
-
-
- bool isLeapYear = false;
- if (time < 0)
- {
- time += secondsPerYear;
- tmp--;
-
- if (IsLeapYear(tmp))
- {
- time += secondsPerDay;
- isLeapYear = true;
- }
- }
- else if (IsLeapYear(tmp))
- {
- isLeapYear = true;
- }
-
-
-
-
-
- int year = tmp;
-
-
-
-
-
- int yearDay = (int) (time / secondsPerDay);
- time -= yearDay * secondsPerDay;
-
-
-
-
- int[] yearDaysByMonth = isLeapYear ? _leapDays : _days;
- int month = 1;
- while (yearDaysByMonth[month] < yearDay) month++;
- int mday = yearDay - yearDaysByMonth[month - 1];
-
-
-
-
-
- int hour = (int) (time / 3600);
- time -= hour * 3600L;
- int min = (int) (time / 60);
- int sec = (int) (time - (min) * 60);
-
-
-
-
-
-
-
-
- return (new DateTime(year + 1900, month, mday, hour, min, sec)).ToLocalTime();
- }
- public static long ToInt64(DateTime time)
- {
- return (long) (time.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
- }
-
-
-
-
- private static bool IsLeapYear(int y)
- {
- return (((y % 4 == 0) && (y % 100 != 0)) || ((y + 1900) % 400 == 0));
- }
-
-
-
-
-
- private static long ElapsedLeapYears(int y)
- {
- return (((y - 1) / 4) - ((y - 1) / 100) + ((y + 299) / 400) - 17);
- }
- private UnixTime()
- {
- throw new NotSupportedException();
- }
- }
- }
|