场景
- macOS 处理时间函数有自己的 NSDate, C 的库函数 time.h相比它就有点弱了, 不过麻雀虽小, 五脏俱全. 麻烦的是在处理一些 UTC,GMT之类的转换时需要自己动手.
说明
Object-C的 NSDate结合了NSDateFormatter来设置日期属性, 比如格式化,本地,时区等等.
如果 C实现需要把本地转换为 UTC, 那么还需要对 time_t进行处理, 调用
localtime,gmtime处理, 相当麻烦, 而Object-C只需要[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];,[dateFormatter setLocale:[NSLocale currentLocale]]即可
例子
//
// TestDate.h
// TestObject-C
//
// Created by sai on 10/26/17.
// Copyright (c) 2017 sai. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TestDate : NSObject
+(void)testDate;
@end
//
// TestDate.mm
// TestObject-C
//
// Created by sai on 10/26/17.
// Copyright (c) 2017 sai. All rights reserved.
//
#import "TestDate.h"
#include <time.h>
#include <chrono>
#include <iostream>
void TimeFormat( time_t nTime, char *szDst,size_t dstLen,const char* format)
{
struct tm m_tm;
time_t tt = nTime;
m_tm = *localtime( &tt );
strftime(szDst,dstLen,format,&m_tm);
}
void GetNowDateTime(char *szDst,size_t dstLen,const char* format)
{
time_t t = time(NULL);
TimeFormat(t,szDst,dstLen,format);
}
char* Convert1900GMTDate(time_t date)
{
struct tm *ttm = localtime(&date);
if (!ttm)
return strdup("");
char* ds = (char*) malloc(24);
memset(ds, 0, 24);
sprintf(ds, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", ttm->tm_year + 1900,
ttm->tm_mon + 1, ttm->tm_mday, ttm->tm_hour, ttm->tm_min,
ttm->tm_sec);
return ds;
}
char* Convert2001GMTtoDate(time_t date)
{
if( date < 0)
date = 0;
struct tm t2001;
memset(&t2001, 0, sizeof(tm));
t2001.tm_year = 2001 - 1900;
t2001.tm_mon = 0;
t2001.tm_mday = 1;
time_t t = time(NULL);
struct tm *local = localtime(&t);
t2001.tm_hour = local->tm_hour;
struct tm *gmt = gmtime(&t);
t2001.tm_hour -= gmt->tm_hour;
time_t seconds = mktime(&t2001);
time_t at = date+seconds;
struct tm *ttm = localtime(&at);
if (!ttm){
at = date / (time_t)1000000000+seconds;
ttm = localtime(&at);
if (!ttm)
return strdup("");
}
char* ds = (char*) malloc(24);
memset(ds, 0, 24);
sprintf(ds, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", ttm->tm_year+1900,
ttm->tm_mon + 1, ttm->tm_mday, ttm->tm_hour, ttm->tm_min,
ttm->tm_sec);
return ds;
}
@implementation TestDate
+(NSString*) DateToUTCString:(NSDate*) date withFormat:(NSString*) dateComponents
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter autorelease];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:dateComponents];
NSString* date_str = [dateFormatter stringFromDate:date];
return date_str;
}
+(NSString*) DateToString:(NSDate*) date withFormat:(NSString*) dateComponents{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter autorelease];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:dateComponents];
NSString* date_str = [dateFormatter stringFromDate:date];
return date_str;
}
+(NSString*)getNowDate
{
char t[32];
memset(t, 0, sizeof(t));
GetNowDateTime(t, 32, "%Y-%m-%d");
NSString *time =[NSString stringWithUTF8String:t];
return time;
}
+(NSString*)getNowDateTimeWithFormat:(const char*)format Length:(int)length
{
char *t = (char*)malloc(length);
memset(t, 0, length);
GetNowDateTime(t, length, format);
NSString* result = [NSString stringWithUTF8String:t];
free(t);
return result;
}
+(NSString*)getNowDateTime
{
char t[32];
memset(t, 0, sizeof(t));
GetNowDateTime(t, 32, "%Y-%m-%d %H:%M:%S");
return [NSString stringWithUTF8String:t];
}
+(void)testCDate
{
auto beg = std::chrono::system_clock::now();
NSLog(@"====== testCDate ======");
NSString* now_date = [self getNowDateTime];
NSLog(@"getNowDateTime %@",now_date);
now_date = [self getNowDateTimeWithFormat:"%H:%M:%S" Length:16];
NSLog(@"getNowDateTimeWithFormat %@",now_date);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-beg;
NSLog(@"elapsed_seconds: %fs", elapsed_seconds.count());
}
+(void)testObjectCDate
{
auto beg = std::chrono::system_clock::now();
NSLog(@"====== testObjectCDate ======");
NSDate *date = [NSDate date];
NSString* now_date = [self DateToString:date withFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"DateToString yyyy-MM-dd HH:mm:ss %@",now_date);
now_date = [self DateToUTCString:date withFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"DateToUTCString %@",now_date);
now_date = [self DateToString:date withFormat:@"HH:mm:ss"];
NSLog(@"DateToString HH:mm:ss %@",now_date);
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-beg;
NSLog(@"elapsed_seconds: %fs", elapsed_seconds.count());
}
+(void)testDate
{
@autoreleasepool {
[self testCDate];
[self testObjectCDate];
}
}
@end
输出
2017-10-26 17:35:46.643 TestObject-C[6156:303] ====== testCDate ======
2017-10-26 17:35:46.644 TestObject-C[6156:303] getNowDateTime 2017-10-26 17:35:46
2017-10-26 17:35:46.645 TestObject-C[6156:303] getNowDateTimeWithFormat 17:35:46
2017-10-26 17:35:46.646 TestObject-C[6156:303] elapsed_seconds: 0.002397s
2017-10-26 17:35:46.646 TestObject-C[6156:303] ====== testObjectCDate ======
2017-10-26 17:35:46.654 TestObject-C[6156:303] DateToString yyyy-MM-dd HH:mm:ss 2017-10-26 17:35:46
2017-10-26 17:35:46.655 TestObject-C[6156:303] DateToUTCString 2017-10-26 09:35:46
2017-10-26 17:35:46.657 TestObject-C[6156:303] DateToString HH:mm:ss 17:35:46
2017-10-26 17:35:46.657 TestObject-C[6156:303] elapsed_seconds: 0.010893s
macOS时间处理
2万+

被折叠的 条评论
为什么被折叠?



