util.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/isofs/util.c
  4. */
  5. #include <linux/time.h>
  6. #include "isofs.h"
  7. /*
  8. * We have to convert from a MM/DD/YY format to the Unix ctime format.
  9. * We have to take into account leap years and all of that good stuff.
  10. * Unfortunately, the kernel does not have the information on hand to
  11. * take into account daylight savings time, but it shouldn't matter.
  12. * The time stored should be localtime (with or without DST in effect),
  13. * and the timezone offset should hold the offset required to get back
  14. * to GMT. Thus we should always be correct.
  15. */
  16. struct timespec64 iso_date(u8 *p, int flags)
  17. {
  18. int year, month, day, hour, minute, second, tz;
  19. struct timespec64 ts;
  20. if (flags & ISO_DATE_LONG_FORM) {
  21. year = (p[0] - '0') * 1000 +
  22. (p[1] - '0') * 100 +
  23. (p[2] - '0') * 10 +
  24. (p[3] - '0') - 1900;
  25. month = ((p[4] - '0') * 10 + (p[5] - '0'));
  26. day = ((p[6] - '0') * 10 + (p[7] - '0'));
  27. hour = ((p[8] - '0') * 10 + (p[9] - '0'));
  28. minute = ((p[10] - '0') * 10 + (p[11] - '0'));
  29. second = ((p[12] - '0') * 10 + (p[13] - '0'));
  30. ts.tv_nsec = ((p[14] - '0') * 10 + (p[15] - '0')) * 10000000;
  31. tz = p[16];
  32. } else {
  33. year = p[0];
  34. month = p[1];
  35. day = p[2];
  36. hour = p[3];
  37. minute = p[4];
  38. second = p[5];
  39. ts.tv_nsec = 0;
  40. /* High sierra has no time zone */
  41. tz = flags & ISO_DATE_HIGH_SIERRA ? 0 : p[6];
  42. }
  43. if (year < 0) {
  44. ts.tv_sec = 0;
  45. } else {
  46. ts.tv_sec = mktime64(year+1900, month, day, hour, minute, second);
  47. /* sign extend */
  48. if (tz & 0x80)
  49. tz |= (-1 << 8);
  50. /*
  51. * The timezone offset is unreliable on some disks,
  52. * so we make a sanity check. In no case is it ever
  53. * more than 13 hours from GMT, which is 52*15min.
  54. * The time is always stored in localtime with the
  55. * timezone offset being what get added to GMT to
  56. * get to localtime. Thus we need to subtract the offset
  57. * to get to true GMT, which is what we store the time
  58. * as internally. On the local system, the user may set
  59. * their timezone any way they wish, of course, so GMT
  60. * gets converted back to localtime on the receiving
  61. * system.
  62. *
  63. * NOTE: mkisofs in versions prior to mkisofs-1.10 had
  64. * the sign wrong on the timezone offset. This has now
  65. * been corrected there too, but if you are getting screwy
  66. * results this may be the explanation. If enough people
  67. * complain, a user configuration option could be added
  68. * to add the timezone offset in with the wrong sign
  69. * for 'compatibility' with older discs, but I cannot see how
  70. * it will matter that much.
  71. *
  72. * Thanks to kuhlmav@elec.canterbury.ac.nz (Volker Kuhlmann)
  73. * for pointing out the sign error.
  74. */
  75. if (-52 <= tz && tz <= 52)
  76. ts.tv_sec -= tz * 15 * 60;
  77. }
  78. return ts;
  79. }