gpt4 book ai didi

iphone - 如何使用 Objective C 在 SQLite 中获取日期时间列

转载 作者:IT王子 更新时间:2023-10-29 06:31:01 26 4
gpt4 key购买 nike

如何使用 Objective C 在 SQLite 中获取日期时间列?

我有一个包含 4 个字段的表:pkdatetimevalue1value2pk(主键)、value1value2 是整数,所以我使用:

   int value1 = sqlite3_column_int(statement, 2);
int value1 = sqlite3_column_int(statement, 3);

但是我应该为 datetime 使用什么?

最佳答案

在 SQLite 中,本身没有日期/时间列类型,因此最终将日期表示为 Julian 日期值(实际列)或字符串(文本列)。 SQLite 在日期如何用字符串表示方面也非常特别,yyyy-MM-dd HH:mm:ss(仅)。

这些是我为处理来自 Objective-C 的 SQLite 日期而编写的一些方法。这些方法在 NSDate 上的一个类别中实现。

请务必检查 SQLite 提供的用于处理 Julian 日期的功能。我发现这些非常有用 ( http://www.sqlite.org/lang_datefunc.html )。代码示例中包含用于导出 NSDate 的 julianDay 的函数。

看起来这里也涵盖了这个主题。 Persisting Dates to SQLite3 in an iPhone Application

+ (NSDate *) dateWithSQLiteRepresentation: (NSString *) myString;
{
NSAssert3(myString, @"%s: %d; %s; Invalid argument. myString == nil", __FILE__, __LINE__, __PRETTY_FUNCTION__);

return [[self sqlLiteDateFormatter] dateFromString: myString];
}

+ (NSDate *) dateWithSQLiteRepresentation: (NSString *) myString timeZone: (NSString *) myTimeZone;
{
NSString * dateWithTimezone = nil;
NSDate * result = nil;

NSAssert3(myString, @"%s: %d; %s; Invalid argument. myString == nil", __FILE__, __LINE__, __PRETTY_FUNCTION__);
NSAssert3(myTimeZone, @"%s: %d; %s; Invalid argument. myTimeZone == nil", __FILE__, __LINE__, __PRETTY_FUNCTION__);

dateWithTimezone = [[NSString alloc] initWithFormat: @"%@ %@", myString, myTimeZone];
result = [[self sqlLiteDateFormatterWithTimezone] dateFromString: dateWithTimezone];
[dateWithTimezone release];

return result;
}

+ (NSString *) sqlLiteDateFormat;
{
return @"yyyy-MM-dd HH:mm:ss";
}

+ (NSString *) sqlLiteDateFormatWithTimeZone;
{
static NSString * result = nil;

if (!result) {
result = [[NSString alloc] initWithFormat: @"%@ zzz", [self sqlLiteDateFormat]];
}

return result;
}

+ (NSDateFormatter *) sqlLiteDateFormatter;
{
static NSDateFormatter * _result = nil;

if (!_result) {
_result = [[NSDateFormatter alloc] init];
[_result setDateFormat: [self sqlLiteDateFormat]];
}

return _result;
}

+ (NSDateFormatter *) sqlLiteDateFormatterWithTimezone;
{
static NSDateFormatter * _result = nil;

if (!_result) {
_result = [[NSDateFormatter alloc] init];
[_result setDateFormat: [self sqlLiteDateFormatWithTimeZone]];
}

return _result;
}


- (NSString *) sqlLiteDateRepresentation;
{
NSString * result = nil;

result = [[NSDate sqlLiteDateFormatter] stringFromDate: self];

return result;
}

- (NSTimeInterval) unixTime;
{
NSTimeInterval result = [self timeIntervalSince1970];

return result;
}

#define SECONDS_PER_DAY 86400
#define JULIAN_DAY_OF_ZERO_UNIX_TIME 2440587.5
- (NSTimeInterval) julianDay;
{
return [self unixTime]/SECONDS_PER_DAY + JULIAN_DAY_OF_ZERO_UNIX_TIME;
}

+ (NSDate *) dateWithJulianDay: (NSTimeInterval) myTimeInterval
{
NSDate *result = [self dateWithTimeIntervalSince1970: (myTimeInterval - JULIAN_DAY_OF_ZERO_UNIX_TIME) * SECONDS_PER_DAY];

return result;
}

关于iphone - 如何使用 Objective C 在 SQLite 中获取日期时间列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1711504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com