Commit 3729e923226f0b7920fe11ba07f3808cb694ac90
1 parent
0b5b676f
0.0.3
Showing
3 changed files
with
51 additions
and
1 deletions
CNLiveBaseTools.podspec
CNLiveBaseTools/Classes/CNLiveTimeTools.h
| ... | ... | @@ -42,6 +42,14 @@ NS_ASSUME_NONNULL_BEGIN |
| 42 | 42 | + (NSInteger)getIntervalsWithTimeStamp:(NSTimeInterval)timeStamp; |
| 43 | 43 | //时间转换为时间戳,精确到微秒 |
| 44 | 44 | + (NSTimeInterval)getTimeStampWithDate:(NSDate *)date; |
| 45 | +//时间转换为时间戳,精确到秒 | |
| 46 | ++ (NSTimeInterval)get10TimeStampWithDate:(NSDate *)date; | |
| 47 | + | |
| 48 | +//根据时间戳返回时间 | |
| 49 | ++ (NSString *)getDateByTimeStamp:(NSString *)timeStampString formatter:(NSString *)formatterStr; | |
| 50 | + | |
| 51 | +//传入 秒 得到 xx:xx:xx | |
| 52 | ++ (NSString *)getMMSSFromSS:(NSString *)totalTime; | |
| 45 | 53 | |
| 46 | 54 | @end |
| 47 | 55 | ... | ... |
CNLiveBaseTools/Classes/CNLiveTimeTools.m
| ... | ... | @@ -220,5 +220,47 @@ |
| 220 | 220 | { |
| 221 | 221 | return [[NSNumber numberWithDouble:[date timeIntervalSince1970] * 1000 * 1000] longLongValue]; |
| 222 | 222 | } |
| 223 | ++ (NSTimeInterval)get10TimeStampWithDate:(NSDate *)date{ | |
| 224 | + return [[NSNumber numberWithDouble:[date timeIntervalSince1970]] longLongValue]; | |
| 225 | +} | |
| 226 | + | |
| 227 | +#pragma mark - 根据时间戳返回时间 | |
| 228 | ++ (NSString *)getDateByTimeStamp:(NSString *)timeStampString formatter:(NSString *)formatterStr { | |
| 229 | + if(timeStampString.length == 13){ | |
| 230 | + NSTimeInterval interval = [timeStampString doubleValue] / 1000.0; | |
| 231 | + NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval]; | |
| 232 | + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
| 233 | + [formatter setDateFormat:formatterStr]; | |
| 234 | + NSString *dateString = [formatter stringFromDate: date]; | |
| 235 | + return dateString; | |
| 236 | + } | |
| 237 | + // iOS 生成的时间戳是10位 | |
| 238 | + NSTimeInterval interval = [timeStampString doubleValue]; | |
| 239 | + NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval]; | |
| 240 | + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | |
| 241 | + [formatter setDateFormat:formatterStr]; | |
| 242 | + NSString *dateString = [formatter stringFromDate: date]; | |
| 243 | + return dateString; | |
| 244 | + | |
| 245 | +} | |
| 223 | 246 | |
| 247 | +//传入 秒 得到 xx:xx:xx | |
| 248 | ++ (NSString *)getMMSSFromSS:(NSString *)totalTime{ | |
| 249 | + if(!totalTime){ | |
| 250 | + return @"00:00:00"; | |
| 251 | + } | |
| 252 | + NSInteger seconds = [totalTime integerValue]; | |
| 253 | + | |
| 254 | + //format of hour | |
| 255 | + NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600]; | |
| 256 | + //format of minute | |
| 257 | + NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60]; | |
| 258 | + //format of second | |
| 259 | + NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60]; | |
| 260 | + //format of time | |
| 261 | + NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second]; | |
| 262 | + | |
| 263 | + return format_time; | |
| 264 | + | |
| 265 | +} | |
| 224 | 266 | @end | ... | ... |