iOS--时间戳

谷梁英资
2023-12-01

写在前面

目前的所有电子产品的时间都是按照全球统一的Unix时间来计时的,unix时间又是从1970年1月1日0时刻开始的。所以无论是苹果还是安卓 还是Windows 或者其他的linux系统都是同一套时间体系;

由于本人项目开发中经常会配件需要时间计算的工作,所以呢在工作之余就把项目中的代码拿出来单独写的一个关于Unix时间计算的工具类,以供大家使用。

主要属性:

@interface TimeCalculator : NSObject
@property (nonatomic,assign) NSTimeInterval    unixTimeInterval;//GMT时间戳 微秒级
@property (nonatomic,strong) NSDate   *unixDate;//GMT时间 2016-01-04 8:55:46 +0000
@property (nonatomic,strong) NSString *LocalTimeZone;//本地时区 GMT-12  GMT+12
@property (nonatomic,assign) NSInteger timeOffset;//时间偏移量
@property (nonatomic,assign) double    unixTimestamp;//GMT+0 毫秒
@property (nonatomic,assign) NSInteger unixZeroTimestamp;//当天零点 GMT+0 秒

主要方法:

+ (TimeCalculator *)shareTimeCalculator;//单例 类方法
- (NSTimeInterval)getUnixTimestampAtNow;//Unix时间戳  若要获取某天的零时刻  必须先执行找个方法
- (NSString *)getSystemTimeZone;//本地时区
- (NSString *)getTimeStringWithTime:(double)time;//字符串输出时间 设置日期格式带毫秒的 2016-01-04 16:55:46
- (NSInteger)getUnixTimeWithDay:(NSInteger)day;// 某天的零时Unix时间戳
- (NSInteger)getUnixTimeWithDay:(NSInteger)day AndClock:(NSInteger)clock; // 某天的特定时刻Unix时间戳
- (NSInteger)gettimestampWithDateFormatString:(NSString *)dateFormatString;
- (NSString *)formatTimeWithTime:(NSNumber *)time;//根据传入时间数值 返回hh:mm:ss格式的时间  这个是表示时间点的
- (NSString *)formatHMWithTime:(NSNumber *)time;//根据传入时间数值 返回hh:mm格式的时间 这个时用来表示时间长短的
- (NSString *)getTravelTimeWithStartTime:(NSNumber *)startTime andEndTime:(NSNumber *)endTime;//根据传入时间数值返回时间差值分钟 这个时用来表示时间长短的
- (NSString *)getDayHourMinWithTimeStamp:(NSNumber *)time;
@end

TimeCalculator.m文件
包含方法的具体实现,用法都在.h文件里写了


+ (TimeCalculator *)shareTimeCalculator{
    static TimeCalculator *unixTime = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        unixTime = [[TimeCalculator alloc]init];
    });
    return unixTime;
}
- (NSTimeInterval)getUnixTimestampAtNow{
    self.unixDate = [NSDate date];
    NSTimeInterval time=[self.unixDate timeIntervalSince1970];
    self.unixTimeInterval =time;
    NSString *timeIntervalString = [NSString stringWithFormat:@"%f",self.unixTimeInterval];
    NSString *micSecondString = [timeIntervalString substringWithRange:NSMakeRange(timeIntervalString.length-6, 3)];
    NSInteger micSec = [micSecondString integerValue];
    self.unixTimestamp = (NSInteger)time+micSec/1000.000;
    [self getUnixZeroTimestamp];
    [self getSystemTimeZone];
    [self getTimeOffset];
    return self.unixTimeInterval;
}

