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

1

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

Tallula answered 16/6, 2015 at 21:40 Comment(10)
What exactly happens?Levis
Wait for the specification to update and use 64-bit integer? Year 2038 isn't exactly relevant in calendar applications yet.Stetson
@Levis - Times beyond January 2038 is stored internally as a negative number, which creates the event on December 1901 rather than January 2038.Tallula
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.Coverlet
Do you mean in the code? It all seems OK on debug, only the Insert function inserts the wrong dateTallula
Well... who forces you to use the Google Calendar API?Symptom
Is there any other option to insert events to Google Calendars / external calendars with Android application?Tallula
Can't you use a database? Here's a great example: codeyourenthusiasm.wordpress.com/category/androidSymptom
Database won't help my goal.Tallula
Hexadecimal>64 bit> 32 bit Google needs to update their stuffZephyr
F
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.

Feigned answered 17/6, 2015 at 1:7 Comment(1)
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...Tallula

© 2022 - 2024 — McMap. All rights reserved.