All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.time4j.ZonalQuery Maven / Gradle / Ivy

There is a newer version: 4.38
Show newest version
/*
 * -----------------------------------------------------------------------
 * Copyright © 2013-2014 Meno Hochschild, 
 * -----------------------------------------------------------------------
 * This file (ZonalQuery.java) is part of project Time4J.
 *
 * Time4J is free software: You can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Time4J is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Time4J. If not, see .
 * -----------------------------------------------------------------------
 */

package net.time4j;

import net.time4j.engine.ChronoElement;
import net.time4j.engine.ChronoFunction;
import net.time4j.tz.Timezone;
import net.time4j.tz.ZonalOffset;


class ZonalQuery
    implements ChronoFunction {

    //~ Instanzvariablen --------------------------------------------------

    private final ChronoElement element;
    private final Timezone tz;
    private final ZonalOffset offset;

    //~ Konstruktoren -----------------------------------------------------

    ZonalQuery(
        ChronoElement element,
        Timezone tz
    ) {
        super();

        if (tz == null) {
            throw new NullPointerException("Missing timezone.");
        }

        this.element = element;
        this.tz = tz;
        this.offset = null;

    }

    ZonalQuery(
        ChronoElement element,
        ZonalOffset offset
    ) {
        super();

        if (offset == null) {
            throw new NullPointerException("Missing timezone offset.");
        }

        this.element = element;
        this.tz = null;
        this.offset = offset;

    }

    //~ Methoden ----------------------------------------------------------

    @Override
    public V apply(Moment context) {

        ZonalOffset shift = (
            (this.offset == null)
            ? this.tz.getOffset(context)
            : this.offset);

        if (
            (this.element == PlainTime.SECOND_OF_MINUTE)
            && context.isLeapSecond()
            && (shift.getFractionalAmount() == 0)
            && ((shift.getAbsoluteSeconds() % 60) == 0)
        ) {
            return this.element.getType().cast(Integer.valueOf(60));
        }

        return PlainTimestamp.from(context, shift).get(this.element);

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy