I just ran into this ERXTimestampUtilities.firstDateInSameMonth issue. The current behavior of firstDateInSameMonth yields the following results which is not what I expected (but is partially explainable by the zero-based month reporting)
val t = new NSTimestamp() //
> t : com.webobjects.foundation.NSTimestamp = 2014-03-05 02:27:56
Etc/GMT
val s = ERXTimestampUtilities.firstDateInSameMonth(t)
> s : com.webobjects.foundation.NSTimestamp = 2014-01-28 05:00:00
Etc/GMT
val e = ERXTimestampUtilities.firstDateInNextMonth(t)
> e : com.webobjects.foundation.NSTimestamp = 2014-02-28 05:00:00
Etc/GMT
Using Joda Time got me what I was expecting:
val f = new DateMidnight(t).withDayOfMonth(1).toDate()
> f : java.util.Date = Sat Mar 01 00:00:00 EST 2014
val l = new DateMidnight(f).plusMonths(1).toDate()
> l : java.util.Date = Tue Apr 01 00:00:00 EDT 2014
Updating firstDateInSameMonth to not care about the day of the month and add 1
to the value returned by monthOfYear gave me more what I was expecting.
public static NSTimestamp firstDateInSameMonth(NSTimestamp value) {
return new NSTimestamp(yearOfCommonEra(value), monthOfYear(value)+1, 1,
0, 0, 0, NSTimeZone.defaultTimeZone());
}
val t = new NSTimestamp() //
> t : com.webobjects.foundation.NSTimestamp = 2014-03-05 02:45:35
Etc/GMT
val s = ERXTimestampUtilities.firstDateInSameMonth(t)
> s : com.webobjects.foundation.NSTimestamp = 2014-03-01 05:00:00
Etc/GMT
val e = ERXTimestampUtilities.firstDateInNextMonth(t)
> e : com.webobjects.foundation.NSTimestamp = 2014-04-01 05:00:00
Etc/GMT
val f = new DateMidnight(t).withDayOfMonth(1).toDate()
> f : java.util.Date = Sat Mar 01 00:00:00 EST 2014
val l = new DateMidnight(f).plusMonths(1).toDate()
> l : java.util.Date = Tue Apr 01 00:00:00 EDT 2014
Does this fix the firstDateInMonth issue or am I missing something? I see
earlier in this thread that changing monthOfYear to be 1-based instead of
0-based was proposed. I haven’t given much thought as to whether adhering to
the GregorianCalendar values would be more or less confusing so I left that the
same.
Is there someone else pushing changes into this issue? I don’t want to work at
cross-purposes.
On Jan 14, 2013, at 3:07 PM, Johann Werner <[email protected]> wrote:
> Hi Louis,
>
> can you wrap that up into a pull request patching ERXTimestampUtilities
> instead of ERXTimestampUtility (which has been deprecated)? If you already
> wrote some testing code you could add that to ERXTest too :-)
>
> Am 10.12.2012 um 22:20 schrieb Louis Demers <[email protected]>:
>
>> Hi,
>>
>> I tried using ERXTimestampUtility.firstDateInNextMonth() and
>> ERXTimestampUtility.firstDateInSameMonth() and didn't get what I expected.
>> Either it's a bug or, the name is less than ideal. I started testing other
>> calls in like firstDateInSameWeek() and discovered problems as well.
>>
>> If anybody is using those calls, please explain what they are supposed to do
>> ...
>>
>>
>> Here are some test I ran on the 2012-11-20 22:33:13 Etc/GMT
>>
>> <snip>
>>
>> So here are what I would propose
>>
>> // First we should correct the offset by one (making month 1 based instead
>> of 0 base)
>
> I agree that this would make more sense though that would certainly break
> peoples code and if I see it correct it mimics the return values of
> GregorianCalendar which is 0-based. Perhaps some Javadocs specifying that bit
> of info would be better?
>
> jw
>
>>
>> public static int monthOfYear(NSTimestamp t) {
>> return 1 + calendarForTimestamp(t).get(Calendar.MONTH);
>> }
>>
>> Then correct the faulty methods
>>
>> public NSTimestamp firstDateInSameWeek(NSTimestamp value) {
>> return new NSTimestamp(
>> ERXTimestampUtility.yearOfCommonEra(value),
>> ERXTimestampUtility.monthOfYear(value) ,
>> ERXTimestampUtility.dayOfMonth(value) -
>> ERXTimestampUtility.dayOfWeek(value) + 1,
>> 0, 0, 0, NSTimeZone.defaultTimeZone()
>> );
>> }
>>
>> public NSTimestamp firstDateInSameMonth(NSTimestamp value) {
>> return new NSTimestamp(ERXTimestampUtility.yearOfCommonEra(value),
>> ERXTimestampUtility.monthOfYear(value), 1, 0, 0, 0,
>> NSTimeZone.defaultTimeZone());
>> }
>>
>>
>>
>> Cheers
>>
>>
>> Louis Demers eng.
>> Vice-President, Co-Founder
>> Obzerv Technologies Inc.
>> 400 Jean Lesage, suite 201
>> Quebec, QC, Canada
>> G1K 8W1
>> T 418.524.3522
>> F 418.524.6745
>> www.obzerv.com
>>
>>
>>
>>
>>
>>
>> Louis Demers eng.
>> www.obzerv.com
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list ([email protected])
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/webobjects-dev/elemgee%40gmail.com
>
> This email sent to [email protected]
signature.asc
Description: Message signed with OpenPGP using GPGMail
_______________________________________________ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list ([email protected]) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [email protected]
