使用SQLITE3和Objective C转换DATETIME的问题 [英] Issues converting DATETIME using SQLITE3 and Objective C

查看:255
本文介绍了使用SQLITE3和Objective C转换DATETIME的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DATETIME列的sqlite3数据库,其列格式如下:_
2013-01-09 04:00:00



我试图找出如何确定时间是AM还是PM?



这是我读的并使用Objective-C从Sqlite3 DB转换DATETIME对象:

  NSDateFormatter * dateFormat = [[NSDateFormatter alloc ] 在里面]; 
[dateFormat setDateFormat:@yyyy-MM-dd hh:mm:ss];
char * DateTimeColumn =(char *)sqlite3_column_text(statement,13);
NSDate * myDate = [dateFormat dateFromString:[NSString stringWithUTF8String:DateTimeColumn]];

这个工作真的很好,除了它不知道这是4:00 AM还是下午4:00 。


我看到一个帖子建议在格式化程序字符串的末尾添加一个 a ,如下所示:

yyyy-MM-dd hh:mm:ss a _
...找出AM / PM - 但是导致了崩溃。 (我当然也在我的DATETIME列中添加了 AM PM 到实际值,如:
2013-01-09 04:00:00 AM


但它仍然崩溃



不知道我做错了什么 - 或者这是不能做的。



是唯一的解决方案将DB中的条目全部为24小时格式(意思是:下午4:00应输入16:00) - 然后使用 IF 语句来减去12从值> 12,以确定一个时间的AM或PM'ness?_
我会认为这将有一些自动化功能...任何想法?



===============================



编辑/更新:

将字符串转换为NSDate后,我突然得到一个'(null)',这是当前版本的代码:

  NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@yyyy -MM-dd hh:mm:ss];
char * DateTimeColumn =(char *)sqlite3_column_text(statement,13);
NSString * DateTimeString = [NSString stringWithUTF8String:DateTimeColumn];
NSLog(@DateTimeString =%@,DateTimeString);
//这个NSLog给我一个很好的字符串:DateTimeString = 2013-01-09 16:00:00
//但是当我从这个字符串创建一个NSDate对象:
NSDate * myDate = [dateFormat dateFromString:DateTimeString];
// ...并注销:
NSLog(@REAL DATETIME object =%@,[myDate description]);
//控制台显示:REAL DATETIME object =(null)
//当我将NSDate对象添加到NSDate数组时,我崩溃:
[tempNSDateObjectsArray addObject:我的约会];

控制台显示:
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,
原因:'*** - [__ NSArrayM insertObject:atIndex:]:对象不能nil'
***首次调用堆栈:


解决方案

HH ,而不是 hh ,是以24小时制日期格式:

  [dateFormat setDateFormat:@yyyy-MM-dd HH:mm:ss]; 

使用 hh解析2013-01-09 16:00:00 因为小时格式失败,因此返回 nil



请注意, NSDateFormatter 根据本地时区解释日期字符串,除非您设置了明确的时区。您可能希望将时区信息添加到字符串中,或​​者使用不同的格式(例如,从UNIX开始,自1.1.1970开始的UNIX时间)来存储日期。






ADDED: SQLite datetime()函数返回一个字符串YYYY-MM-DD HH:MM: SS是UTC / GMT的日期和时间。要将此类字符串转换为 NSDate ,请使用 NSDateFormatter 并将时区设置为GMT:

  NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@yyyy-MM-dd HH:mm:ss];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSString * dateTimeString = @2013-01-09 16:00:00;
NSLog(@DateTimeString =%@,dateTimeString);

NSDate * myDate = [dateFormat dateFromString:dateTimeString];
NSLog(@myDate:%@,myDate);

输出:

  dateTimeString = 2013-01-09 16:00:00 
myDate:= 2013-01-09 16:00:00 +0000

要向用户显示日期,请转换 NSDate NSString ,使用 stringFromDate ,但不要设置时区(以便使用本地时区):

  NSDateFormatter * dateFormat2 = [[NSDateFormatter alloc] init]; 
