美文网首页
Unix时间戳--时间工具类

Unix时间戳--时间工具类

作者: 叫我龙哥 | 来源:发表于2017-03-21 22:53 被阅读27次

    工作之余写的一个关于Unix时间的工具

    //
    //  UnixTime.h
    //  UnixTimeDemo
    //
    //  Created by LiynXu on 16/1/4.
    //  Copyright © 2016年 LiynXu. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface UnixTime : NSObject
    @property (nonatomic,assign) double    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 秒
    + (UnixTime *)shareUnixTime;//单例 类方法
    - (void)getUnixTimestampAtNow;//Unix时间戳  若要获取某天的零时刻  必须先执行找个方法
    - (void)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
    
    

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

    //
    //  UnixTime.m
    //  UnixTimeDemo
    //
    //  Created by LiynXu on 16/1/4.
    //  Copyright © 2016年 LiynXu. All rights reserved.
    //
    
    #import "UnixTime.h"
    
    @implementation UnixTime
    
    + (UnixTime *)shareUnixTime{
        static UnixTime *unixTime = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            unixTime = [[UnixTime alloc]init];
        });
        return unixTime;
    }
    
    - (void)getUnixTimestampAtNow{
        self.unixDate = [NSDate date];
        NSLog(@"GMT %@",self.unixDate);
        NSTimeInterval time=[self.unixDate timeIntervalSince1970];
        self.unixTimeInterval =time;
        NSString *timeIntervalString = [NSString stringWithFormat:@"%f",self.unixTimeInterval];
        NSLog(@"GMTTimeInterval    %@",timeIntervalString);
        NSString *micSecondString = [timeIntervalString substringWithRange:NSMakeRange(timeIntervalString.length-6, 3)];
        //NSLog(@"micSec %@",micSecondString);
        NSInteger micSec = [micSecondString integerValue];
        self.unixTimestamp = (NSInteger)time+micSec/1000.000;
        NSLog(@"GMTTimestamp       %ld",(long)self.unixTimestamp);
        [self getUnixZeroTimestamp];
        [self getSystemTimeZone];
        [self getTimeOffset];
    }
    
    
    - (void)getSystemTimeZone{
        NSTimeZone *timezone = [NSTimeZone systemTimeZone];
        //NSLog(@"timeZone%@",timezone);
        NSString *timeAbbreviation = timezone.abbreviation;
        self.LocalTimeZone = timeAbbreviation;
        if ([timeAbbreviation isEqualToString:@"GMT"]) {
            self.LocalTimeZone = @"GMT+0";
        }
       // NSLog(@"TimeZone.abb: %@",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;
        }
        //NSLog(@"timeOffSet %ld",(long)self.timeOffset);
    }
    
    - (void)getUnixZeroTimestamp{
        self.unixZeroTimestamp =  (NSInteger)(self.unixTimestamp/86400)*86400;
        NSInteger days =  self.unixZeroTimestamp/86400 ;
        NSLog(@"GMTZeroTimestamp   %ld",self.unixZeroTimestamp);
        NSLog(@"days %ld",days);
    }
    
    
    - (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
        //NSLog(@"date %@",date);
        NSString *timeString = [dateFormat stringFromDate:date];
        //NSLog(@"time %@",timeString);
        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];
        NSLog(@"time %ld",time);
        return time;
    }
    
    - (NSInteger)getUnixTimeWithDay:(NSInteger)day{
        NSInteger unixTime = self.unixZeroTimestamp-(day-1)*86400-self.timeOffset;
        NSLog(@"unixTime   %ld",unixTime);
        NSLog(@"%@",[self getTimeStringWithTime:unixTime]);
        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;
        //NSLog(@"unixTime   %ld",unixTime);
        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{
        UnixTime *unixtime = [UnixTime shareUnixTime];
        NSString *string = [unixtime 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
        //NSLog(@"date %@",date);
        NSString *timeString = [dateFormat stringFromDate:date];
        //NSLog(@"time %@",timeString);
        return timeString;
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:Unix时间戳--时间工具类

          本文链接:https://www.haomeiwen.com/subject/anrnnttx.html