Beispiel #1
0
/* Calculates at1 - at2.
 * The only constraint here is that this needs to be the inverse
 * of CFAbsoluteTimeByAddingGregorianUnits(), but that's a very rigid constraint.
 * Unfortunately, due to the nonuniformity of the year and month units, this
 *  inversion essentially has to approximate until it finds the answer.
 */
CFGregorianUnits CFAbsoluteTimeGetDifferenceAsGregorianUnits(CFAbsoluteTime at1, CFAbsoluteTime at2, 
                                                             CFTimeZoneRef tz, 
                                                             CFOptionFlags unitFlags)
{
    const int32_t seconds[5] = {366 * 24 * 3600, 31 * 24 * 3600, 24 * 3600, 3600, 60};
    CFGregorianUnits units = {0, 0, 0, 0, 0, 0.0};
    CFAbsoluteTime atold, atnew = at2;
    int32_t idx, incr;
    incr = (at2 < at1) ? 1 : -1;
    /* Successive approximation: years, then months, then days, then hours, then minutes. */
    for (idx = 0; idx < 5; idx++) {
        if (unitFlags & (1 << idx)) {
            ((int32_t*)&units)[idx] = -3 * incr + (int32_t)((at1 - atnew) / seconds[idx]);
            do {
                atold = atnew;
                ((int32_t*)&units)[idx] += incr;
                atnew = CFAbsoluteTimeAddGregorianUnits(at2, tz, units);
            } while ((1 == incr && atnew <= at1) || (-1 == incr && at1 <= atnew));
            ((int32_t*)&units)[idx] -= incr;
            atnew = atold;
        }
    }
    if (unitFlags & kCFGregorianUnitsSeconds) {
        units.seconds = at1 - atnew;
    }
    if (0.0 == units.seconds) {
        units.seconds = 0.0; // stomp out possible -0.0
    }
    return units;
}
Beispiel #2
0
bool wxOSXTimerImpl::Start( int milliseconds, bool mode )
{
    (void)wxTimerImpl::Start(milliseconds, mode);

    wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") );
    wxCHECK_MSG( m_info->m_timerRef == NULL, false, wxT("attempting to restart a timer") );
    
    CFGregorianUnits gumilli ;
    memset(&gumilli,0,sizeof(gumilli) );
    gumilli.seconds = m_milli / 1000.0;

    CFRunLoopTimerContext ctx ;
    memset( &ctx, 0 , sizeof(ctx) );
    ctx.version = 0;
    ctx.info = this;

    m_info->m_timer = this;
    m_info->m_timerRef = CFRunLoopTimerCreate(
        kCFAllocatorDefault, 
        CFAbsoluteTimeAddGregorianUnits( CFAbsoluteTimeGetCurrent() , NULL, gumilli ),
        IsOneShot() ? 0 : CFTimeInterval( m_milli / 1000.0 ) ,
        0, 0, wxProcessTimer, &ctx);
    
    wxASSERT_MSG( m_info->m_timerRef != NULL, wxT("unable to create timer"));
    
    CFRunLoopRef runLoop = 0;
#if wxOSX_USE_IPHONE
    runLoop = CFRunLoopGetMain();
#else
    runLoop = CFRunLoopGetCurrent();
#endif
    CFRunLoopAddTimer( runLoop, m_info->m_timerRef, kCFRunLoopCommonModes) ;


    return true;
}