7

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

10
  • What exactly happens? Commented Jun 16, 2015 at 21:43
  • Wait for the specification to update and use 64-bit integer? Year 2038 isn't exactly relevant in calendar applications yet. Commented Jun 16, 2015 at 21:44
  • @SLaks - Times beyond January 2038 is stored internally as a negative number, which creates the event on December 1901 rather than January 2038. Commented Jun 16, 2015 at 21:49
  • Java does not have Year 2038 problem neither does the CalendarContract so the problem is probably in the specific backend. @PatrickRoberts Java stores time as milliseconds in 64-bit long. Commented Jun 16, 2015 at 22:04
  • Do you mean in the code? It all seems OK on debug, only the Insert function inserts the wrong date Commented Jun 16, 2015 at 22:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

getTime() returns the correct (positive) number. In fact - in debug mode I can see that all the values are OK a moment before I send them to the "insert" function, and the insert function is not mine...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.