Pregunta

I have an NSArray of CFAbsoluteTimes. They should be sorted from earliest to latest, but if not I can sort them.

What I need to do is find the min and max date (e.g. Jan 1 to Jan 5) and create a bucketization that shows the count for each day between, e.g.:

Jan 1 - 1
Jan 2 - 0
Jan 3 - 4
Jan 4 - 0
Jan 5 - 3

Something like that. What is the simplest way to turn the absolute times into a rounded NSDate of some sort I can count? Intermediate forms don't really matter to me. I just need to write a function that returns a count when given a date.

¿Fue útil?

Solución

You'll probably be happier working completely in Cocoa for this problem, using NSDate instead of CFAbsoluteTime.

Applicable document is the Date and Time Programming Guide.

The general approach you'll need is to work within an NSCalendar, which encapsulates all of the information about days per month, months per year, leap years, DST changes, all for a particular time zone. Convert your NSDate instances to NSDateComponent and you'll be able to extract the day and month numbers, then bucketize from there.

Remember that the function you write (return a count when given a date) will implicitly or explicitly have to handle NSCalendar and NSTimeZone values. The answer will vary by year (is it a leap year? does the interval include a leap day) and locale/date (are we observing Daylight Saving Time in this location right now?).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top