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

Class PrecisionRule

base/src/main/java/net/time4j/Moment.java:3259–3379  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3257 }
3258
3259 private static class PrecisionRule
3260 implements ElementRule<Moment, TimeUnit> {
3261
3262 //~ Methoden ------------------------------------------------------
3263
3264 @Override
3265 public TimeUnit getValue(Moment context) {
3266
3267 int f = context.getNanosecond();
3268
3269 if (f != 0) {
3270 if ((f % MIO) == 0) {
3271 return TimeUnit.MILLISECONDS;
3272 } else if ((f % 1000) == 0) {
3273 return TimeUnit.MICROSECONDS;
3274 } else {
3275 return TimeUnit.NANOSECONDS;
3276 }
3277 }
3278
3279 long secs = context.posixTime;
3280
3281 if (Math.floorMod(secs, 86400) == 0) {
3282 return TimeUnit.DAYS;
3283 } else if (Math.floorMod(secs, 3600) == 0) {
3284 return TimeUnit.HOURS;
3285 } else if (Math.floorMod(secs, 60) == 0) {
3286 return TimeUnit.MINUTES;
3287 } else {
3288 return TimeUnit.SECONDS;
3289 }
3290
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:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected