Search in sources :

Example 1 with OffsetDateTime

use of org.threeten.bp.OffsetDateTime in project selenium_java by sergueik.

the class OffsetDateTimeDeserializer method deserialize.

@Override
public OffsetDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    try {
        // parsed string
        String timestamp = jp.getText();
        // parsing the timestamp to a Date
        DateFormat df = new java.text.SimpleDateFormat("EEE MMM dd kk:mm:ss zzzz yyyy", Locale.ENGLISH);
        Date date = df.parse(timestamp);
        // extracting the properties needed to build a OffsetDateTime object
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        // month is zero-based
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hours = c.get(Calendar.HOUR);
        int mins = c.get(Calendar.MINUTE);
        int sec = c.get(Calendar.SECOND);
        int nanos = 0;
        ZoneOffset zoneOffset = OffsetDateTime.now().getOffset();
        // building the OffsetDateTim object
        OffsetDateTime x = OffsetDateTime.of(year, month, day, hours, mins, sec, nanos, zoneOffset);
        return x;
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : OffsetDateTime(org.threeten.bp.OffsetDateTime) DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) ParseException(java.text.ParseException) Date(java.util.Date) ZoneOffset(org.threeten.bp.ZoneOffset)

Example 2 with OffsetDateTime

use of org.threeten.bp.OffsetDateTime in project SeriesGuide by UweTrottmann.

the class TraktTask method doCheckInAction.

private TraktResponse doCheckInAction() {
    TraktV2 trakt = SgApp.getServicesComponent(context).trakt();
    try {
        retrofit2.Response response;
        String message = args.getString(InitBundle.MESSAGE);
        switch(action) {
            case CHECKIN_EPISODE:
                {
                    // Check in using show Trakt ID
                    // and season and episode number (likely most reliable).
                    long episodeId = args.getLong(InitBundle.EPISODE_ID);
                    SgRoomDatabase database = SgRoomDatabase.getInstance(context);
                    SgEpisode2Numbers episode = database.sgEpisode2Helper().getEpisodeNumbers(episodeId);
                    if (episode == null) {
                        Timber.e("Failed to get episode %d", episodeId);
                        return buildErrorResponse();
                    }
                    Integer showTraktId = ShowTools.getShowTraktId(context, episode.getShowId());
                    if (showTraktId == null) {
                        Timber.e("Failed to get show %d", episode.getShowId());
                        return buildErrorResponse();
                    }
                    SyncEpisode traktEpisode = new SyncEpisode().season(episode.getSeason()).number(episode.getEpisodenumber());
                    Show traktShow = new Show();
                    traktShow.ids = ShowIds.trakt(showTraktId);
                    EpisodeCheckin checkin = new EpisodeCheckin.Builder(traktEpisode, APP_VERSION, null).show(traktShow).message(message).build();
                    response = trakt.checkin().checkin(checkin).execute();
                    break;
                }
            case CHECKIN_MOVIE:
                {
                    int movieTmdbId = args.getInt(InitBundle.MOVIE_TMDB_ID);
                    MovieCheckin checkin = new MovieCheckin.Builder(new SyncMovie().id(MovieIds.tmdb(movieTmdbId)), APP_VERSION, null).message(message).build();
                    response = trakt.checkin().checkin(checkin).execute();
                    break;
                }
            default:
                throw new IllegalArgumentException("check-in action unknown.");
        }
        if (response.isSuccessful()) {
            return new TraktResponse(true, context.getString(R.string.checkin_success_trakt, args.getString(InitBundle.TITLE)));
        } else {
            // check if the user wants to check-in, but there is already a check-in in progress
            CheckinError checkinError = trakt.checkForCheckinError(response);
            if (checkinError != null) {
                OffsetDateTime expiresAt = checkinError.expires_at;
                int waitTimeMin = expiresAt == null ? -1 : (int) ((expiresAt.toInstant().toEpochMilli() - System.currentTimeMillis()) / 1000);
                return new CheckinBlockedResponse(waitTimeMin);
            } else // check if item does not exist on trakt (yet)
            if (response.code() == 404) {
                return new TraktResponse(false, context.getString(R.string.trakt_error_not_exists));
            } else if (SgTrakt.isUnauthorized(context, response)) {
                return new TraktResponse(false, context.getString(R.string.trakt_error_credentials));
            } else {
                Errors.logAndReport("check-in", response);
            }
        }
    } catch (Exception e) {
        Errors.logAndReport("check-in", e);
    }
    // return generic failure message
    return buildErrorResponse();
}
Also used : CheckinError(com.uwetrottmann.trakt5.entities.CheckinError) SyncEpisode(com.uwetrottmann.trakt5.entities.SyncEpisode) SgRoomDatabase(com.battlelancer.seriesguide.provider.SgRoomDatabase) EpisodeCheckin(com.uwetrottmann.trakt5.entities.EpisodeCheckin) SuppressLint(android.annotation.SuppressLint) MovieCheckin(com.uwetrottmann.trakt5.entities.MovieCheckin) TraktV2(com.uwetrottmann.trakt5.TraktV2) OffsetDateTime(org.threeten.bp.OffsetDateTime) SgEpisode2Numbers(com.battlelancer.seriesguide.provider.SgEpisode2Numbers) SyncMovie(com.uwetrottmann.trakt5.entities.SyncMovie) Show(com.uwetrottmann.trakt5.entities.Show)

Aggregations

OffsetDateTime (org.threeten.bp.OffsetDateTime)2 SuppressLint (android.annotation.SuppressLint)1 SgEpisode2Numbers (com.battlelancer.seriesguide.provider.SgEpisode2Numbers)1 SgRoomDatabase (com.battlelancer.seriesguide.provider.SgRoomDatabase)1 TraktV2 (com.uwetrottmann.trakt5.TraktV2)1 CheckinError (com.uwetrottmann.trakt5.entities.CheckinError)1 EpisodeCheckin (com.uwetrottmann.trakt5.entities.EpisodeCheckin)1 MovieCheckin (com.uwetrottmann.trakt5.entities.MovieCheckin)1 Show (com.uwetrottmann.trakt5.entities.Show)1 SyncEpisode (com.uwetrottmann.trakt5.entities.SyncEpisode)1 SyncMovie (com.uwetrottmann.trakt5.entities.SyncMovie)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 ZoneOffset (org.threeten.bp.ZoneOffset)1