SimpleDateFormatter.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.IO;
  21. namespace log4net.DateFormatter
  22. {
  23. /// <summary>
  24. /// Formats the <see cref="DateTime"/> using the <see cref="M:DateTime.ToString(string, IFormatProvider)"/> method.
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// Formats the <see cref="DateTime"/> using the <see cref="DateTime"/> <see cref="M:DateTime.ToString(string, IFormatProvider)"/> method.
  29. /// </para>
  30. /// </remarks>
  31. /// <author>Nicko Cadell</author>
  32. /// <author>Gert Driesen</author>
  33. public class SimpleDateFormatter : IDateFormatter
  34. {
  35. #region Public Instance Constructors
  36. /// <summary>
  37. /// Constructor
  38. /// </summary>
  39. /// <param name="format">The format string.</param>
  40. /// <remarks>
  41. /// <para>
  42. /// Initializes a new instance of the <see cref="SimpleDateFormatter" /> class
  43. /// with the specified format string.
  44. /// </para>
  45. /// <para>
  46. /// The format string must be compatible with the options
  47. /// that can be supplied to <see cref="M:DateTime.ToString(string, IFormatProvider)"/>.
  48. /// </para>
  49. /// </remarks>
  50. public SimpleDateFormatter(string format)
  51. {
  52. m_formatString = format;
  53. }
  54. #endregion Public Instance Constructors
  55. #region Implementation of IDateFormatter
  56. /// <summary>
  57. /// Formats the date using <see cref="M:DateTime.ToString(string, IFormatProvider)"/>.
  58. /// </summary>
  59. /// <param name="dateToFormat">The date to convert to a string.</param>
  60. /// <param name="writer">The writer to write to.</param>
  61. /// <remarks>
  62. /// <para>
  63. /// Uses the date format string supplied to the constructor to call
  64. /// the <see cref="M:DateTime.ToString(string, IFormatProvider)"/> method to format the date.
  65. /// </para>
  66. /// </remarks>
  67. virtual public void FormatDate(DateTime dateToFormat, TextWriter writer)
  68. {
  69. writer.Write(dateToFormat.ToString(m_formatString, System.Globalization.DateTimeFormatInfo.InvariantInfo));
  70. }
  71. #endregion
  72. #region Private Instance Fields
  73. /// <summary>
  74. /// The format string used to format the <see cref="DateTime" />.
  75. /// </summary>
  76. /// <remarks>
  77. /// <para>
  78. /// The format string must be compatible with the options
  79. /// that can be supplied to <see cref="M:DateTime.ToString(string, IFormatProvider)"/>.
  80. /// </para>
  81. /// </remarks>
  82. private readonly string m_formatString;
  83. #endregion Private Instance Fields
  84. }
  85. }