Skip to content

Instantly share code, notes, and snippets.

@krsakai

krsakai/NSDate+Utils.h

Forked from yutaaa/NSDate+Utils.h
Last active Aug 29, 2015
Embed
What would you like to do?
Objective-C NSDate Utils.
//
// NSDate+Utils.h
//
// Copyright (c) 2015 Yuta Takahashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import <Foundation/Foundation.h>
@interface NSDate (Utils)
+ (NSDate *)ytg_localTime;
/**
* 基本形式ISO8601を文字列として返す
*
* @return ex)20150701T035640+0900
*/
+ (NSString *)ytg_basicISO8601String;
/**
* 拡張形式ISO8601を文字列として返す
*
* @return ex)2015-07-01T03:56:40+09:00
*/
+ (NSString *)ytg_extendedISO8601String;
/**
* 基本形式ISO8601をNSDate(GMT)に変換する
*
* @param dateString ex)20150701T035640+0900
*
* @return ex)2015-06-30 18:56:40 +0000
*/
+ (NSDate *)ytg_basicISO8601:(NSString *)dateString;
/**
* 拡張形式ISO8601をNSDate(GMT)に変換する
*
* @param dateString ex)2015-07-01T03:56:40+09:00
*
* @return ex)2015-06-30 18:56:40 +0000
*/
+ (NSDate *)ytg_extendedISO8601:(NSString *)dateString;
@end
//
// NSDate+Utils.m
//
#import "NSDate+Utils.h"
@implementation NSDate (Utils)
+ (NSDate *)ytg_localTime {
NSDate *date = [NSDate date];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSInteger seconds = [timeZone secondsFromGMTForDate:date];
return [NSDate dateWithTimeInterval:seconds sinceDate:date];
}
+ (NSString *)ytg_basicISO8601String {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"yyyyMMdd'T'HHmmssZ"];
return [dateFormatter stringFromDate:[NSDate date]];
}
+ (NSString *)ytg_extendedISO8601String {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
return [dateFormatter stringFromDate:[NSDate date]];
}
+ (NSDate *)ytg_basicISO8601:(NSString *)dateString {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd'T'HHmmssZ"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:locale];
return [formatter dateFromString:dateString];
}
+ (NSDate *)ytg_extendedISO8601:(NSString *)dateString {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:locale];
return [formatter dateFromString:dateString];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment