SimpleDateFormat.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Globalization;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. public class SimpleDateFormat : DateFormat
  6. {
  7. string _format;
  8. CultureInfo Culture
  9. {
  10. get; set;
  11. }
  12. bool Lenient
  13. {
  14. get; set;
  15. }
  16. public SimpleDateFormat() : this("g")
  17. {
  18. }
  19. public SimpleDateFormat(string format) : this(format, CultureInfo.CurrentCulture)
  20. {
  21. }
  22. public SimpleDateFormat(string format, CultureInfo c)
  23. {
  24. Culture = c;
  25. this._format = format.Replace("EEE", "ddd");
  26. this._format = this._format.Replace("Z", "zzz");
  27. SetTimeZone(TimeZoneInfo.Local);
  28. }
  29. public bool IsLenient()
  30. {
  31. return Lenient;
  32. }
  33. public void SetLenient(bool lenient)
  34. {
  35. Lenient = lenient;
  36. }
  37. public override DateTime Parse(string value)
  38. {
  39. if (IsLenient())
  40. return DateTime.Parse(value);
  41. return DateTime.ParseExact(value, _format, Culture);
  42. }
  43. public override string Format(DateTime date)
  44. {
  45. date += GetTimeZone().BaseUtcOffset;
  46. return date.ToString(_format);
  47. }
  48. public string Format(long date)
  49. {
  50. return Extensions.MillisToDateTimeOffset(date, (int)GetTimeZone().BaseUtcOffset
  51. .TotalMinutes)
  52. .DateTime.ToString(_format);
  53. }
  54. }
  55. }