udftime.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Paul Eggert (eggert@twinsun.com).
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. /*
  17. * dgb 10/02/98: ripped this from glibc source to help convert timestamps
  18. * to unix time
  19. * 10/04/98: added new table-based lookup after seeing how ugly
  20. * the gnu code is
  21. * blf 09/27/99: ripped out all the old code and inserted new table from
  22. * John Brockmeyer (without leap second corrections)
  23. * rewrote udf_stamp_to_time and fixed timezone accounting in
  24. * udf_time_to_stamp.
  25. */
  26. /*
  27. * We don't take into account leap seconds. This may be correct or incorrect.
  28. * For more NIST information (especially dealing with leap seconds), see:
  29. * http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
  30. */
  31. #include "udfdecl.h"
  32. #include <linux/types.h>
  33. #include <linux/kernel.h>
  34. #include <linux/time.h>
  35. void
  36. udf_disk_stamp_to_time(struct timespec64 *dest, struct timestamp src)
  37. {
  38. u16 typeAndTimezone = le16_to_cpu(src.typeAndTimezone);
  39. u16 year = le16_to_cpu(src.year);
  40. uint8_t type = typeAndTimezone >> 12;
  41. int16_t offset;
  42. if (type == 1) {
  43. offset = typeAndTimezone << 4;
  44. /* sign extent offset */
  45. offset = (offset >> 4);
  46. if (offset == -2047) /* unspecified offset */
  47. offset = 0;
  48. } else
  49. offset = 0;
  50. dest->tv_sec = mktime64(year, src.month, src.day, src.hour, src.minute,
  51. src.second);
  52. dest->tv_sec -= offset * 60;
  53. dest->tv_nsec = 1000 * (src.centiseconds * 10000 +
  54. src.hundredsOfMicroseconds * 100 + src.microseconds);
  55. /*
  56. * Sanitize nanosecond field since reportedly some filesystems are
  57. * recorded with bogus sub-second values.
  58. */
  59. dest->tv_nsec %= NSEC_PER_SEC;
  60. }
  61. void
  62. udf_time_to_disk_stamp(struct timestamp *dest, struct timespec64 ts)
  63. {
  64. time64_t seconds;
  65. int16_t offset;
  66. struct tm tm;
  67. offset = -sys_tz.tz_minuteswest;
  68. dest->typeAndTimezone = cpu_to_le16(0x1000 | (offset & 0x0FFF));
  69. seconds = ts.tv_sec + offset * 60;
  70. time64_to_tm(seconds, 0, &tm);
  71. dest->year = cpu_to_le16(tm.tm_year + 1900);
  72. dest->month = tm.tm_mon + 1;
  73. dest->day = tm.tm_mday;
  74. dest->hour = tm.tm_hour;
  75. dest->minute = tm.tm_min;
  76. dest->second = tm.tm_sec;
  77. dest->centiseconds = ts.tv_nsec / 10000000;
  78. dest->hundredsOfMicroseconds = (ts.tv_nsec / 1000 -
  79. dest->centiseconds * 10000) / 100;
  80. dest->microseconds = (ts.tv_nsec / 1000 - dest->centiseconds * 10000 -
  81. dest->hundredsOfMicroseconds * 100);
  82. }
  83. /* EOF */