You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitDateFormatter.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2011, 2012 Robin Rosenberg and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import java.text.DateFormat;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Locale;
  14. import java.util.TimeZone;
  15. import org.eclipse.jgit.lib.PersonIdent;
  16. /**
  17. * A utility for formatting dates according to the Git log.date formats plus
  18. * extensions.
  19. * <p>
  20. * The enum {@link org.eclipse.jgit.util.GitDateFormatter.Format} defines the
  21. * available types.
  22. */
  23. public class GitDateFormatter {
  24. private DateFormat dateTimeInstance;
  25. private DateFormat dateTimeInstance2;
  26. private final Format format;
  27. /**
  28. * Git and JGit formats
  29. */
  30. public enum Format {
  31. /**
  32. * Git format: Time and original time zone
  33. */
  34. DEFAULT,
  35. /**
  36. * Git format: Relative time stamp
  37. */
  38. RELATIVE,
  39. /**
  40. * Git format: Date and time in local time zone
  41. */
  42. LOCAL,
  43. /**
  44. * Git format: ISO 8601 plus time zone
  45. */
  46. ISO,
  47. /**
  48. * Git formt: RFC 2822 plus time zone
  49. */
  50. RFC,
  51. /**
  52. * Git format: YYYY-MM-DD
  53. */
  54. SHORT,
  55. /**
  56. * Git format: Seconds size 1970 in UTC plus time zone
  57. */
  58. RAW,
  59. /**
  60. * Locale dependent formatting with original time zone
  61. */
  62. LOCALE,
  63. /**
  64. * Locale dependent formatting in local time zone
  65. */
  66. LOCALELOCAL
  67. }
  68. /**
  69. * Create a new Git oriented date formatter
  70. *
  71. * @param format
  72. * a {@link org.eclipse.jgit.util.GitDateFormatter.Format}
  73. * object.
  74. */
  75. public GitDateFormatter(Format format) {
  76. this.format = format;
  77. switch (format) {
  78. default:
  79. break;
  80. case DEFAULT: // Not default:
  81. dateTimeInstance = new SimpleDateFormat(
  82. "EEE MMM dd HH:mm:ss yyyy Z", Locale.US); //$NON-NLS-1$
  83. break;
  84. case ISO:
  85. dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", //$NON-NLS-1$
  86. Locale.US);
  87. break;
  88. case LOCAL:
  89. dateTimeInstance = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", //$NON-NLS-1$
  90. Locale.US);
  91. break;
  92. case RFC:
  93. dateTimeInstance = new SimpleDateFormat(
  94. "EEE, dd MMM yyyy HH:mm:ss Z", Locale.US); //$NON-NLS-1$
  95. break;
  96. case SHORT:
  97. dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd", Locale.US); //$NON-NLS-1$
  98. break;
  99. case LOCALE:
  100. case LOCALELOCAL:
  101. SystemReader systemReader = SystemReader.getInstance();
  102. dateTimeInstance = systemReader.getDateTimeInstance(
  103. DateFormat.DEFAULT, DateFormat.DEFAULT);
  104. dateTimeInstance2 = systemReader.getSimpleDateFormat("Z"); //$NON-NLS-1$
  105. break;
  106. }
  107. }
  108. /**
  109. * Format committer, author or tagger ident according to this formatter's
  110. * specification.
  111. *
  112. * @param ident
  113. * a {@link org.eclipse.jgit.lib.PersonIdent} object.
  114. * @return formatted version of date, time and time zone
  115. */
  116. @SuppressWarnings("boxing")
  117. public String formatDate(PersonIdent ident) {
  118. switch (format) {
  119. case RAW:
  120. int offset = ident.getTimeZoneOffset();
  121. String sign = offset < 0 ? "-" : "+"; //$NON-NLS-1$ //$NON-NLS-2$
  122. int offset2;
  123. if (offset < 0)
  124. offset2 = -offset;
  125. else
  126. offset2 = offset;
  127. int hours = offset2 / 60;
  128. int minutes = offset2 % 60;
  129. return String.format("%d %s%02d%02d", //$NON-NLS-1$
  130. ident.getWhen().getTime() / 1000, sign, hours, minutes);
  131. case RELATIVE:
  132. return RelativeDateFormatter.format(ident.getWhen());
  133. case LOCALELOCAL:
  134. case LOCAL:
  135. dateTimeInstance.setTimeZone(SystemReader.getInstance()
  136. .getTimeZone());
  137. return dateTimeInstance.format(ident.getWhen());
  138. case LOCALE:
  139. TimeZone tz = ident.getTimeZone();
  140. if (tz == null)
  141. tz = SystemReader.getInstance().getTimeZone();
  142. dateTimeInstance.setTimeZone(tz);
  143. dateTimeInstance2.setTimeZone(tz);
  144. return dateTimeInstance.format(ident.getWhen()) + " " //$NON-NLS-1$
  145. + dateTimeInstance2.format(ident.getWhen());
  146. default:
  147. tz = ident.getTimeZone();
  148. if (tz == null)
  149. tz = SystemReader.getInstance().getTimeZone();
  150. dateTimeInstance.setTimeZone(ident.getTimeZone());
  151. return dateTimeInstance.format(ident.getWhen());
  152. }
  153. }
  154. }