Iso8601DateFormatter.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. namespace log4net.DateFormatter
  22. {
  23. /// <summary>
  24. /// Formats the <see cref="DateTime"/> as <c>"yyyy-MM-dd HH:mm:ss,fff"</c>.
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// Formats the <see cref="DateTime"/> specified as a string: <c>"yyyy-MM-dd HH:mm:ss,fff"</c>.
  29. /// </para>
  30. /// </remarks>
  31. /// <author>Nicko Cadell</author>
  32. /// <author>Gert Driesen</author>
  33. public class Iso8601DateFormatter : AbsoluteTimeDateFormatter
  34. {
  35. #region Public Instance Constructors
  36. /// <summary>
  37. /// Default constructor
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// Initializes a new instance of the <see cref="Iso8601DateFormatter" /> class.
  42. /// </para>
  43. /// </remarks>
  44. public Iso8601DateFormatter()
  45. {
  46. }
  47. #endregion Public Instance Constructors
  48. #region Override implementation of AbsoluteTimeDateFormatter
  49. /// <summary>
  50. /// Formats the date without the milliseconds part
  51. /// </summary>
  52. /// <param name="dateToFormat">The date to format.</param>
  53. /// <param name="buffer">The string builder to write to.</param>
  54. /// <remarks>
  55. /// <para>
  56. /// Formats the date specified as a string: <c>"yyyy-MM-dd HH:mm:ss"</c>.
  57. /// </para>
  58. /// <para>
  59. /// The base class will append the <c>",fff"</c> milliseconds section.
  60. /// This method will only be called at most once per second.
  61. /// </para>
  62. /// </remarks>
  63. override protected void FormatDateWithoutMillis(DateTime dateToFormat, StringBuilder buffer)
  64. {
  65. buffer.Append(dateToFormat.Year);
  66. buffer.Append('-');
  67. int month = dateToFormat.Month;
  68. if (month < 10)
  69. {
  70. buffer.Append('0');
  71. }
  72. buffer.Append(month);
  73. buffer.Append('-');
  74. int day = dateToFormat.Day;
  75. if (day < 10)
  76. {
  77. buffer.Append('0');
  78. }
  79. buffer.Append(day);
  80. buffer.Append(' ');
  81. // Append the 'HH:mm:ss'
  82. base.FormatDateWithoutMillis(dateToFormat, buffer);
  83. }
  84. #endregion Override implementation of AbsoluteTimeDateFormatter
  85. }
  86. }