iOS NSDate 和NSString RFC1123格式互转

时间:2022-09-10 21:32:32

下内容来自于 MKNetworkKit iOS开源库中文件 ”NSDate+RFC1123“


NSDate+RFC1123.h文件

//
// NSDate+RFC1123.h
// MKNetworkKit
//
// Created by Marcus Rohrmoser
// http://blog.mro.name/2009/08/nsdateformatter-http-header/
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1

// No obvious license attached

@interface NSDate (RFC1123)
/**
Convert a RFC1123 'Full-Date' string
(http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1)
into NSDate.
*/
+(NSDate*)dateFromRFC1123:(NSString*)value_;

/**
Convert NSDate into a RFC1123 'Full-Date' string
(http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1).
*/
-(NSString*)rfc1123String;

@end

NSDate+RFC1123.m文件


//
// NSDate+RFC1123.m
// MKNetworkKit
//
// Originally created by Marcus Rohrmoser
// http://blog.mro.name/2009/08/nsdateformatter-http-header/
// Updated with strptime methods by Bo98
//
// No obvious license attached

#import "NSDate+RFC1123.h"
#import <time.h>
#import <xlocale.h>

@implementation NSDate (RFC1123)

+(NSDate*)dateFromRFC1123:(NSString*)value_
{
if(value_ == nil)
return nil;

const char *str = [value_ UTF8String];
const char *fmt;
NSDate *retDate;
char *ret;

fmt = "%a, %d %b %Y %H:%M:%S %Z";
struct tm rfc1123timeinfo;
memset(&rfc1123timeinfo, 0, sizeof(rfc1123timeinfo));
ret = strptime_l(str, fmt, &rfc1123timeinfo, NULL);
if (ret) {
time_t rfc1123time = mktime(&rfc1123timeinfo);
retDate = [NSDate dateWithTimeIntervalSince1970:rfc1123time];
if (retDate != nil)
return retDate;
}


fmt = "%A, %d-%b-%y %H:%M:%S %Z";
struct tm rfc850timeinfo;
memset(&rfc850timeinfo, 0, sizeof(rfc850timeinfo));
ret = strptime_l(str, fmt, &rfc850timeinfo, NULL);
if (ret) {
time_t rfc850time = mktime(&rfc850timeinfo);
retDate = [NSDate dateWithTimeIntervalSince1970:rfc850time];
if (retDate != nil)
return retDate;
}

fmt = "%a %b %e %H:%M:%S %Y";
struct tm asctimeinfo;
memset(&asctimeinfo, 0, sizeof(asctimeinfo));
ret = strptime_l(str, fmt, &asctimeinfo, NULL);
if (ret) {
time_t asctime = mktime(&asctimeinfo);
return [NSDate dateWithTimeIntervalSince1970:asctime];
}

return nil;
}

-(NSString*)rfc1123String
{
time_t date = (time_t)[self timeIntervalSince1970];
struct tm timeinfo;
gmtime_r(&date, &timeinfo);
char buffer[32];
size_t ret = strftime_l(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S GMT", &timeinfo, NULL);
if (ret) {
return @(buffer);
} else {
return nil;
}
}

@end



测试:


    NSDate* now = [NSDatedate];

   NSLog(@"now = %@",now);

   NSString* rfcStr = [now rfc1123String];

   NSLog(@"rfcStr = %@",rfcStr);

    

   NSDate* dateFormRfc = [NSDatedateFromRFC1123:rfcStr];

   NSLog(@"dateFormRfc = %@",dateFormRfc);


输出结果:

2014-12-29 16:48:15.622 [1914:89248] now = 2014-12-29 08:48:14 +0000

2014-12-29 16:48:20.061 [1914:89248] rfcStr = Mon, 29 Dec 2014 08:48:14 GMT

2014-12-29 16:48:26.303 [1914:89248] dateFormRfc = 2014-12-29 08:48:14 +0000