View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.base;
33  
34  import com.google.common.annotations.Beta;
35  import java.time.Instant;
36  import java.util.concurrent.TimeUnit;
37  
38  /**
39   * A Utility class that allows for quick conversion between nanotime and epoch relative time.
40   * @author Zoltan Farkas
41   */
42  @Beta
43  public final class Timing {
44  
45    public static final long MAX_MS_SPAN = TimeUnit.NANOSECONDS.toMillis(Long.MAX_VALUE);
46  
47    static {
48      // load classes to improve timing.
49      TimeSource.nanoTime();
50      System.currentTimeMillis();
51    }
52  
53    private static final Timing LATEST_TIMING = new Timing();
54  
55    private final long nanoTimeRef;
56    private final long currentTimeMillisRef;
57  
58    private Timing() {
59      nanoTimeRef = TimeSource.nanoTime();
60      currentTimeMillisRef = System.currentTimeMillis();
61    }
62  
63    public long fromNanoTimeToEpochMillis(final long nanoTime) {
64      return currentTimeMillisRef + TimeUnit.NANOSECONDS.toMillis(nanoTime - nanoTimeRef);
65    }
66  
67    public Instant fromNanoTimeToInstant(final long nanoTime) {
68      long relNanos = nanoTime - nanoTimeRef;
69      return Instant.ofEpochSecond(currentTimeMillisRef / 1000
70              + relNanos / 1000000000, (currentTimeMillisRef % 1000) * 1000000 + relNanos %  1000000000);
71    }
72  
73    public long fromEpochMillisToNanoTime(final long epochTimeMillis) {
74      long msSinceLast = epochTimeMillis - currentTimeMillisRef;
75      if (Math.abs(msSinceLast) > MAX_MS_SPAN) {
76        return TimeSource.nanoTime() + Long.MAX_VALUE;
77      }
78      return nanoTimeRef + TimeUnit.MILLISECONDS.toNanos(msSinceLast);
79    }
80  
81    public static Timing getCurrentTiming() {
82      return LATEST_TIMING;
83    }
84  
85    @Override
86    public String toString() {
87      return "Timing{" + "nanoTimeRef=" + nanoTimeRef + ", currentTimeMillisRef=" + currentTimeMillisRef + '}';
88    }
89  
90  
91  }