NSDate+ISO8601.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // NSDate+ISO8601.m
  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+ISO8601.h"
  26. #include <time.h>
  27. @implementation NSDate (ISO8601)
  28. + (NSDate *)dateFromISO8601String:(NSString *)value {
  29. if (!value.length)
  30. return nil;
  31. struct tm tm;
  32. strptime(value.UTF8String, "%Y-%m-%dT%H:%M:%S%z", &tm);
  33. time_t t = mktime(&tm);
  34. return [NSDate dateWithTimeIntervalSince1970:t + [[NSTimeZone localTimeZone] secondsFromGMT]];
  35. }
  36. - (NSString *)ISO8601String {
  37. struct tm *timeinfo;
  38. char buffer[80];
  39. time_t rawtime = (time_t)[self timeIntervalSince1970];
  40. timeinfo = gmtime(&rawtime);
  41. strftime(buffer, 80, "%Y-%m-%dT%H:%M:%SZ", timeinfo);
  42. return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
  43. }
  44. @end