snaggs
snaggs

Reputation: 5713

How to get the real time of recurring events?

I use following method to fetch all events for calendars:

public Cursor getEventsCursor(Context context) {        
    long curr = System.currentTimeMillis();
    long begin = curr - DateUtils.DAY_IN_MILLIS * 30; // - 30 days
    long end = curr + DateUtils.DAY_IN_MILLIS * 30;   // + 30 days

    Cursor cur = null;
    ContentResolver cr = context.getContentResolver();

    String selection = Events.VISIBLE + "= ?";
    String[] selectionArgs = new String[] {"1"};
    String order = Events.DTSTART + " DESC";

    Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
    ContentUris.appendId(builder, begin);
    ContentUris.appendId(builder, end);

    return cr.query(builder.build(), EVENT_PROJECTION, 
            selection, selectionArgs, order);
} 

It works, but I get the initial time for all recurring events!

Logs

Feeder milestone; eventId: 12;  date=2012-12-06 11:00:00  // initial time
Feeder milestone; eventId: 23;  date=2012-12-06 11:00:00
Feeder milestone; eventId: 178; date=2012-12-06 11:00:00
Feeder milestone; eventId: 180; date=2012-12-06 11:00:00

How to get the real times of recurring events?

Example: if recurring event Feeder milestone is 2-weekly event, I expect:

Feeder milestone; eventId: 12;  date=2014-08-03 11:00:00
Feeder milestone; eventId: 23;  date=2014-08-27 11:00:00
Feeder milestone; eventId: 178; date=2014-09-11 11:00:00
Feeder milestone; eventId: 180; date=2014-09-25 11:00:00

Upvotes: 4

Views: 1196

Answers (1)

Andrew T.
Andrew T.

Reputation: 4707

From Calendar Provider documentation, there are 3 things related to Events, and one of them is Instances.

Events: This table holds the event-specific information. Each row in this table has the information for a single event—for example, event title, location, start time, end time, and so on. The event can occur one-time or can recur multiple times. Attendees, reminders, and extended properties are stored in separate tables. They each have an EVENT_ID that references the _ID in the Events table.

Instances: This table holds the start and end time for each occurrence of an event. Each row in this table represents a single event occurrence. For one-time events there is a 1:1 mapping of instances to events. For recurring events, multiple rows are automatically generated that correspond to multiple occurrences of that event.

(emphasis mine)

In case of recurring event, Events.DTSTART refers to the starting time of whole events.

On the other hand, Instances.BEGIN and Instances.END refer to the starting and ending time of each occurrence of an event.

In general case, always use Instances.BEGIN to get the starting time of all events (whether recurring or not) conveniently.

Upvotes: 8

Related Questions