2
0

SimpleDateFormat.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. get; set;
  10. }
  11. bool Lenient {
  12. get; set;
  13. }
  14. public SimpleDateFormat (): this ("g")
  15. {
  16. }
  17. public SimpleDateFormat (string format): this (format, CultureInfo.CurrentCulture)
  18. {
  19. }
  20. public SimpleDateFormat (string format, CultureInfo c)
  21. {
  22. Culture = c;
  23. this._format = format.Replace ("EEE", "ddd");
  24. this._format = this._format.Replace ("Z", "zzz");
  25. SetTimeZone (TimeZoneInfo.Local);
  26. }
  27. public bool IsLenient ()
  28. {
  29. return Lenient;
  30. }
  31. public void SetLenient (bool lenient)
  32. {
  33. Lenient = lenient;
  34. }
  35. public override DateTime Parse (string value)
  36. {
  37. if (IsLenient ())
  38. return DateTime.Parse (value);
  39. return DateTime.ParseExact (value, _format, Culture);
  40. }
  41. public override string Format (DateTime date)
  42. {
  43. date += GetTimeZone().BaseUtcOffset;
  44. return date.ToString (_format);
  45. }
  46. public string Format (long date)
  47. {
  48. return Extensions.MillisToDateTimeOffset (date, (int)GetTimeZone ().BaseUtcOffset.TotalMinutes).DateTime.ToString (_format);
  49. }
  50. }
  51. }