DateTimeConverter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace PhpZip\Util;
  3. /**
  4. * Convert unix timestamp values to DOS date/time values and vice versa.
  5. *
  6. * The DOS date/time format is a bitmask:
  7. *
  8. * 24 16 8 0
  9. * +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
  10. * |Y|Y|Y|Y|Y|Y|Y|M| |M|M|M|D|D|D|D|D| |h|h|h|h|h|m|m|m| |m|m|m|s|s|s|s|s|
  11. * +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
  12. * \___________/\________/\_________/ \________/\____________/\_________/
  13. * year month day hour minute second
  14. *
  15. * The year is stored as an offset from 1980.
  16. * Seconds are stored in two-second increments.
  17. * (So if the "second" value is 15, it actually represents 30 seconds.)
  18. *
  19. * @see https://docs.microsoft.com/ru-ru/windows/win32/api/winbase/nf-winbase-filetimetodosdatetime?redirectedfrom=MSDN
  20. *
  21. * @author Ne-Lexa alexey@nelexa.ru
  22. * @license MIT
  23. *
  24. * @internal
  25. */
  26. class DateTimeConverter
  27. {
  28. /**
  29. * Smallest supported DOS date/time value in a ZIP file,
  30. * which is January 1st, 1980 AD 00:00:00 local time.
  31. */
  32. const MIN_DOS_TIME = 0x210000; // (1 << 21) | (1 << 16)
  33. /**
  34. * Largest supported DOS date/time value in a ZIP file,
  35. * which is December 31st, 2107 AD 23:59:58 local time.
  36. */
  37. const MAX_DOS_TIME = 0xff9fbf7d; // ((2107 - 1980) << 25) | (12 << 21) | (31 << 16) | (23 << 11) | (59 << 5) | (58 >> 1);
  38. /**
  39. * Convert a 32 bit integer DOS date/time value to a UNIX timestamp value.
  40. *
  41. * @param int $dosTime Dos date/time
  42. *
  43. * @return int Unix timestamp
  44. */
  45. public static function msDosToUnix($dosTime)
  46. {
  47. if ($dosTime <= self::MIN_DOS_TIME) {
  48. $dosTime = 0;
  49. } elseif ($dosTime > self::MAX_DOS_TIME) {
  50. $dosTime = self::MAX_DOS_TIME;
  51. }
  52. // date_default_timezone_set('UTC');
  53. return mktime(
  54. (($dosTime >> 11) & 0x1f), // hours
  55. (($dosTime >> 5) & 0x3f), // minutes
  56. (($dosTime << 1) & 0x3e), // seconds
  57. (($dosTime >> 21) & 0x0f), // month
  58. (($dosTime >> 16) & 0x1f), // day
  59. ((($dosTime >> 25) & 0x7f) + 1980) // year
  60. );
  61. }
  62. /**
  63. * Converts a UNIX timestamp value to a DOS date/time value.
  64. *
  65. * @param int $unixTimestamp the number of seconds since midnight, January 1st,
  66. * 1970 AD UTC
  67. *
  68. * @return int a DOS date/time value reflecting the local time zone and
  69. * rounded down to even seconds
  70. * and is in between DateTimeConverter::MIN_DOS_TIME and DateTimeConverter::MAX_DOS_TIME
  71. */
  72. public static function unixToMsDos($unixTimestamp)
  73. {
  74. if ($unixTimestamp < 0) {
  75. throw new \InvalidArgumentException('Negative unix timestamp: ' . $unixTimestamp);
  76. }
  77. $date = getdate($unixTimestamp);
  78. $dosTime = (
  79. (($date['year'] - 1980) << 25) |
  80. ($date['mon'] << 21) |
  81. ($date['mday'] << 16) |
  82. ($date['hours'] << 11) |
  83. ($date['minutes'] << 5) |
  84. ($date['seconds'] >> 1)
  85. );
  86. if ($dosTime <= self::MIN_DOS_TIME) {
  87. $dosTime = 0;
  88. }
  89. return $dosTime;
  90. }
  91. }