NSDate+RFC1123.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // NSDate+RFC1123.h
  3. // OCWebDAVClient
  4. //
  5. // This class is based in https://github.com/zwaldowski/DZWebDAVClient. Copyright (c) 2012 Zachary Waldowski, Troy Brant, Marcus Rohrmoser, and Sam Soffes.
  6. //
  7. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "NSDate+RFC1123.h"
  26. static NSDateFormatter *RFC1123Formatter(void) {
  27. static NSDateFormatter *formatter = nil;
  28. static dispatch_once_t onceToken;
  29. dispatch_once(&onceToken, ^{
  30. formatter = [NSDateFormatter new];
  31. formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  32. formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
  33. formatter.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss z";
  34. });
  35. return formatter;
  36. }
  37. static NSDateFormatter *RFC850Formatter(void) {
  38. static NSDateFormatter *formatter = nil;
  39. static dispatch_once_t onceToken;
  40. dispatch_once(&onceToken, ^{
  41. formatter = [NSDateFormatter new];
  42. formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  43. formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
  44. formatter.dateFormat = @"EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z";
  45. });
  46. return formatter;
  47. }
  48. static NSDateFormatter *ASCTimeFormatter(void) {
  49. static NSDateFormatter *formatter = nil;
  50. static dispatch_once_t onceToken;
  51. dispatch_once(&onceToken, ^{
  52. formatter = [NSDateFormatter new];
  53. formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  54. formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
  55. formatter.dateFormat = @"EEE MMM d HH':'mm':'ss yyyy";
  56. });
  57. return formatter;
  58. }
  59. @implementation NSDate (RFC1123)
  60. + (NSDate *)dateFromRFC1123String:(NSString *)value {
  61. if (!value.length)
  62. return nil;
  63. return [RFC1123Formatter() dateFromString: value] ?: [RFC850Formatter() dateFromString: value] ?: [ASCTimeFormatter() dateFromString: value];
  64. }
  65. - (NSString *)RFC1123String {
  66. return [RFC1123Formatter() stringFromDate: self];
  67. }
  68. @end