[squeak-dev] Testing membership in a Set of Date objects

Tim Johnson digit at sonic.net
Fri Oct 19 01:04:19 UTC 2018


Hi,

I am very grateful to see the discussions about timezones on the list 
recently, because I just got hit by it today.

I couldn't figure out why a Set containing Date objects didn't properly 
detect membership of a Date which was clearly present in the Set. It was 
because, I realize now, the Date objects in the Set had an offset of  
-0:07:00:00 (they are created by DateAndTime>>#now), and Dates created 
by e.g. "Date today" or "Date yesterday" either have no offset or the 
offset is 0:00:00:00.  I have found the comment in Timespan 
class>>#defaultOffset -- but it does not encourage me that I could 
change my defaultOffset to help myself.

In my tests (code snippet below), if I make an OrderedCollection from 
the Set, the OrderedCollection can find the date object, even though the 
Set can't.

In the Mean Time (get it?), can I create Dates with offsets?  
"(DateAndTime now - 1 day) asDate" works...?  Or are there any other 
workarounds?  Should I walk away from Date and use something else?

Thanks!

(in a Workspace)
| dates |
dates := Set newFrom: { Date yesterday. Date tomorrow.  DateAndTime now 
asDate }.
dates includes: (Date year: 2018 month: 10 day: 18).   "=> false"
dates asOrderedCollection includes: (Date year: 2018 month: 10 day: 18). 
  "=> true"


(in an inspector on a Set of dates with offsets)
| a m |
a := Date year: 2018 month: 10 day: 17.
self includes: a.				"-> false"
self like: a.					"-> nil"
self asOrderedCollection includes: a.  "-> true"
m := self array at: 4.		"-> 17 October 2018"
m start offset.				"-> -0:07:00:00"
a start offset				"-> 0:00:00:00"



More information about the Squeak-dev mailing list