• NSString+TimeCategory


    NSString+TimeCategory.h
    //------------------------------------------------
    #import <foundation foundation.h="">
     
     
    @interface NSString (TimeCategory) 
    + (NSString *)stringWithTime:(NSTimeInterval)time;
    - (NSTimeInterval)timeValue;
     
    @end
    //------------------------------------------------
    //NSString+TimeCategory.m
    //------------------------------------------------
    #import "NSString+TimeCategory.h"
     
     
    @implementation NSString (TimeCategory)
     
    + (NSString *)stringWithTime:(NSTimeInterval)time {
        BOOL isPositive;
        NSInteger timeInt;
         
        if (time > 3600 * 24 || time < - 3600 * 24)
            return nil;
        if (time < 0) {
            timeInt = (NSInteger)-time;
            isPositive = NO;
        } else {
            timeInt = (NSInteger)time;
            isPositive = YES;
        }
     
     
        NSInteger hour = timeInt/3600;
        NSInteger minute = (timeInt%3600)/60;
        NSInteger second = (timeInt%3600)%60;
     
        if (hour > 0) {
            if (isPositive) {
                return [NSString stringWithFormat:@"%d%d:%d%d:%d%d", 
    hour/10, hour%10, minute/10, minute%10, second/10, second%10];
            } else {
                return [NSString stringWithFormat:@"-%d%d:%d%d:%d%d", 
    hour/10, hour%10, minute/10, minute%10, second/10, second%10];
            }
     
        } else {
            if (isPositive) {
                return [NSString stringWithFormat:@"%d%d:%d%d", minute/10, minute%10, second/10, second%10];
            } else {
                return [NSString stringWithFormat:@"-%d%d:%d%d", minute/10, minute%10, second/10, second%10];
            }
     
        }
    }
     
    - (NSTimeInterval)timeValue {
        NSInteger hour = 0, minute = 0, second = 0;
        NSArray *sections = [self componentsSeparatedByString:@":"];
        NSInteger count = [sections count];
        second = [[sections objectAtIndex:count - 1] integerValue];
        minute = [[sections objectAtIndex:count - 2] integerValue];
        if (count > 2) {
            hour = [[sections objectAtIndex:0] integerValue];
        }
        return hour * 3600 + minute * 60 + second;
    }
     
    @end
    </foundation>
  • 相关阅读:
    MyBatis 学习记录3 MapperMethod类
    MyBatis 学习记录2 Mapper对象是如何生成的
    MyBatis 学习记录1 一个简单的demo
    hadoop学习记录1 初始hadoop
    Java枚举类的serialVersionUID
    docker学习记录1
    mysql utf8方式连接查看表数据乱码的问题
    在Spring中使用Redis Lua脚本批量删除缓存
    redis 批量删除键
    前后端分离跨域问题解决方案
  • 原文地址:https://www.cnblogs.com/Clin/p/3395995.html
Copyright © 2020-2023  润新知