- (NSString *)getSystemTimeZone{
    NSTimeZone *timezone = [NSTimeZone systemTimeZone];
    NSString *timeAbbreviation = timezone.abbreviation;
    self.LocalTimeZone = timeAbbreviation;
    if ([timeAbbreviation isEqualToString:@"GMT"]) {
        self.LocalTimeZone = @"GMT+0";
    }
    return self.LocalTimeZone;
}
- (void)getTimeOffset{
    NSString *str1 = [self.LocalTimeZone substringWithRange:NSMakeRange(3, 1)];
    NSString *str2 = [self.LocalTimeZone substringWithRange:NSMakeRange(4, self.LocalTimeZone.length-4)];
    NSInteger timeZoneOffset = [str2 integerValue];
    if ([str1 isEqualToString:@"+"]) {
        self.timeOffset = timeZoneOffset*3600;
    }else if ([str1 isEqualToString:@"-"]){
        self.timeOffset = -timeZoneOffset*3600;
    }
}
- (NSInteger)getUnixZeroTimestamp{
    self.unixZeroTimestamp =  (NSInteger)(self.unixTimestamp/86400)*86400;
    return self.unixZeroTimestamp;
}

- (NSString *)getTimeStringWithTime:(double)time{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    // 设置日期格式带秒的 2016-01-04 16:55:46
    NSString *timeString = [dateFormat stringFromDate:date];
    return timeString;
}
- (NSInteger)gettimestampWithDateFormatString:(NSString *)dateFormatString{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormat dateFromString:dateFormatString];
    NSInteger time= [date timeIntervalSince1970];
    return time;
}
- (NSInteger)getUnixTimeWithDay:(NSInteger)day{
    NSInteger unixTime = self.unixZeroTimestamp-(day-1)*86400-self.timeOffset;
    return unixTime;
}
- (NSInteger)getUnixTimeWithDay:(NSInteger)day AndClock:(NSInteger)clock{//特定时刻的Unix时间戳
    
    if (clock<0||clock>24) {//为了避免传入数据不正确 进行换算 增强可靠性
        clock = clock%24;
        if (clock<0) {
            clock=clock+24;
        }else{
            clock=clock;
        }
    }else{
        clock=clock;
    }
    
    NSInteger unixTime = self.unixZeroTimestamp-(day-1)*86400+3600*clock;//-self.timeOffset;
    return unixTime;
}
- (NSString *)formatTimeWithTime:(NSNumber *)time{
    float _time_2 = [time floatValue];
    NSInteger _time_1 = [time integerValue];
    NSInteger sec = (NSInteger)((_time_2-_time_1)*60);
    NSInteger hour;
    NSInteger min;
    if (_time_1>=1 && _time_1<60) {
        min = _time_1%60;
        return [NSString stringWithFormat:@"%ld:%.2ld",min,sec];
    }
    if (_time_1>=60) {
        hour = _time_1/60;
        min = _time_1%60;
        return [NSString stringWithFormat:@"%ld:%.2ld:%.2ld",hour,min,sec];
    }
    return [NSString stringWithFormat:@"0:%.2ld",sec];
}
- (NSString *)formatHMWithTime:(NSNumber *)time{
    TimeCalculator *timeCalculator = [TimeCalculator shareTimeCalculator];
    NSString *string = [timeCalculator getTimeStringWithTime:[time integerValue]];
    NSArray *firArray = [string componentsSeparatedByString:@" "];
    NSString *firstring = firArray[1];
    
    NSMutableArray *secArray = [NSMutableArray arrayWithArray:[firstring componentsSeparatedByString:@":"]];
    [secArray removeLastObject];
    
    return [secArray componentsJoinedByString:@":"];
    
}
- (NSString *)getTravelTimeWithStartTime:(NSNumber *)startTime andEndTime:(NSNumber *)endTime{
    NSInteger _startTime = [startTime integerValue];
    NSInteger _endTime = [endTime integerValue];
    NSInteger travelTime = (_endTime - _startTime)/60+1;
    return [NSString stringWithFormat:@"%ld",travelTime];
}
- (NSString *)getDayHourMinWithTimeStamp:(NSNumber *)time{
    NSInteger _time = [time integerValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_time];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm"];
    // 设置日期格式 2016-01-04 16:55:46
    NSString *timeString = [dateFormat stringFromDate:date];
    return timeString;
}

最后

这个时间工具类写的并不是很完美,具体的可能需要各位在去修改了

源码地址

欢迎访问!

 类似资料: