Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 963
TamarG Avatar asked Jun 16 '15 21:06

TamarG


People also ask

How do you solve 2038 problems?

There is no universal solution for the Year 2038 problem. For example, in the C language, any change to the definition of the time_t data type would result in code-compatibility problems in any application in which date and time representations are dependent on the nature of the signed 32-bit time_t integer.

What will happen on 19 January 2038?

The maximum value of time before it rolls over to a negative (and invalid) value is 2,147,483,647, which translates into January 19, 2038. On this date, any C programs that use the standard time library will start to have problems with date calculations.

What will happen to computers in 2038?

The 2038 problem refers to the time encoding error that will occur in the year 2038 in 32-bit systems. This may cause havoc in machines and services that use time to encode instructions and licenses. The effects will primarily be seen in devices that are not connected to the internet.

What happens when Unix time runs out?

The end of time The most imminent overflow date is the 32-bit signed integer-based systems', scheduled for 19 January 2038, at 03:14:07 UTC. One second later, computers will fall back to 13 December 1901, at 20:45:52 UTC. This behavior is due to an integer overflow occurring at the time of adding the next second.


1 Answers

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.

like image 190
nbroeking Avatar answered Oct 20 '22 22:10

nbroeking