[dateFormat2 setDateFormat:@yyyy-MM-dd HH:mm:ss];

NSString * localDateString = [dateFormat2 stringFromDate:myDate];
NSLog(@localDateString:%@,localDateString);

输出:

  localDateString:2013-01-09 17:00:00 

现在,时间显示为 17:00:00 ,因为我当地的时区是GMT + 01。


I have a sqlite3 Database with a DATETIME column containing values formatted like this:
2013-01-09 04:00:00

I'm trying to figure out how to determine if times are AM or PM?

Here's how I read and convert the DATETIME objects from my Sqlite3 DB using Objective-C:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
char *DateTimeColumn = (char *)sqlite3_column_text(statement, 13);
NSDate *myDate =[dateFormat dateFromString:[NSString stringWithUTF8String:DateTimeColumn]];

This works really well except it doesn't figure out whether this is 4:00AM or 4:00PM.

I saw a posting suggesting adding an a at the end of the formatter string like so:
"yyyy-MM-dd hh:mm:ss a"
... to figure out AM/PM - but that caused crashes. (I of course also added AM and PM to the actual values in my DATETIME column like so:
2013-01-09 04:00:00 AM
but its still crashed.

Not sure if I'm doing something wrong - or if this just can't be done.

Is the only solution to have the entries in the DB itself all be in the 24hr. format (meaning: 4:00PM should be entered as 16:00) - and then use IF statements to subtract 12 from values > 12 so as to determine a time's AM or PM'ness?
I would have thought there'd be some automated function for this... Any ideas?

=================================

EDIT/UPDATE:
I'm suddenly getting a '(null)' after converting the string into an NSDate. Here's the current version of the code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
char *DateTimeColumn = (char *)sqlite3_column_text(statement, 13);
NSString *DateTimeString = [NSString stringWithUTF8String:DateTimeColumn];
NSLog(@"DateTimeString = %@", DateTimeString);
   // This NSLog gives me a good string: "DateTimeString = 2013-01-09 16:00:00"
// But when I create an NSDate object from this string:
NSDate *myDate =[dateFormat dateFromString:DateTimeString];
// ... and log it out: 
NSLog(@"the REAL DATETIME object = %@", [myDate description]);
// the console shows: "the REAL DATETIME object = (null)"
// and when I add this NSDate object to my Array of NSDates, I crash:
[tempNSDateObjectsArray addObject:myDate];

Console shows: 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',     
reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:

解决方案

HH, not hh, is for the 24-hour date format:

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

Parsing "2013-01-09 16:00:00" with hh as hour-format fails and therefore returns nil.

Note that NSDateFormatter interprets the date string according to the local timezone, unless you set an explicit time zone. You might want to add the time zone information to the strings, or use different format (e.g. UNIX time, seconds since 1.1.1970) to store the date.


ADDED: The SQLite datetime() function returns a string "YYYY-MM-DD HH:MM:SS" which is the date and time in UTC/GMT. To convert such a string to a NSDate, use a NSDateFormatter and set the time zone to GMT:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSString *dateTimeString = @"2013-01-09 16:00:00";
NSLog(@"DateTimeString = %@", dateTimeString);

NSDate *myDate =[dateFormat dateFromString:dateTimeString];
NSLog(@"myDate:          %@", myDate);

Output:

dateTimeString = 2013-01-09 16:00:00
myDate:        = 2013-01-09 16:00:00 +0000

To present a date to the user, convert NSDate to NSString, using stringFromDate, but do not set a time zone (so that the local time zone is used):

NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString *localDateString = [dateFormat2 stringFromDate:myDate];
NSLog(@"localDateString: %@", localDateString);

Output:

localDateString: 2013-01-09 17:00:00

The time is now shown as 17:00:00, because my local time zone is "GMT+01".

这篇关于使用SQLITE3和Objective C转换DATETIME的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