DateTimeDateFormatter.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Text;
  21. using System.Globalization;
  22. namespace log4net.DateFormatter
  23. {
  24. /// <summary>
  25. /// Formats a <see cref="DateTime"/> as <c>"dd MMM yyyy HH:mm:ss,fff"</c>
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// Formats a <see cref="DateTime"/> in the format
  30. /// <c>"dd MMM yyyy HH:mm:ss,fff"</c> for example,
  31. /// <c>"06 Nov 1994 15:49:37,459"</c>.
  32. /// </para>
  33. /// </remarks>
  34. /// <author>Nicko Cadell</author>
  35. /// <author>Gert Driesen</author>
  36. /// <author>Angelika Schnagl</author>
  37. public class DateTimeDateFormatter : AbsoluteTimeDateFormatter
  38. {
  39. #region Public Instance Constructors
  40. /// <summary>
  41. /// Default constructor.
  42. /// </summary>
  43. /// <remarks>
  44. /// <para>
  45. /// Initializes a new instance of the <see cref="DateTimeDateFormatter" /> class.
  46. /// </para>
  47. /// </remarks>
  48. public DateTimeDateFormatter()
  49. {
  50. m_dateTimeFormatInfo = DateTimeFormatInfo.InvariantInfo;
  51. }
  52. #endregion Public Instance Constructors
  53. #region Override implementation of AbsoluteTimeDateFormatter
  54. /// <summary>
  55. /// Formats the date without the milliseconds part
  56. /// </summary>
  57. /// <param name="dateToFormat">The date to format.</param>
  58. /// <param name="buffer">The string builder to write to.</param>
  59. /// <remarks>
  60. /// <para>
  61. /// Formats a DateTime in the format <c>"dd MMM yyyy HH:mm:ss"</c>
  62. /// for example, <c>"06 Nov 1994 15:49:37"</c>.
  63. /// </para>
  64. /// <para>
  65. /// The base class will append the <c>",fff"</c> milliseconds section.
  66. /// This method will only be called at most once per second.
  67. /// </para>
  68. /// </remarks>
  69. override protected void FormatDateWithoutMillis(DateTime dateToFormat, StringBuilder buffer)
  70. {
  71. int day = dateToFormat.Day;
  72. if (day < 10)
  73. {
  74. buffer.Append('0');
  75. }
  76. buffer.Append(day);
  77. buffer.Append(' ');
  78. buffer.Append(m_dateTimeFormatInfo.GetAbbreviatedMonthName(dateToFormat.Month));
  79. buffer.Append(' ');
  80. buffer.Append(dateToFormat.Year);
  81. buffer.Append(' ');
  82. // Append the 'HH:mm:ss'
  83. base.FormatDateWithoutMillis(dateToFormat, buffer);
  84. }
  85. #endregion Override implementation of AbsoluteTimeDateFormatter
  86. #region Private Instance Fields
  87. /// <summary>
  88. /// The format info for the invariant culture.
  89. /// </summary>
  90. private readonly DateTimeFormatInfo m_dateTimeFormatInfo;
  91. #endregion Private Instance Fields
  92. }
  93. }