[share] IOS development – use of nstimezone time zone setting and use timestamp to calculate time difference

Time:2022-1-2

This problem is encountered in the project. After I get the time string from the background, I need to deal with the calculation event of “time difference between two times” in the previous period. I found that the time zone in China was changed from 12:04 GMT to 8:0000 GMT in 2016 (i.e. the time zone in 2016 was changed from 8:04 GMT to 57:0000 GMT), which is the standard time zone in China, Beijing time used in China is GMT + 8.

With this idea, I began to try to

dateFormatter.timeZone = [NSTimeZonetimeZoneWithAbbreviation:@"GMT+0800"];

To set the time zone, but it didn’t work.
Then I tried to add the abbreviation of Chinese standard time name and set its standard name to @ “Asia / Shanghai”, which did not work.

In the trouble, I found a way to successfully solve this problem:

[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];// Directly specify the time zone. This is East Zone 8

By the way, several time zone setting methods are listed

[NSTimeZone systemTimeZone];// System time zone  
[NSTimeZone defaultTimeZone];// The default time zone seems to be no different from the previous one 
[NSTimeZone timeZoneForSecondsFromGMT:0];// This is the GMT + 0 time zone

Code example:
A method of calculating two times with timestamp. The code is not optimized, but only the implementation is completed, which provides a preliminary idea

NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
        [dateFormatter1 setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        [dateFormatter1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];// You may need to set the time zone, which is set as East 8, that is, Beijing time
        NSDate * questionDate = [dateFormatter1 dateFromString:self.questionCreatetime];// The time is 14:48:46 + 0000 on April 12, 2016
        
        NSString * date2 = [MyUtil getNoneNilString:self.answerDic[@"createtime"]];
        NSDate * answerDate = [dateFormatter1 dateFromString:date2];// Time: April 12, 2016 14:57:58 + 0000
        
        //Convert to timestamp
        NSString * timeSp1 = [NSString stringWithFormat:@"%ld", (long)[questionDate timeIntervalSince1970]];
        NSString * timeSp2 = [NSString stringWithFormat:@"%ld", (long)[answerDate timeIntervalSince1970]];
        NSInteger time1 = [timeSp1 integerValue];//1460472526
        NSInteger time2 = [timeSp2 integerValue];//1460473078
        NSInteger response = time2 - time1;//552
//        NSString * theResponse = [NSString stringWithFormat:@"%@", @(response)];//1970-01-01 00:09:12 +0000
        NSTimeInterval theResponse = response;
        NSDate * responseTimeInterval = [NSDate dateWithTimeIntervalSince1970:theResponse];
        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm:ss"];
        [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
        NSString * responseTime = [dateFormatter stringFromDate:responseTimeInterval];// Output result 00:09:12