문제

In my android application, I use STRFTIME function to get data from SQLite by comparing different Date Strings.

I set Promotion StartDate and EndDate to 2013-10-07 and 2013-12-31 respectively. Promotion exists when current DateTime is between StartDate and EndDate.

The following is a complete list of valid strftime() substitutions:

%d       day of month: 00
%f       fractional seconds: SS.SSS
%H       hour: 00-24
%j       day of year: 001-366
%J       Julian day number
%m       month: 01-12
%M       minute: 00-59
%s       seconds since 1970-01-01
%S       seconds: 00-59
%w       day of week 0-6 with Sunday==0
%W       week of year: 00-53
%Y       year: 0000-9999=

And I use the following query to retrieve the data.

Select * From Sal_Promotion p, Sal_Promotion_CustomerTypeAndProduct pd 
Where p.Sal_PromotionID = d.Sal_PromotionID and 
strftime('%s','now') >= strftime('%s',Sal_StartDate) and 
strftime('%s','now') = strftime('%s',Sal_EndDate) and 
Sal_CustomerTypeID = customerTypeID and 
p.Sal_PromotionID = promoID;

When I tested my application on 07 Oct 2013 morning 7 am, I get the following output.

Output:

StartDate: 2013-10-07 00:00:00 (Value: 1381104000)

EndDate: 2013-12-31 00:00:00 (Value: 1388448000)

CurrentDateTime: 2013-10-07 07:00:00 (Value: 1381100400)

CurrentDateTime value should be between StartDate and EndDate. But why the value is wrong? Current DateTime is 3600 seconds (1 hour) later than StartDate.

My current city is Singapore (GMT+8:00). Is it because of time zone difference?

Can someone point me out what is the problem?

도움이 되었습니까?

해결책

The timezone offset is your problem. SQLite3 date and time functions default to UTC.

One solution is to just compare the date strings directly

date('now') >= Sal_StartDate and date('now) <= Sal_EndDate

You can also pass a 'localtime' option to the SQLite3 date and time functions. See the SQLite3 date and time functions documentation.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top