MCPcopy Create free account
hub / github.com/MenoData/Time4J / withValue

Method withValue

base/src/main/java/net/time4j/Moment.java:3293–3339  ·  view source on GitHub ↗
(
            Moment context,
            TimeUnit value,
            boolean lenient
        )

Source from the content-addressed store, hash-verified

3291 }
3292
3293 @Override
3294 public Moment withValue(
3295 Moment context,
3296 TimeUnit value,
3297 boolean lenient
3298 ) {
3299
3300 if (value == null) {
3301 throw new IllegalArgumentException("Missing precision.");
3302 }
3303
3304 Moment result;
3305
3306 switch (value) {
3307 case DAYS:
3308 long secsD = Math.floorDiv(context.posixTime, 86400) * 86400;
3309 return Moment.of(secsD, TimeScale.POSIX);
3310 case HOURS:
3311 long secsH = Math.floorDiv(context.posixTime, 3600) * 3600;
3312 return Moment.of(secsH, TimeScale.POSIX);
3313 case MINUTES:
3314 long secsM = Math.floorDiv(context.posixTime, 60) * 60;
3315 return Moment.of(secsM, TimeScale.POSIX);
3316 case SECONDS:
3317 result = Moment.of(context.posixTime, 0, TimeScale.POSIX);
3318 break;
3319 case MILLISECONDS:
3320 int f3 = (context.getNanosecond() / MIO) * MIO;
3321 result = Moment.of(context.posixTime, f3, TimeScale.POSIX);
3322 break;
3323 case MICROSECONDS:
3324 int f6 = (context.getNanosecond() / 1000) * 1000;
3325 result = Moment.of(context.posixTime, f6, TimeScale.POSIX);
3326 break;
3327 case NANOSECONDS:
3328 return context;
3329 default:
3330 throw new UnsupportedOperationException(value.name());
3331 }
3332
3333 if (context.isLeapSecond() && LeapSeconds.getInstance().isEnabled()) {
3334 return result.plus(1, SI.SECONDS);
3335 } else {
3336 return result;
3337 }
3338
3339 }
3340
3341 @Override
3342 public boolean isValid(

Callers

nothing calls this directly

Calls 8

ofMethod · 0.95
getInstanceMethod · 0.95
plusMethod · 0.95
floorDivMethod · 0.80
getNanosecondMethod · 0.65
nameMethod · 0.65
isLeapSecondMethod · 0.65
isEnabledMethod · 0.45

Tested by

no test coverage detected