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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

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