"Year 2038 _problem" in Google Calendar API (Android application)

1.2k views Asked by At

I'm building an Android application and the application enables the user to insert events to Google Calendar and external calendar (like Exchange account).

The problem is that if the user wants to add an event after 2038, it creates the event in the past (for example - January 2038 becomes December 1901, and 4th of July 2038 becomes May 28, 1902). I did some research and figured that the problem is "Year 2038 problem".

The Year 2038 problem is an issue for computing and data storage situations in which time values are stored or calculated as a signed 32-bit integer, and this number is interpreted as the number of seconds since 00:00:00 UTC on 1 January 1970. Such implementations cannot encode times after 03:14:07 UTC on 19 January 2038.

The latest time that can be represented in Unix's signed 32-bit integer time format is 03:14:07 UTC on Tuesday, 19 January 2038 (2,147,483,647 seconds after 1 January 1970). Times beyond that will "wrap around" and be stored internally as a negative number, which these systems will interpret as having occurred on 13 December 1901 rather than 19 January 2038. This is caused by integer overflow.

It seems that my Java code works fine and the milliseconds I get are OK, but when I send the values to Google API insert function - I thinks it doesn't know how to deal with it and then it inserts the event at the wrong date (year 1901 and above). Is there any way to handle it?

This is my code:

private void InsertEvent(MyEvent myEvent) {
    Uri EVENTS_URI = Uri.parse(getCalendarUriBase() + "events"); 

    ContentValues eventValues = new ContentValues();
    eventValues.put("eventTimezone",  TimeZone.getDefault().getID());
    eventValues.put("calendar_id", myEvent.calId);
    eventValues.put("title",myEvent.title);
    eventValues.put("allDay", 1);

    long dateStart = myEvent.startDate.getTime();   // returns milliseconds - 2160248400000 for date 06/16/2038
    eventValues.put("dtstart", dateStart );   

    long dateEnd = myEvent.endDate.getTime();
    eventValues.put("dtend", dateEnd  );

    // At this point, in debug mode, I can see that the millisecond of dtstart and dtend are OK. 
    Uri u1 = contentResolver.insert(EVENTS_URI, eventValues );  // API's function
}

This is Google documentation about inserting an event: http://developer.android.com/guide/topics/providers/calendar-provider.html#add-event

1

There are 1 answers

1
nbroeking On

I can't fully help without all of your code but I have ran into a similar issue before. I would check that you are not casting anything to an int along your data pipeline.

1.) Check MyEvent getTime() doesnt return an int

2.) check that MyEvent SetTime does not set it as an int

3.) check no other int casts exist.

If you are casting to an int, Implict or Explict, then java will turn your number into the negative representation of your number.