如何在没有硬编码手动计算的情况下用 UTC 时区偏移量偏移 NSDate [英] How to offset NSDate with UTC timezone offset without hardcoded manual calculation

查看:56
本文介绍了如何在没有硬编码手动计算的情况下用 UTC 时区偏移量偏移 NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设当前当地时间是 15:11 UTC.我从服务器检索了一个数据集,该数据集显示了如下所示的企业的开业关闭时间:

Imagine the current local time being 15:11 UTC. I retrieve a data set from the server showing the opening closing time of a business displayed like so:

{
close = {
   day = 3;
   time = 0200;
};
open = {
   day = 2;
   time = 1700;
};

我还收到一个 utc-offset 属性,如下所示:"utc_offset" = "-420"; 我想这是一个分钟偏移量,给出 7 小时的小时偏移量,考虑到我所在的时区是 UTC,我收到的营业地点的营业时间信息适用于比洛杉矶晚 7 小时的营业时间.

I also receive a utc-offset property exposed like so: "utc_offset" = "-420"; which I imagine is a minute offset giving an hour offset of 7 hours which seems right considering the timezone I'm in is UTC and the business location's opening hours information I'm receiving is for a business in Los Angeles who are 7 hours behind.

我如何使用此属性然后能够对其进行任何时间计算我想确定当前本地时间是否介于我已经计算出的开盘时间和收盘时间之间,但考虑到时间比较是在本地时区完成的,当它需要在针对该时间范围进行计算之前进行偏移时,计算结果是错误的.

How do I use this property to then be able to do any time calculations on it I want to determine whether the current local time falls between the open and close time that bit I have figured out but the calculations come out wrong considering the time comparison is done in the local timezone when it needs to be offset before calculating against that time range.

我尽量避免做类似的事情

I'm trying to avoid doing things like

伪代码:NSDate.date 小时组件 + (UTC_offset/60 = -7 小时)

更新:以下是我目前正在检查商家现在是否营业

Update: Here's how I'm currently checking if the business is open right now

        if currentArmyTime.compare(String(openInfo.time)) != .OrderedAscending && currentArmyTime.compare(String(closeInfo.time)) != .OrderedDescending {
            //The business is open right now, though this will not take into consideration the business's time zone offset.
        }

是否更容易抵消当前时间?

Is it easier to offset the current time?

推荐答案

在日期操作中使用打开"和关闭"时间之前,您需要从已设置为时区的日历创建一个 NSDate那些时候.举个例子:

Before you can use the 'open' and 'close' times in date operations you need to create an NSDate from a calendar that has been set to the time zone for those times. Here's an example:

// Create calendar for the time zone
NSInteger timeOffsetInSeconds = -420 * 60;
NSTimeZone *tz = [NSTimeZone timeZoneForSecondsFromGMT:timeOffsetInSeconds];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
calendar.timeZone = tz;

// Create an NSDate from your source data
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.day = 1;
comps.month = 1;
comps.year = 2016;
comps.hour = 8;
comps.minute = 0;
NSDate *openTime = [calendar dateFromComponents:comps];

// 'openTime' can now be to compared with local time.
NSLog(@"openTime = %@", openTime);  // Result is openTime = 2016-01-01 15:00:00 +0000

您应该将上述代码放入一个方法中,该方法接收原始时间和要应用的时间偏移量.

You should put the above code into a method that takes in the raw time and the time offset to apply.

这篇关于如何在没有硬编码手动计算的情况下用 UTC 时区偏移量偏移 NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