zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    多个数字和数字字符串混合运算规则
    关于js对象引用的小例子
    实现函数 isInteger(x) 来判断 x 是否是整数
    写一个少于 80 字符的函数,判断一个字符串是不是回文字符串
    关于数组排序
    事件委托(事件代理)的原理以及优缺点是什么?
    将url的查询参数解析成字典对象
    js dom操作获取节点的一些方法
    js中arguments的应用
    深度克隆---js对象引用
  • 原文地址:https://www.cnblogs.com/Clin/p/3395995.html
Copyright © 2011-2022 走看看