Unix时间戳--时间工具类

工作之余写的一个关于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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,860评论 25 709
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,244评论 2 33
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,656评论 9 468
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,079评论 19 139
  • 圆梦 作为一个患有严重拖延症的人,想完成一篇没有期限的分享文也是相当不容易了。上上上个月我去爬了梦寐以求的富士山...
    Yihan虎二妞阅读 604评论 0 0