Mongodb bson objectid(oid)をObjective-cの生成時間に変換しますか?
-
04-10-2019 - |
質問
私はこれを見つけました:
働き: http://github.com/timburks/numongodb/blob/master/src/bson.c#l128バイト: http://github.com/timburks/numongodb/blob/master/src/platform_hacks.h#l55構造: http://github.com/timburks/numongodb/blob/master/src/bson.h#l70
しかし、これをサーバーから文字列として取得し、created_atタイムスタンプを抽出したいiPhoneアプリにこれをどのように使用しますか?これは私がこれまでに持っているものです。これはObjective-Cメソッドですが、objective-C .mファイルにCコードを配置できますか?
- timeFromBsonOid:(NSString *)oid {
time_t out;
memcpy(&out, oid, 4);
return out;
}
マット
解決
OID文字列をこのようにnsdateに変換できます。
NSString *asd = @"4c8f695bdaf9856dbe000008";
long result;
BOOL success = [[NSScanner scannerWithString:[asd substringToIndex:8]] scanHexLongLong:&result];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:result];
他のヒント
コッシの答えは少し古いものです。そうしないと、32ビットと64ビットのデバイスで奇妙な動作とクラッシュに気付く可能性があるため、署名のない長い長さを必ず使用してください。
NSString *asd = @"4c8f695bdaf9856dbe000008";
unsigned long long result;
BOOL success = [[NSScanner scannerWithString:[asd substringToIndex:8]] scanHexLongLong:&result];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:result];
所属していません StackOverflow