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>
  • 相关阅读:
    用ps命令查看进程的内存
    女人
    一个中文系高材生的高水平请假条
    常用元件封装
    premiere 视频滤镜详解
    Start of Authority Record (SOA) (转) Anny
    DNS Record Format and Types Anny
    域名解析的配置文件 /etc/resolv.conf Anny
    System Information Record (HINFO)(转) Anny
    Bug分析:为bug预防奠定基础 (转) Anny
  • 原文地址:https://www.cnblogs.com/Clin/p/3395995.html
Copyright © 2011-2022 走看看