udftime.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: LGPL-2.0+
  2. /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Paul Eggert (eggert@twinsun.com). */
  5. /*
  6. * dgb 10/02/98: ripped this from glibc source to help convert timestamps
  7. * to unix time
  8. * 10/04/98: added new table-based lookup after seeing how ugly
  9. * the gnu code is
  10. * blf 09/27/99: ripped out all the old code and inserted new table from
  11. * John Brockmeyer (without leap second corrections)
  12. * rewrote udf_stamp_to_time and fixed timezone accounting in
  13. * udf_time_to_stamp.
  14. */
  15. /*
  16. * We don't take into account leap seconds. This may be correct or incorrect.
  17. * For more NIST information (especially dealing with leap seconds), see:
  18. * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
  19. */
  20. #include "udfdecl.h"
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/time.h>
  24. void
  25. udf_disk_stamp_to_time(struct timespec64 *dest, struct timestamp src)
  26. {
  27. u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone);
  28. u16 year = le16_to_cpu(src.year);
  29. uint8_t type = typeAndTimezone >> 12;
  30. int16_t offset;
  31. if (type == 1) {
  32. offset = typeAndTimezone << 4;
  33. /* sign extent offset */
  34. offset = (offset >> 4);
  35. if (offset == -2047) /* unspecified offset */
  36. offset = 0;
  37. } else
  38. offset = 0;
  39. dest->tv_sec = mktime64(year, src.month, src.day, src.hour, src.minute,
  40. src.second);
  41. dest->tv_sec -= offset * 60;
  42. /*
  43. * Sanitize nanosecond field since reportedly some filesystems are
  44. * recorded with bogus sub-second values.
  45. */
  46. if (src.centiseconds < 100 && src.hundredsOfMicroseconds < 100 &&
  47. src.microseconds < 100) {
  48. dest->tv_nsec = 1000 * (src.centiseconds * 10000 +
  49. src.hundredsOfMicroseconds * 100 + src.microseconds);
  50. } else {
  51. dest->tv_nsec = 0;
  52. }
  53. }
  54. void
  55. udf_time_to_disk_stamp(struct timestamp *dest, struct timespec64 ts)
  56. {
  57. time64_t seconds;
  58. int16_t offset;
  59. struct tm tm;
  60. offset = -sys_tz.tz_minuteswest;
  61. dest->typeAndTimezone = cpu_to_le16(0x1000 | (offset & 0x0FFF));
  62. seconds = ts.tv_sec + offset * 60;
  63. time64_to_tm(seconds, 0, &tm);
  64. dest->year = cpu_to_le16(tm.tm_year + 1900);
  65. dest->month = tm.tm_mon + 1;
  66. dest->day = tm.tm_mday;
  67. dest->hour = tm.tm_hour;
  68. dest->minute = tm.tm_min;
  69. dest->second = tm.tm_sec;
  70. dest->centiseconds = ts.tv_nsec / 10000000;
  71. dest->hundredsOfMicroseconds = (ts.tv_nsec / 1000 -
  72. dest->centiseconds * 10000) / 100;
  73. dest->microseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000 -
  74. dest->hundredsOfMicroseconds * 100);
  75. }
  76. /* EOF */