-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChrono.m
40 lines (34 loc) · 1.1 KB
/
Chrono.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// Chrono.m
// Calendar Plugin
//
// Created by Patrick Robertson on 24/03/2024.
//
#import "Chrono.h"
@implementation Chrono
- (id)init {
if (self = [super init]) {
vm = [[JSVirtualMachine alloc] init];
c = [[JSContext alloc] initWithVirtualMachine:vm];
jscode = [[NSString alloc] initWithContentsOfURL:[[NSBundle bundleForClass:[self class]] URLForResource:@"Chrono.bundle" withExtension:@"js"] encoding:NSUTF8StringEncoding error:nil];
[c evaluateScript:jscode];
}
return self;
}
+(id)sharedInstance{
static id _sharedInstance;
if (!_sharedInstance){
_sharedInstance = [[[self class] allocWithZone:[self zone]] init];
}
return _sharedInstance;
}
-(NSDate *)parse:(NSString *)phrase withLocale:(NSString *)locale {
JSValue *v = [[c objectForKeyedSubscript:@"Chrono"] objectForKeyedSubscript:@"Chrono"];
JSValue *result = [v invokeMethod:@"parse" withArguments:@[phrase, locale]];
// chrono returns a nil string if it can't parse the info
if ([result toString]) {
return [result toDate];
}
return nil;
}
@end