Skip to content

Commit

Permalink
Merge pull request #284 from effectivemade/backendApp/hotfix/calendar…
Browse files Browse the repository at this point in the history
…-date-conversion

backend: fix date conversion
  • Loading branch information
Red1tum authored Jul 24, 2024
2 parents 9285334 + cb2f481 commit 8f9b7d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package office.effective.features.booking.converters

import com.google.api.client.util.DateTime
import office.effective.common.constants.BookingConstants

/**
* Converts local time to Google [DateTime] in GMT.
* Converts local time to [DateTime].
*
* Use it for all requests to Google Calendar.
*/
fun Long.toGoogleDateTime(): DateTime {
return DateTime(this - BookingConstants.DEFAULT_TIMEZONE_OFFSET_MILLIS)
fun Long.toDateTime(): DateTime {
return DateTime(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import office.effective.features.workspace.repository.WorkspaceRepository
import office.effective.model.RecurrenceModel.Companion.toRecurrence
import org.slf4j.LoggerFactory
import java.time.Instant
import java.time.ZoneId
import java.time.ZonedDateTime
import java.util.*
import kotlin.collections.List as List

Expand Down Expand Up @@ -349,9 +351,12 @@ class GoogleCalendarConverter(
* @return [Instant]
*/
private fun toLocalInstant(googleDateTime: EventDateTime?) : Instant {
val gmtEpoch: Long? = googleDateTime?.dateTime?.value
val localEpoch = gmtEpoch?.let { it + BookingConstants.DEFAULT_TIMEZONE_OFFSET_MILLIS } ?: 0
return Instant.ofEpochMilli(localEpoch)
if (googleDateTime == null) return Instant.ofEpochMilli(0)

val instant = Instant.ofEpochMilli(googleDateTime.dateTime.value)
val correctedTime = ZonedDateTime.ofInstant(instant, ZoneId.of(googleDateTime.timeZone))
.withZoneSameInstant(ZoneId.of(BookingConstants.DEFAULT_TIMEZONE_ID))
return correctedTime.toInstant()
}

/**
Expand All @@ -361,7 +366,7 @@ class GoogleCalendarConverter(
*/
private fun Instant.toGoogleEventDateTime() : EventDateTime {
val googleEventDateTime = EventDateTime()
googleEventDateTime.dateTime = this.toEpochMilli().toGoogleDateTime()
googleEventDateTime.dateTime = this.toEpochMilli().toDateTime()
googleEventDateTime.timeZone = BookingConstants.DEFAULT_TIMEZONE_ID
return googleEventDateTime
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import office.effective.common.exception.InstanceNotFoundException
import office.effective.common.exception.MissingIdException
import office.effective.common.exception.WorkspaceUnavailableException
import office.effective.features.booking.converters.GoogleCalendarConverter
import office.effective.features.booking.converters.toGoogleDateTime
import office.effective.features.booking.converters.toDateTime
import office.effective.features.calendar.repository.CalendarIdsRepository
import office.effective.features.user.repository.UserEntity
import office.effective.features.user.repository.UserRepository
Expand Down Expand Up @@ -137,7 +137,7 @@ class BookingMeetingRepository(
/**
* Request template containing all required parameters
*
* @param timeMin lover bound for filtering bookings by start time.
* @param timeMin lower bound for filtering bookings by start time.
* Old Google calendar events may not appear correctly in the system and cause unexpected exceptions.
* Should be a time in the default timezone.
* @param timeMax upper bound (exclusive) for an event's start time to filter by.
Expand All @@ -154,8 +154,8 @@ class BookingMeetingRepository(
): Calendar.Events.List {
return calendarEvents.list(calendarId)
.setSingleEvents(singleEvents)
.setTimeMin(timeMin.toGoogleDateTime())
.setTimeMax(timeMax?.toGoogleDateTime())
.setTimeMin(timeMin.toDateTime())
.setTimeMax(timeMax?.toDateTime())
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package office.effective.features.booking.repository

import com.google.api.client.googleapis.json.GoogleJsonResponseException
import com.google.api.client.util.DateTime
import com.google.api.services.calendar.Calendar
import com.google.api.services.calendar.model.Event
import office.effective.common.constants.BookingConstants
import office.effective.common.exception.InstanceNotFoundException
import office.effective.common.exception.MissingIdException
import office.effective.common.exception.WorkspaceUnavailableException
import office.effective.features.booking.converters.GoogleCalendarConverter
import office.effective.features.booking.converters.toGoogleDateTime
import office.effective.features.booking.converters.toDateTime
import office.effective.model.Booking
import org.slf4j.LoggerFactory
import java.time.Instant
Expand Down Expand Up @@ -105,8 +104,8 @@ class BookingRegularRepository(
): Calendar.Events.List {
return calendarEvents.list(calendarId)
.setSingleEvents(singleEvents)
.setTimeMin(timeMin.toGoogleDateTime())
.setTimeMax(timeMax?.toGoogleDateTime())
.setTimeMin(timeMin.toDateTime())
.setTimeMax(timeMax?.toDateTime())
}

/**
Expand Down

0 comments on commit 8f9b7d4

Please sign in to comment.