Exemplo n.º 1
0
        public static TimeValue Truncate(TemporalUnit unit, TemporalValue input, MapValue fields, System.Func <ZoneId> defaultZone)
        {
            OffsetTime time        = input.getTimePart(defaultZone);
            OffsetTime truncatedOT = AssertValidUnit(() => time.truncatedTo(unit));

            if (fields.Size() == 0)
            {
                return(time(truncatedOT));
            }
            else
            {
                // Timezone needs some special handling, since the builder will shift keeping the instant instead of the local time
                AnyValue timezone = fields.Get("timezone");
                if (timezone != NO_VALUE)
                {
                    ZonedDateTime currentDT     = AssertValidArgument(() => ZonedDateTime.ofInstant(Instant.now(), TimezoneOf(timezone)));
                    ZoneOffset    currentOffset = currentDT.Offset;
                    truncatedOT = truncatedOT.withOffsetSameLocal(currentOffset);
                }

                return(UpdateFieldMapWithConflictingSubseconds(fields, unit, truncatedOT, (mapValue, offsetTime) =>
                {
                    if (mapValue.size() == 0)
                    {
                        return time(offsetTime);
                    }
                    else
                    {
                        return Build(mapValue.updatedWith("time", time(offsetTime)), defaultZone);
                    }
                }));
            }
        }
Exemplo n.º 2
0
            public override TimeValue buildInternal()
            {
                bool       selectingTime = fields.containsKey(TemporalFields.Time);
                bool       selectingTimeZone;
                OffsetTime result;

                if (selectingTime)
                {
                    AnyValue time = fields.get(TemporalFields.Time);
                    if (!(time is TemporalValue))
                    {
                        throw new InvalidValuesArgumentException(string.Format("Cannot construct time from: {0}", time));
                    }
                    TemporalValue t = ( TemporalValue )time;
                    result            = t.getTimePart(_defaultZone);
                    selectingTimeZone = t.supportsTimeZone();
                }
                else
                {
                    ZoneId timezone = timezone();
                    if (!(timezone is ZoneOffset))
                    {
                        timezone = AssertValidArgument(() => ZonedDateTime.ofInstant(Instant.now(), timezone())).Offset;
                    }

                    result            = DefaultTime(timezone);
                    selectingTimeZone = false;
                }

                result = assignAllFields(result);
                if (timezone != null)
                {
                    ZoneOffset currentOffset = AssertValidArgument(() => ZonedDateTime.ofInstant(Instant.now(), timezone())).Offset;
                    if (selectingTime && selectingTimeZone)
                    {
                        result = result.withOffsetSameInstant(currentOffset);
                    }
                    else
                    {
                        result = result.withOffsetSameLocal(currentOffset);
                    }
                }
                return(Time(result));
            }
Exemplo n.º 3
0
            protected internal override TimeValue selectTime(AnyValue temporal)
            {
                if (!(temporal is TemporalValue))
                {
                    throw new InvalidValuesArgumentException(string.Format("Cannot construct time from: {0}", temporal));
                }
                if (temporal is TimeValue && timezone == null)
                {
                    return(( TimeValue )temporal);
                }

                TemporalValue v    = ( TemporalValue )temporal;
                OffsetTime    time = v.getTimePart(_defaultZone);

                if (timezone != null)
                {
                    ZoneOffset currentOffset = AssertValidArgument(() => ZonedDateTime.ofInstant(Instant.now(), timezone())).Offset;
                    time = time.withOffsetSameInstant(currentOffset);
                }
                return(time(time));
            }
Exemplo n.º 4
0
        internal override OffsetTime GetTimePart(System.Func <ZoneId> defaultZone)
        {
            ZoneOffset currentOffset = AssertValidArgument(() => ZonedDateTime.ofInstant(Instant.now(), defaultZone())).Offset;

            return(OffsetTime.of(_value.toLocalTime(), currentOffset));
        }
