0

I have access to TAI time with seconds and nano seconds - let me just called this T.

I want to now create a java Instant class corresponding to this value T.

I've looked for the appropriate constructors to do this but no luck.

  • 2
    Define your terms. By "TAI" do you mean [International Atomic Time](https://en.m.wikipedia.org/wiki/International_Atomic_Time) that is 37 seconds ahead of UTC? – Basil Bourque Jun 09 '17 at 20:18
  • "TAI time with seconds and nano seconds " please define the seconds, measured since when? SI-seconds??? Otherwise all existing answers here are guesswork without knowing your definition. – Meno Hochschild Jun 22 '17 at 13:04
  • TAI-time is usually represented in lexical space (like "2017-06-22T14:23:59") where no leap seconds occur. The representation as count of seconds elapsed since XXX (1970, 1958, ...) is somehow unusual, not standardized and tricky in detail, see also the excerpt of my library [documentation example of time scales](http://time4j.net/javadoc-en/net/time4j/Moment.html). Important: During a leap second, the conversion to `Instant` will always fail (no support in Java-8). – Meno Hochschild Jun 22 '17 at 13:57

2 Answers2

5

I'd recommend looking at, or using, the code in the ThreeTen-Extra library that explicity handles TAI and UTC. Here is the Javadoc.

TaiInstant tai = TaiInstant.ofTaiSeconds(taiSecs, taiNanos);
Instant instant = tai.toInstant();

The second method applies UTC-SLS conversion.

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
  • Unfortunately, this conversion seems to be broken for all times before 1972 (where `Instant` is defined as UT and not UTC-SLS if we get the official spec for serious). – Meno Hochschild Aug 16 '17 at 14:48
-2

If you have access to the time elements separately you could do something like this:

long millisecondsTime = 1262347200000l;
long nanoseconds = 10202;
Instant taiInstance = Instant.ofEpochMilli(millisecondsTime);
taiInstance = taiInstance.plus(Duration.ofNanos(nanoseconds));
MRyken
  • 22
  • 3
  • thanks a bunch ! But will this also adjust the nano seconds ? I have TAI time to nano second precision and would like to set the seconds and nano seconds correctly - sorry if this is a stupid question the documentation just confuses me – Farrukh Azfar Jun 09 '17 at 19:12
  • Do you by chance have access to all the different date/time information separately? I've updated the answer to reflect a different option. – MRyken Jun 09 '17 at 19:23
  • There is also quite a bit of information on nanoseconds provided within this question that may be helpful but nothing in it answers exactly what you are trying to accomplish, which I'm hoping my above answer did: https://stackoverflow.com/questions/20689055/java-8-instant-now-with-nanosecond-resolution – MRyken Jun 09 '17 at 19:46
  • 1
    Calling `taiInstance.plus(...)` and not using the result makes this a misleading answer at the moment. – Jon Skeet Jun 09 '17 at 19:50
  • 1
    Thank you @JonSkeet I've updated the answer accordingly. – MRyken Jun 09 '17 at 19:52
  • 4
    This answer doesn't do any conversion from TAI to UTC. – Daniel Pryden Jun 09 '17 at 20:41
  • @MRyken thanks again ! However I don't see where Duration comes from - my compiler doesn't recognize it - is a declaration neccesary an import ? – Farrukh Azfar Jun 09 '17 at 22:12
  • The import for the Duration class is: import java.time.Duration;. You also need to be using java 8. – MRyken Jun 10 '17 at 14:51