diff --git a/entitlement/src/main/java/com/ning/billing/entitlement/api/EntitlementDateHelper.java b/entitlement/src/main/java/com/ning/billing/entitlement/api/EntitlementDateHelper.java
index 5c80493..e1a1697 100644
--- a/entitlement/src/main/java/com/ning/billing/entitlement/api/EntitlementDateHelper.java
+++ b/entitlement/src/main/java/com/ning/billing/entitlement/api/EntitlementDateHelper.java
@@ -1,6 +1,7 @@
package com.ning.billing.entitlement.api;
import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import com.ning.billing.account.api.Account;
@@ -26,29 +27,44 @@ public class EntitlementDateHelper {
* Relies on the subscriptionStartDate for the reference time
*
* @param requestedDate
- * @param subscriptionStartDate
+ * @param referenceDateTime
* @param callContext
* @return
* @throws EntitlementApiException
*/
- public DateTime fromLocalDateAndReferenceTime(final LocalDate requestedDate, final DateTime subscriptionStartDate, final InternalCallContext callContext) throws EntitlementApiException {
+ public DateTime fromLocalDateAndReferenceTime(final LocalDate requestedDate, final DateTime referenceDateTime, final InternalCallContext callContext) throws EntitlementApiException {
try {
+ /*
final Account account = accountApi.getAccountByRecordId(callContext.getAccountRecordId(), callContext);
final DateAndTimeZoneContext timeZoneContext = new DateAndTimeZoneContext(subscriptionStartDate, account.getTimeZone(), clock);
final DateTime computedTime = timeZoneContext.computeUTCDateTimeFromLocalDate(requestedDate);
- //return computedTime.isAfter(clock.getUTCNow()) ? clock.getUTCNow() : computedTime;
return computedTime;
+ */
+
+ final Account account = accountApi.getAccountByRecordId(callContext.getAccountRecordId(), callContext);
+ final LocalDate localDateNowInAccountTimezone = new LocalDate(requestedDate, account.getTimeZone());
+
+ // Datetime from local date in account timezone and with given reference time
+ final DateTime t1 = localDateNowInAccountTimezone.toDateTime(referenceDateTime.toLocalTime(), account.getTimeZone());
+ // Datetime converted back in UTC
+ final DateTime t2 = new DateTime(t1, DateTimeZone.UTC);
+ return t2;
+
} catch (AccountApiException e) {
throw new EntitlementApiException(e);
}
}
- public DateTime fromNowAndReferenceTime(final DateTime subscriptionStartDate, final InternalCallContext callContext) throws EntitlementApiException {
+ public DateTime fromNowAndReferenceTime(final DateTime referenceDateTime, final InternalCallContext callContext) throws EntitlementApiException {
try {
final Account account = accountApi.getAccountByRecordId(callContext.getAccountRecordId(), callContext);
- final DateAndTimeZoneContext timeZoneContext = new DateAndTimeZoneContext(subscriptionStartDate, account.getTimeZone(), clock);
- return timeZoneContext.computeUTCDateTimeFromNow();
+ final LocalDate localDateNowInAccountTimezone = new LocalDate(clock.getUTCNow(), account.getTimeZone());
+ // Datetime from local date in account timezone and with given reference time
+ final DateTime t1 = localDateNowInAccountTimezone.toDateTime(referenceDateTime.toLocalTime(), account.getTimeZone());
+ // Datetime converted back in UTC
+ final DateTime t2 = new DateTime(t1, DateTimeZone.UTC);
+ return t2;
} catch (AccountApiException e) {
throw new EntitlementApiException(e);
}
diff --git a/entitlement/src/test/java/com/ning/billing/entitlement/api/TestEntitlementDateHelper.java b/entitlement/src/test/java/com/ning/billing/entitlement/api/TestEntitlementDateHelper.java
new file mode 100644
index 0000000..b793490
--- /dev/null
+++ b/entitlement/src/test/java/com/ning/billing/entitlement/api/TestEntitlementDateHelper.java
@@ -0,0 +1,63 @@
+package com.ning.billing.entitlement.api;
+
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.LocalDate;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import com.ning.billing.account.api.Account;
+import com.ning.billing.entitlement.EntitlementTestSuiteNoDB;
+import com.ning.billing.util.callcontext.InternalTenantContext;
+
+public class TestEntitlementDateHelper extends EntitlementTestSuiteNoDB {
+
+ private Account account;
+ private EntitlementDateHelper dateHelper;
+
+ @BeforeClass(groups = "fast")
+ public void beforeMethod() throws Exception {
+ super.beforeClass();
+
+
+ account = Mockito.mock(Account.class);
+ Mockito.when(accountInternalApi.getAccountByRecordId(Mockito.anyLong(), Mockito.<InternalTenantContext>any())).thenReturn(account);
+ dateHelper = new EntitlementDateHelper(accountInternalApi, clock);
+ }
+
+ @Test(groups = "fast")
+ public void testWithAccountInUtc() throws EntitlementApiException {
+
+ final LocalDate initialDate = new LocalDate(2013, 8, 7);
+ clock.setDay(initialDate);
+
+ Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC);
+
+ final DateTime refererenceDateTime = new DateTime(2013, 1, 1, 15, 43, 25, 0, DateTimeZone.UTC);
+ final DateTime targetDate = dateHelper.fromNowAndReferenceTime(refererenceDateTime, internalCallContext);
+ final DateTime expectedDate = new DateTime(2013, 8, 7, 15, 43, 25, 0, DateTimeZone.UTC);
+ Assert.assertEquals(targetDate, expectedDate);
+ }
+
+
+ @Test(groups = "fast")
+ public void testWithAccountInUtcMinus8() throws EntitlementApiException {
+
+ // We start with a time only 6:43:25 < 8 -> localTime in accountTimeZone will be 2013, 8, 6
+ clock.setTime(new DateTime(2013, 8, 7, 6, 43, 25, 0, DateTimeZone.UTC));
+
+ final DateTimeZone timeZoneUtcMinus8 = DateTimeZone.forOffsetHours(-8);
+ Mockito.when(account.getTimeZone()).thenReturn(timeZoneUtcMinus8);
+
+ // We also use a reference time of 1, 28, 10, 0 -> DateTime in accountTimeZone will be (2013, 8, 6, 1, 28, 10)
+ final DateTime refererenceDateTime = new DateTime(2013, 1, 1, 1, 28, 10, 0, DateTimeZone.UTC);
+ final DateTime targetDate = dateHelper.fromNowAndReferenceTime(refererenceDateTime, internalCallContext);
+
+ // And so that datetime in UTC becomes expectedDate below
+ final DateTime expectedDate = new DateTime(2013, 8, 6, 9, 28, 10, 0, DateTimeZone.UTC);
+ Assert.assertEquals(targetDate, expectedDate);
+ }
+
+}