SMBDate.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2006-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.jlan.smb;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. /**
  23. * SMB date/time class.
  24. *
  25. * <p>Date/time encoding returned by older SMB/CIFS requests.
  26. *
  27. * @author gkspencer
  28. */
  29. public final class SMBDate extends Date {
  30. // Constants
  31. //
  32. // Bit masks for extracting the date/time fields from an SMB encoded date/time.
  33. //
  34. private static final int Days = 0x001F;
  35. private static final int Month = 0x01E0;
  36. private static final int Year = 0xFE00;
  37. private static final int TwoSeconds = 0x001F;
  38. private static final int Minutes = 0x07E0;
  39. private static final int Hours = 0xF800;
  40. /**
  41. * Construct the SMBDate using a seconds since 1-Jan-1970 00:00:00 value.
  42. *
  43. * @param secs Seconds since base date/time 1970 value
  44. */
  45. public SMBDate(int secs) {
  46. super (secs & 0x7FFFFFFF);
  47. }
  48. /**
  49. * Construct the SMBDate using the SMB encoded date/time values.
  50. *
  51. * @param dat SMB encoded date value
  52. * @param tim SMB encoded time value
  53. */
  54. public SMBDate(int dat, int tim) {
  55. // Extract the date from the SMB encoded value
  56. int days = dat & Days;
  57. int months = (dat & Month) >> 5;
  58. int year = (dat & Year) >> 9;
  59. // Extract the time from the SMB encoded value
  60. int secs = (tim & TwoSeconds) * 2;
  61. int mins = (tim & Minutes) >> 5;
  62. int hours = (tim & Hours) >> 11;
  63. // Use a calendar object to create the date/time value
  64. Calendar cal = Calendar.getInstance();
  65. cal.clear();
  66. cal.set(year + 1980, months - 1, days, hours, mins, secs);
  67. // Initialize this dates raw value
  68. this.setTime(cal.getTime().getTime());
  69. }
  70. /**
  71. * Create a new SMBDate using the long time value.
  72. *
  73. * @param dattim long
  74. */
  75. public SMBDate(long dattim) {
  76. super(dattim);
  77. }
  78. /**
  79. * Return this date as an SMB encoded date.
  80. *
  81. * @return SMB encoded date value.
  82. */
  83. public final int asSMBDate() {
  84. // Use a calendar object to get the day, month and year values
  85. Calendar cal = Calendar.getInstance();
  86. cal.setTime(this);
  87. // Build the SMB encoded date value
  88. int smbDate = cal.get(Calendar.DAY_OF_MONTH);
  89. smbDate += (cal.get(Calendar.MONTH) + 1) << 5;
  90. smbDate += (cal.get(Calendar.YEAR) - 1980) << 9;
  91. // Return the SMB encoded date value
  92. return smbDate;
  93. }
  94. /**
  95. * Return this time as an SMB encoded time.
  96. *
  97. * @return SMB encoded time value.
  98. */
  99. public final int asSMBTime() {
  100. // Use a calendar object to get the hour, minutes and seconds values
  101. Calendar cal = Calendar.getInstance();
  102. cal.setTime(this);
  103. // Build the SMB encoded time value
  104. int smbTime = cal.get(Calendar.SECOND) / 2;
  105. smbTime += cal.get(Calendar.MINUTE) << 5;
  106. smbTime += cal.get(Calendar.HOUR_OF_DAY) << 11;
  107. // Return the SMB encoded time value
  108. return smbTime;
  109. }
  110. }