Exemplo n.º 5
0
            public override DateTimeValue buildInternal()
            {
                bool          selectingDate     = fields.containsKey(TemporalFields.Date);
                bool          selectingTime     = fields.containsKey(TemporalFields.Time);
                bool          selectingDateTime = fields.containsKey(TemporalFields.Datetime);
                bool          selectingEpoch    = fields.containsKey(TemporalFields.EpochSeconds) || fields.containsKey(TemporalFields.EpochMillis);
                bool          selectingTimeZone;
                ZonedDateTime result;

                if (selectingDateTime)
                {
                    AnyValue dtField = fields.get(TemporalFields.Datetime);
                    if (!(dtField is TemporalValue))
                    {
                        throw new InvalidValuesArgumentException(string.Format("Cannot construct date time from: {0}", dtField));
                    }
                    TemporalValue dt       = ( TemporalValue )dtField;
                    LocalTime     timePart = dt.getTimePart(_defaultZone).toLocalTime();
                    ZoneId        zoneId   = dt.getZoneId(_defaultZone);
                    result            = ZonedDateTime.of(dt.DatePart, timePart, zoneId);
                    selectingTimeZone = dt.supportsTimeZone();
                }
                else if (selectingEpoch)
                {
                    if (fields.containsKey(TemporalFields.EpochSeconds))
                    {
                        AnyValue epochField = fields.get(TemporalFields.EpochSeconds);
                        if (!(epochField is IntegralValue))
                        {
                            throw new InvalidValuesArgumentException(string.Format("Cannot construct date time from: {0}", epochField));
                        }
                        IntegralValue epochSeconds = ( IntegralValue )epochField;
                        result = AssertValidArgument(() => ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochSeconds.LongValue() * 1000), timezone()));
                    }
                    else
                    {
                        AnyValue epochField = fields.get(TemporalFields.EpochMillis);
                        if (!(epochField is IntegralValue))
                        {
                            throw new InvalidValuesArgumentException(string.Format("Cannot construct date time from: {0}", epochField));
                        }
                        IntegralValue epochMillis = ( IntegralValue )epochField;
                        result = AssertValidArgument(() => ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis.LongValue()), timezone()));
                    }
                    selectingTimeZone = false;
                }
                else if (selectingTime || selectingDate)
                {
                    LocalTime time;
                    ZoneId    zoneId;
                    if (selectingTime)
                    {
                        AnyValue timeField = fields.get(TemporalFields.Time);
                        if (!(timeField is TemporalValue))
                        {
                            throw new InvalidValuesArgumentException(string.Format("Cannot construct time from: {0}", timeField));
                        }
                        TemporalValue t = ( TemporalValue )timeField;
                        time              = t.getTimePart(_defaultZone).toLocalTime();
                        zoneId            = t.getZoneId(_defaultZone);
                        selectingTimeZone = t.supportsTimeZone();
                    }
                    else
                    {
                        time              = LocalTimeValue.DefaultLocalTime;
                        zoneId            = timezone();
                        selectingTimeZone = false;
                    }
                    LocalDate date;
                    if (selectingDate)
                    {
                        AnyValue dateField = fields.get(TemporalFields.Date);
                        if (!(dateField is TemporalValue))
                        {
                            throw new InvalidValuesArgumentException(string.Format("Cannot construct date from: {0}", dateField));
                        }
                        TemporalValue t = ( TemporalValue )dateField;
                        date = t.DatePart;
                    }
                    else
                    {
                        date = DateValue.DefaultCalenderDate;
                    }
                    result = ZonedDateTime.of(date, time, zoneId);
                }
                else
                {
                    result            = defaultZonedDateTime;
                    selectingTimeZone = false;
                }

                if (fields.containsKey(TemporalFields.Week) && !selectingDate && !selectingDateTime && !selectingEpoch)
                {
                    // Be sure to be in the start of the week based year (which can be later than 1st Jan)
                    result = result.with(IsoFields.WEEK_BASED_YEAR, safeCastIntegral(TemporalFields.Year.name(), fields.get(TemporalFields.Year), TemporalFields.Year.defaultValue)).with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 1).with(ChronoField.DAY_OF_WEEK, 1);
                }

                result = assignAllFields(result);
                if (timezone != null)
                {
                    if (((selectingTime || selectingDateTime) && selectingTimeZone) || selectingEpoch)
                    {
                        try
                        {
                            result = result.withZoneSameInstant(timezone());
                        }
                        catch (DateTimeParseException e)
                        {
                            throw new InvalidValuesArgumentException(e.Message, e);
                        }
                    }
                    else
                    {
                        result = result.withZoneSameLocal(timezone());
                    }
                }
                return(Datetime(result));
            }