时间你懂的,
//
// TimeUtil.m
// QTui
//
// Created by wangyanan on 9/14/15.
// Copyright (c) 2015 wangyanan. All rights reserved.
//
#import "TimeUtil.h"
@interface TimeUtil()
@property(nonatomic,strong) NSCalendar *currentCalendar;
@property(nonatomic,strong) NSDateFormatter *formatter;
@property(nonatomic,strong) NSDateComponents *resultComps;
@end
@implementation TimeUtil
+(TimeUtil *)sharedTimeUtil
{
static TimeUtil *timeUtil = nil;
@synchronized(self) {
if (!timeUtil) {
timeUtil = [[self alloc]init];
}
}
return timeUtil;
}
/**
* 强大的dateCalendar
date --->dateComponent ---->dateCalendar
*
* @param date
*
* @return
*/
-(NSDateComponents *)dateComponentByDate:(NSDate *)date
{
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
return [self.currentCalendar components:unitFlags fromDate:date];
}
-(NSString *)startTimeStr:(NSInteger)timeType
{
NSString *formateStr = nil;
NSString *returnStr = nil;
switch (timeType)
{
case MHour:
{
formateStr = @"yyyyMMddHH";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSDateComponents *currentComps = [self dateComponentByDate:date];
[currentComps setYear:[currentComps year]];
[currentComps setMonth:[currentComps month]];
[currentComps setDay:[currentComps day]];
[currentComps setHour:0];
// [currentComps setHour:[currentComps hour]-12];
NSDate *returnDate = [self.currentCalendar dateFromComponents:currentComps];
returnStr = [self.formatter stringFromDate:returnDate];
break;
}
case MDay:
{
formateStr = @"yyyyMMdd";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSDateComponents *currentComps = [self dateComponentByDate:date];
[currentComps setYear:[currentComps year]];
[currentComps setMonth:[currentComps month]];
[currentComps setDay:[currentComps day]-7];
[currentComps setHour:0];
NSDate *returnDate = [self.currentCalendar dateFromComponents:currentComps];
returnStr = [self.formatter stringFromDate:returnDate];
break;
}
case MWeek:
{
formateStr = @"yyyyww";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSDateComponents *currentComps = [self dateComponentByDate:date];
// [currentComps setWeekOfYear:[currentComps weekOfYear]-4];
NSDateComponents *mComps = [[NSDateComponents alloc]init];
[mComps setYear:[currentComps year]];
[mComps setWeekday:[currentComps weekday]];
[mComps setWeekOfYear:[currentComps weekOfYear]-4];
NSDate *returnDate = [self.currentCalendar dateFromComponents:mComps];
returnStr = [self.formatter stringFromDate:returnDate];
break;
}
default:
{
formateStr = @"yyyyMM";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSDateComponents *currentComps = [self dateComponentByDate:date];
// [currentComps setYear:[currentComps year]];
[currentComps setMonth:[currentComps month]-4];
NSDate *returnDate = [self.currentCalendar dateFromComponents:currentComps];
returnStr = [self.formatter stringFromDate:returnDate];
break;
}
}
TTLog(@"....%@",returnStr);
return returnStr;
}
-(NSString *)endTimeStr:(NSInteger)timeType
{
NSString *formateStr = nil;
NSString *returnStr = nil;
switch (timeType)
{
case MHour:
{
formateStr = @"yyyyMMddHH";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSDateComponents *currentComps = [self dateComponentByDate:date];
[currentComps setYear:[currentComps year]];
[currentComps setMonth:[currentComps month]];
[currentComps setDay:[currentComps day]];
[currentComps setHour:12];
NSDate *returnDate = [self.currentCalendar dateFromComponents:currentComps];
returnStr = [self.formatter stringFromDate:returnDate];
break;
}
case MDay:
{
formateStr = @"yyyyMMdd";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
returnStr = [self.formatter stringFromDate:date];
break;
}
case MWeek:
{
formateStr = @"yyyyww";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
returnStr = [self.formatter stringFromDate:date];
break;
}
default:
{
formateStr = @"yyyyMM";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
returnStr = [self.formatter stringFromDate:date];
break;
}
}
TTLog(@"....%@",returnStr);
return returnStr;
}
/**
* 显示在X轴的时间格式
*
* @param
*
* @return
*/
-(NSArray *)sTimeWithType:(StatisTimeType)type
{
NSString *formateStr = nil;
NSString *returnStr = nil;
NSMutableArray *returnArray = [NSMutableArray array];
switch (type) {
case MHour:
{
// yyyy-MM-dd HH:mm:ss.fff
// 按小时=2015082010,按天=20150820,按周=201534,按月份=201508</li>
formateStr = @"yyyyMMddHH";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
NSDateComponents *currentComps = [self.currentCalendar components:unitFlags fromDate:date];
[self.resultComps setYear:[currentComps year]];
[self.resultComps setMonth:[currentComps month]];
[self.resultComps setDay:[currentComps day]];
// for(int i =0;i<12;i++){
for(int i =11;i>-1;i--){
[self.resultComps setHour:[currentComps hour]-i];
NSDate *returnDate = [self.currentCalendar dateFromComponents:self.resultComps];
self.formatter.dateFormat = @"HH点";
returnStr = [self.formatter stringFromDate:returnDate];
[returnArray addObject:returnStr];
}
break;
}
case MDay:
{
formateStr = @"yyyyMMdd";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
NSDateComponents *currentComps = [self.currentCalendar components:unitFlags fromDate:date];
[self.resultComps setYear:[currentComps year]];
[self.resultComps setMonth:[currentComps month]];
for(int i =6;i>-1;i--){
[self.resultComps setDay:[currentComps day]-i];
NSDate *returnDate = [self.currentCalendar dateFromComponents:self.resultComps];
self.formatter.dateFormat = @"MM月dd日";
returnStr = [self.formatter stringFromDate:returnDate];
[returnArray addObject:returnStr];
}
break;
}
case MWeek:
{
formateStr = @"yyyyww";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
NSDateComponents *currentComps = [self.currentCalendar components:unitFlags fromDate:date];
NSDateComponents *mComps = [[NSDateComponents alloc]init];
[mComps setYear:[currentComps year]];
[mComps setWeekday:[currentComps weekday]];
NSMutableArray *tempArray = [NSMutableArray array];
for(int i =3;i>-1;i--){
[mComps setWeekOfYear:[currentComps weekOfYear]-i];
NSDate *returnDate = [self.currentCalendar dateFromComponents:mComps];
self.formatter.dateFormat = @"yyyy年ww周";
NSString *returnStr = [self.formatter stringFromDate:returnDate];
[tempArray addObject:returnStr];
}
for (NSString *timeStr in tempArray) {
self.formatter.dateFormat = @"yyyy年ww周";
NSDate *mDate = [self.formatter dateFromString:timeStr];
currentComps = [self.currentCalendar components:unitFlags fromDate:mDate];
[mComps setYear:[currentComps year]];
[mComps setMonth:[currentComps month]];
[mComps setDay:[currentComps day]];
self.formatter.dateFormat = @"MM.dd";
NSDate *returnDate = [self.currentCalendar dateFromComponents:mComps];
NSString * strOne = [self.formatter stringFromDate:returnDate];
[mComps setDay:[currentComps day]+6];
returnDate = [self.currentCalendar dateFromComponents:mComps];
NSString * strTwo = [self.formatter stringFromDate:returnDate];
returnStr = [NSString stringWithFormat:@"%@-%@",strOne,strTwo];
[returnArray addObject:returnStr];
}
break;
}
default:
{
formateStr = @"yyyyMM";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
NSDateComponents *currentComps = [self.currentCalendar components:unitFlags fromDate:date];
[self.resultComps setYear:[currentComps year]];
[self.resultComps setDay:[currentComps day]];
[self.resultComps setHour:[currentComps hour]];
for(int i =3;i>-1;i--){
[self.resultComps setMonth:[currentComps month]-i];
self.formatter.dateFormat = @"yyyy年MM月";
NSDate *returnDate = [self.currentCalendar dateFromComponents:self.resultComps];
returnStr = [self.formatter stringFromDate:returnDate];
[returnArray addObject:returnStr];
}
break;
}
}
return returnArray;
}
/**
* 传递给服务器的时间格式
*
* @param type
*
* @return
*/
-(NSArray *)tTimeWithType:(StatisTimeType)type
{
NSString *formateStr = nil;
NSString *returnStr = nil;
NSMutableArray *returnArray = [NSMutableArray array];
switch (type) {
case MHour:
{
// yyyy-MM-dd HH:mm:ss.fff
// 按小时=2015082010,按天=20150820,按周=201534,按月份=201508</li>
formateStr = @"yyyyMMddHH";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
NSDateComponents *currentComps = [self.currentCalendar components:unitFlags fromDate:date];
[self.resultComps setYear:[currentComps year]];
[self.resultComps setMonth:[currentComps month]];
[self.resultComps setDay:[currentComps day]];
for(int i =11;i>-1;i--){
[self.resultComps setHour:[currentComps hour]-i];
NSDate *returnDate = [self.currentCalendar dateFromComponents:self.resultComps];
returnStr = [self.formatter stringFromDate:returnDate];
[returnArray addObject:returnStr];
}
// for(int i =0;i<12;i++){
// [self.resultComps setHour:i];
// NSDate *returnDate = [self.currentCalendar dateFromComponents:self.resultComps];
// returnStr = [self.formatter stringFromDate:returnDate];
// [returnArray addObject:returnStr];
//
// }
break;
}
case MDay:
{
formateStr = @"yyyyMMdd";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
NSDateComponents *currentComps = [self.currentCalendar components:unitFlags fromDate:date];
[self.resultComps setYear:[currentComps year]];
[self.resultComps setMonth:[currentComps month]];
for(int i =6;i>-1;i--){
[self.resultComps setDay:[currentComps day]-i];
NSDate *returnDate = [self.currentCalendar dateFromComponents:self.resultComps];
returnStr = [self.formatter stringFromDate:returnDate];
[returnArray addObject:returnStr];
}
break;
}
case MWeek:
{
formateStr = @"yyyyww";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
NSDateComponents *currentComps = [self.currentCalendar components:unitFlags fromDate:date];
NSDateComponents *mComps = [[NSDateComponents alloc]init];
[mComps setYear:[currentComps year]];
[mComps setWeekday:[currentComps weekday]];
for(int i =3;i>-1;i--){
[mComps setWeekOfYear:[currentComps weekOfYear]-i];
NSDate *returnDate = [self.currentCalendar dateFromComponents:mComps];
NSString *returnStr = [self.formatter stringFromDate:returnDate];
[returnArray addObject:returnStr];
}
break;
}
default:
{
formateStr = @"yyyyMM";
self.formatter.dateFormat = formateStr;
NSDate *date = [NSDate date];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond|NSCalendarUnitWeekOfYear;
NSDateComponents *currentComps = [self.currentCalendar components:unitFlags fromDate:date];
[self.resultComps setYear:[currentComps year]];
[self.resultComps setDay:[currentComps day]];
[self.resultComps setHour:[currentComps hour]];
for(int i =3;i>-1;i--){
[self.resultComps setMonth:[currentComps month]-i];
NSDate *returnDate = [self.currentCalendar dateFromComponents:self.resultComps];
returnStr = [self.formatter stringFromDate:returnDate];
[returnArray addObject:returnStr];
}
break;
}
}
return returnArray;
}
-(NSDateFormatter *)formatter
{
if (!_formatter) {
_formatter = [[NSDateFormatter alloc]init];
}
return _formatter;
}
-(NSCalendar *)currentCalendar
{
if (!_currentCalendar) {
_currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
}
return _currentCalendar;
}
-(NSDateComponents *)resultComps
{
if (!_resultComps) {
_resultComps = [[NSDateComponents alloc]init];
}
return _resultComps;
}
@end