Created
October 2, 2012 14:33
-
-
Save naokits/3819591 to your computer and use it in GitHub Desktop.
日付処理関連
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Utils.m | |
// StoreKitEventChecker | |
// | |
// Created by Naoki TSUTSUI on 12/01/30. | |
// | |
#import "Utils.h" | |
#include <objc/runtime.h> | |
@implementation Utils | |
//----------------------------------------------------------------------------- | |
// | |
//----------------------------------------------------------------------------- | |
// 文字列からNSDateを生成 | |
// 文字列(NSString)からNSDateへの変換はNSDateFormatを使用する | |
// | |
// ISO-8601形式の日付をNSDateに変換 | |
- (NSDate *)string2Date:(NSString *)strDate | |
{ | |
LOG_CURRENT_METHOD; | |
if (!strDate) { | |
return [NSDate date]; | |
} | |
NSDateFormatter *sISO8601 = [[NSDateFormatter alloc] init]; | |
[sISO8601 setTimeStyle:NSDateFormatterFullStyle]; | |
[sISO8601 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; | |
if ([strDate hasSuffix:@"Z"]) { | |
strDate = [[strDate substringToIndex:(strDate.length-1)] stringByAppendingString:@"GMT"]; | |
} | |
NSDate *date = [sISO8601 dateFromString:strDate]; | |
return date; | |
} | |
//----------------------------------------------------------------------------- | |
/*! | |
// 現在設定されているロケールによる時間(NSDate)を返す | |
// @param dateString 日付文字列 | |
「2012-01-04 19:12:02 Etc/GMT」という形式の日付文字列の場合、「Etc/」部分はNSDateFormatterでは | |
フォーマット化できないので、"Etc/"の部分を無視する様にフォーマットを設定する。 | |
NSDateFormatterを使用して無視したい箇所は、'Etc/'ZZZ」のようにアポストロフィーで囲む | |
Data Formatting Guide:Date Formatters | |
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1 | |
*/ | |
//----------------------------------------------------------------------------- | |
- (NSDate *)dateFromString:(NSString *)dateString | |
{ | |
LOG_CURRENT_METHOD; | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss 'Etc/'ZZZ"]; | |
[dateFormatter setLocale:[NSLocale currentLocale]]; | |
NSDate *date = [dateFormatter dateFromString:dateString]; | |
return date; | |
} | |
//----------------------------------------------------------------------------- | |
// | |
//----------------------------------------------------------------------------- | |
+ (void)showDateFromString:(NSString *)dateString locale:(NSLocale *)locale | |
{ | |
Utils *u = [[Utils alloc] init]; | |
NSDate *date = [u dateFromString:dateString]; | |
[u showDate:date style:NSDateFormatterFullStyle]; | |
} | |
//----------------------------------------------------------------------------- | |
// | |
//----------------------------------------------------------------------------- | |
- (NSDate *)dateFromInterval:(NSNumber *)interval | |
{ | |
NSDate *date; | |
date = [NSDate dateWithTimeIntervalSince1970:[interval doubleValue] / 1000.0f]; | |
return date; | |
} | |
//----------------------------------------------------------------------------- | |
// | |
//----------------------------------------------------------------------------- | |
+ (void)showDateFromInterval:(NSNumber *)interval locale:(NSLocale *)locale | |
{ | |
Utils *u = [[Utils alloc] init]; | |
NSDate *date = [u dateFromInterval:interval]; | |
[u showDate:date style:NSDateFormatterFullStyle]; | |
} | |
//----------------------------------------------------------------------------- | |
// 指定した日付スタイルで時刻を表示する | |
//----------------------------------------------------------------------------- | |
- (void)showDate:(NSDate *)date style:(NSDateFormatterStyle)style | |
{ | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
[dateFormatter setLocale:[NSLocale currentLocale]]; // FIXME: 修正する | |
switch (style) { | |
case NSDateFormatterNoStyle: | |
// スタイルの指定なし NSDateFormatterNoStyleだと何も表示されない | |
break; | |
case NSDateFormatterShortStyle: | |
// ショートスタイルの指定 | |
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; | |
[dateFormatter setDateStyle:NSDateFormatterShortStyle]; | |
NSLog(@"Date ShortStyle %@", [dateFormatter stringFromDate:date]); | |
break; | |
case NSDateFormatterMediumStyle: | |
// ミディアムスタイルの指定 | |
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; | |
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; | |
NSLog(@"Date MediumStyle %@", [dateFormatter stringFromDate:date]); | |
break; | |
case NSDateFormatterFullStyle: | |
// フルスタイルの指定 | |
[dateFormatter setTimeStyle:NSDateFormatterFullStyle]; | |
[dateFormatter setDateStyle:NSDateFormatterFullStyle]; | |
NSLog(@"Date FullStyle %@", [dateFormatter stringFromDate:date]); | |
break; | |
case NSDateFormatterLongStyle: | |
// ロングスタイルの指定 | |
[dateFormatter setTimeStyle:NSDateFormatterLongStyle]; | |
[dateFormatter setDateStyle:NSDateFormatterLongStyle]; | |
NSLog(@"Date LongStyle %@", [dateFormatter stringFromDate:date]); | |
break; | |
default: | |
break; | |
} | |
// return [dateFormatter stringFromDate:date]; | |
} | |
/* | |
LOG(@"class:%@", [purchaseDateString class]); | |
NSString *className = NSStringFromClass([purchaseDateString class]); | |
LOG(@"className:%@", className); | |
*/ | |
// クラスの持つメソッド名を全て表示する。 | |
+ (void)showMethodsWithClass:(Class)class | |
{ | |
// unsigned i; | |
// Class c = [NSDictionary class]; | |
unsigned numMethods; | |
Method *methods = class_copyMethodList(class, &numMethods); | |
for (unsigned i=0; i<numMethods; ++i) { | |
// <rdar://problem/6190950> method_getName crash on NSObject method when GC is enabled | |
SEL aMethod = method_getName(methods[i]); | |
NSLog(@"メソッド名:%@", NSStringFromSelector(aMethod)); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment