Created
February 22, 2018 06:23
-
-
Save rok-git/00568e7c6ad0b40e1e4b0729628e3187 to your computer and use it in GitHub Desktop.
parse a string as Date and Time in any format
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
// parse a date and time | |
#import <Foundation/Foundation.h> | |
int | |
main(int argc, char *argv[]) | |
{ | |
@autoreleasepool{ | |
NSError *error = nil; | |
if(argc != 2){ | |
return -1; | |
} | |
NSString *str = [NSString stringWithUTF8String: argv[1]]; | |
NSDataDetector *det = [NSDataDetector dataDetectorWithTypes: NSTextCheckingTypeDate error: &error]; | |
NSTextCheckingResult *match = [det firstMatchInString: str options: 0 range: NSMakeRange(0, [str length])]; | |
if(match){ | |
NSDate *date = [match date]; | |
NSLog(@"%@ → %@", str, date); | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment