objective-c中获取时间的步骤

objective-c中获取时间的方法

------头文件

#import <Foundation/Foundation.h>

#include <mach/mach_time.h>


@interface TimeStampHelper : NSObject

+ (NSTimeInterval)timestamp;

@end


----实现文件



#import "TimeStamp.h"


@implementation TimeStampHelper


+ (NSTimeInterval)timestamp

{

    // get the timebase info -- different on phone and OSX

    mach_timebase_info_data_t info;

    mach_timebase_info(&info);

    

    // get the time

    uint64_t absTime = mach_absolute_time();

    

    // apply the timebase info

    absTime *= info.numer;

    absTime /= info.denom;

    

    // convert nanoseconds into seconds and return

    return (NSTimeInterval) ((double) absTime / 1000000000.0);

}


@end