DateTimeFormat.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace System.Runtime.Serialization
  5. {
  6. using System.Globalization;
  7. /// <summary>
  8. /// This class is used to customize the way DateTime is
  9. /// serialized or deserialized by <see cref="Json.DataContractJsonSerializer"/>
  10. /// </summary>
  11. public class DateTimeFormat
  12. {
  13. private string formatString;
  14. private IFormatProvider formatProvider;
  15. private DateTimeStyles dateTimeStyles;
  16. /// <summary>
  17. /// Initailizes a new <see cref="DateTimeFormat"/> with the specified
  18. /// formatString and DateTimeFormatInfo.CurrentInfo as the
  19. /// formatProvider.
  20. /// </summary>
  21. /// <param name="formatString">Specifies the formatString to be used.</param>
  22. public DateTimeFormat(string formatString) : this(formatString, DateTimeFormatInfo.CurrentInfo)
  23. {
  24. }
  25. /// <summary>
  26. /// Initailizes a new <see cref="DateTimeFormat"/> with the specified
  27. /// formatString and formatProvider.
  28. /// </summary>
  29. /// <param name="formatString">Specifies the formatString to be used.</param>
  30. /// <param name="formatProvider">Specifies the formatProvider to be used.</param>
  31. public DateTimeFormat(string formatString, IFormatProvider formatProvider)
  32. {
  33. if (formatString == null)
  34. {
  35. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("formatString");
  36. }
  37. if (formatProvider == null)
  38. {
  39. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("formatProvider");
  40. }
  41. this.formatString = formatString;
  42. this.formatProvider = formatProvider;
  43. this.dateTimeStyles = DateTimeStyles.RoundtripKind;
  44. }
  45. /// <summary>
  46. /// Gets the FormatString set on this instance.
  47. /// </summary>
  48. public string FormatString
  49. {
  50. get
  51. {
  52. return this.formatString;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets the FormatProvider set on this instance.
  57. /// </summary>
  58. public IFormatProvider FormatProvider
  59. {
  60. get
  61. {
  62. return this.formatProvider;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets or sets the <see cref="DateTimeStyles"/> on this instance.
  67. /// </summary>
  68. public DateTimeStyles DateTimeStyles
  69. {
  70. get
  71. {
  72. return this.dateTimeStyles;
  73. }
  74. set
  75. {
  76. this.dateTimeStyles = value;
  77. }
  78. }
  79. }
  80. }