首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CGEventTimestamp到NSDate

CGEventTimestamp到NSDate
EN

Stack Overflow用户
提问于 2009-10-21 05:19:53
回答 4查看 2.1K关注 0票数 1

我只是尝试将CGEventTimestamp (它是一个无符号的64位整数,大致表示系统启动后的纳秒)转换为NSDate。我知道有一种方法可以将“自系统启动以来的时间”转换为NSTimeInterval或相对于参考日期的日期,但我找不到它。

如何将CGEventTimestamp转换为NSDate可以接受的内容?谢谢。

(忘了说,需要10.5友好,如果可能的话,避免碳排放)

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-10-20 21:42:25

代码语言:javascript
运行
复制
// Determine time in NSTimeInterval seconds this event took place after system start
GCEventTimestamp nanoseconds = GetIt();
NSTimeInterval seconds = (NSTimeInterval)nanoseconds / 1000000000.0;

// GetCurrentEventTime() gives you time in seconds since system start
EventTime currentTimeInSeconds = GetCurrentEventTime();

// Given this you can figure out the date of system start, and then add
// your event time
NSDate* startTime = [NSDate dateWithTimeIntervalSinceNow:-currentTimeInSeconds];
NSDate* eventTime = [startTime dateByAddingTimeInterval:seconds];
票数 4
EN

Stack Overflow用户

发布于 2009-10-21 23:26:24

nall让我找到了正确的方向。我在NSDate上拼凑了一个类别,它负责细节,既对10.5/10.6友好,又不使用碳。谢谢你的帮助,nall!

NSDate-Additions.h

代码语言:javascript
运行
复制
#import <Foundation/Foundation.h>
@interface NSDate (NSDate_Additions)

+(NSTimeInterval) timeIntervalSinceSystemStartup;
-(NSTimeInterval) timeIntervalSinceSystemStartup;
+(NSDate *) dateOfSystemStartup;
+(NSDate *) dateWithCGEventTimestamp:(CGEventTimestamp)cgTimestamp;

@end

NSDate-Additions.m

代码语言:javascript
运行
复制
#import "NSDate-Additions.h"
#include <assert.h>
#include <CoreServices/CoreServices.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>

#if MAC_OS_X_VERSION_MAX_ALLOWED == MAC_OS_X_VERSION_10_5
@interface NSProcessInfo (SnowLeopard)
- (NSTimeInterval)systemUptime;
@end

@interface NSDate (SnowLeopard)
- (id)dateByAddingTimeInterval:(NSTimeInterval)seconds;
@end
#endif


// Boosted from Apple sample code
uint64_t UpTimeInNanoseconds(void)
{
    uint64_t        time;
    uint64_t        timeNano;
    static mach_timebase_info_data_t    sTimebaseInfo;

    time = mach_absolute_time();

    // Convert to nanoseconds.

    // If this is the first time we've run, get the timebase.
    // We can use denom == 0 to indicate that sTimebaseInfo is
    // uninitialised because it makes no sense to have a zero
    // denominator is a fraction.

    if ( sTimebaseInfo.denom == 0 ) {
        (void) mach_timebase_info(&sTimebaseInfo);
    }

    // Do the maths.  We hope that the multiplication doesn't
    // overflow; the price you pay for working in fixed point.

    timeNano = time * sTimebaseInfo.numer / sTimebaseInfo.denom;

    return timeNano;
}


@implementation NSDate (NSDate_Additions)

+(NSTimeInterval) timeIntervalSinceSystemStartup
{
    NSTimeInterval interval;
    long sysVersion;

    Gestalt( gestaltSystemVersion, &sysVersion );
    if( sysVersion >= 0x1060 )
        interval = [[NSProcessInfo processInfo] systemUptime];
    else
        interval = UpTimeInNanoseconds() / 1000000000.0;

    return( interval );
}

-(NSTimeInterval) timeIntervalSinceSystemStartup
{
    return( [self timeIntervalSinceDate:[NSDate dateOfSystemStartup]] );
}

+(NSDate *) dateOfSystemStartup
{
    return( [NSDate dateWithTimeIntervalSinceNow:-([NSDate timeIntervalSinceSystemStartup])] );
}

+(NSDate *) dateWithCGEventTimestamp:(CGEventTimestamp)cgTimestamp
{
    NSDate *ssuDate = nil;
    NSDate *cgDate = nil;
    long sysVersion;

    ssuDate = [NSDate dateOfSystemStartup];

    Gestalt( gestaltSystemVersion, &sysVersion );
    if( sysVersion >= 0x1060 )
        cgDate = [ssuDate dateByAddingTimeInterval:(cgTimestamp/1000000000.0)];
    else
        cgDate = [ssuDate addTimeInterval:(cgTimestamp/1000000000.0)];

    return( cgDate );
}

@end
票数 3
EN

Stack Overflow用户

发布于 2009-12-08 17:26:48

关于上面的代码NSDate-Additions.m。行"timeNano = time * sTimebaseInfo.numer / sTimebaseInfo.denom;“将导致iPhone OS 2.2.1上的内存损坏。修复方法是使用如下类型的强制转换: timeNano = time * (uint64_t)sTimebaseInfo.numer / sTimebaseInfo.denom;

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1597383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档