简体   繁体   English

如何在iPhone SDK中找到用户已更改时间的信息?

[英]how to find that user has changed time in iphone sdk?

I have created an application.in the app i start a countdown timer and save current date as double value in database also i save end date according to countdown timer .after closing and return to app i get time form database find the difference between current date and start date and set the countdown according to that.but a problem occurs when i change the time 8 hours ahead then the count down timer behaves differently.how do i find that user has changed time or time zone? 我已经创建了一个应用程序。在应用程序中,我启动一个倒数计时器并将当前日期保存为数据库中的双精度值,同时我根据倒数计时器保存了结束日期。关闭并返回到应用程序后,我获取了时间表单数据库,找到了当前日期之间的差和开始日期并据此设置倒计时。但是当我将时间更改为提前8小时时出现问题,那么倒数计时器的行为会有所不同。我如何发现用户已更改了时间或时区? by the if i change time zone suppose new delhi to usa it works fine but in the same time zone if i increase/decrease date or time it does not behave as expected.how do i manage this? 如果我更改时区,假设新德里飞往美国,则可以正常工作,但是如果我增加/减少日期或时间不符合预期,则在同一时区中该如何处理?

Also i had seen a strange problem when increase/decrease date or time the app closes as soon as it launched.i am unable to figure it out 当增加/减少日期或应用程序启动时关闭的时间时,我也遇到了一个奇怪的问题。我无法弄清楚

For your timer to be exact, you should just use the end date. 为了使您的计时器准确无误,您应该只使用结束日期。 I'm not sure why you want to keep the start date. 我不确定为什么要保留开始日期。

Here's an example with a continuous countdown: 这是一个连续倒计时的示例:

// I'm assuming you want to update the countdown every seconds
// So you should set a timer like this somewhere
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick) userInfo:nil repeats:YES];

- (void)updateCountdown {
    NSTimeInterval timeInterval = [endDate timeIntervalSinceNow];

    unsigned int d, h, m, s;
    NSTimeInterval r;

    d = timeInterval / (24 * 3600);
    r = timeInterval - d * 24 * 3600;

    h = r / 3600;
    r = r - h * 3600;

    m = r / 60;
    r = r - m * 60;

    s = r;

    NSString *countdown = [NSString stringWithFormat:@"%.2d:%.2d:%.2d:%.2d", d, h, m, s];
    // Now you could use countdown to update the text of a UILabel
}

- (void)timerTick {
    if ([endDate compare:[NSDate date]] == NSOrderedDescending) {
        //endDate is later in time than "now", ie not yet reached
        [self updateCountdown];
    } else {
        //stop the countdown
        [timer invalidate];
    }
}

endDate should be a NSDate containing the date when the countdown should reach 0. endDate应该是一个NSDate,其中包含倒计时应达到0的日期。

Because the countdown is adjusted every seconds, you won't have issues even if the system time or timezone changes. 由于倒计时是每秒钟调整一次,因此即使系统时间或时区发生变化,您也不会遇到任何问题。

However make sure you save and restore endDate correctly upon app restart/close (ie use a fixed reference always! I would use the timeIntervalSinceReferenceDate and dateWithTimeIntervalSinceReferenceDate: from the NSDate class). 但是,请确保在应用程序重新启动/关闭时正确保存和还原endDate(即始终使用固定引用!我将使用NSDate类中的timeIntervalSinceReferenceDate和dateWithTimeIntervalSinceReferenceDate:)。

If you're just interested in having a timer firing once at endDate use something like this: 如果您只想让计时器在endDate触发一次,请使用以下方法:

NSTimer* timer = [[NSTimer alloc] initWithFireDate:endDate
                                          interval:0.0f
                                            target:self
                                          selector:@selector(endDateReached:)
                                          userInfo:nil
                                           repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer release];

I'm not sure how you can get around the user changing the time while your application isn't running. 我不确定如何在应用程序不运行时改变用户的时间。 But I don't think you need to store the startDate or the duration of the timer. 但是我认为您不需要存储startDate或计时器的持续时间。 What you could do is store the endDate in the database. 您可以做的是将endDate存储在数据库中。 I would try and store it in a timezone agnostic manner, like GMT. 我会尝试以与时区无关的方式将其存储,例如GMT。

Then the next time your application is started, take the dates stored in your database and convert them back to the local time. 然后,下次启动应用程序时,获取存储在数据库中的日期,并将其转换回本地时间。 Also get the current time from the NSDate class when your application is started. 启动应用程序时,还要从NSDate类获取当前时间。 The current time can then be subtracted from the converted endDate to calculate the timer duration. 然后可以从转换后的endDate中减去当前时间,以计算计时器持续时间。

This should allow the user to change their time zone and still fire events at the correct time. 这应该允许用户更改其时区,并且仍然在正确的时间触发事件。 You'd also have less to store in your database. 您还可以更少地存储在数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM