how to convert nanoseconds of uptime into a readable date time format

  • Last Update :
  • Techknowledgy :

tl;dr

Duration.ofNanos(45_900_482_044_637 L)

Note the L appended to numeric literal for a long. Also, underscores make lengthy numbers easier for humans to decipher.

long input = 45_900_482_044_637 L;

Let's convert that number to a Duration object.

Duration duration = Duration.ofNanos(input);

To get the uptime in human-readable format in Python:

>>> from datetime
import timedelta
   >>>
   ns = 45900482044637 >>>
   print(timedelta(microseconds = round(ns, -3) // 1000))
         12: 45: 00.482045

Use:

long millis = TimeUnit.MILLISECONDS.convert(nanosecond, TimeUnit.NANOSECONDS);
Date date = new Date(millis);
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm aaa");
String dateFormatted = formatter.format(date);

Suggestion : 2

To get the uptime in human-readable anycodings_android format in Python:,Vertx-opentracing - the built span contains the http method name instead of operation name,How to write in Vim's command prompt from script without actually executing the command, but let the user the possibility to edit it?,The answer is twelve hours, forty-five anycodings_android minutes, and a fraction of a second.

tl;dr

Duration.ofNanos(45_900_482_044_637 L)

Note the L appended to numeric literal anycodings_android for a long. Also, underscores make anycodings_android lengthy numbers easier for humans to anycodings_android decipher.

long input = 45_900_482_044_637 L;

Let's convert that number to a Duration anycodings_android object.

Duration duration = Duration.ofNanos(input);

To get the uptime in human-readable anycodings_android format in Python:

>>> from datetime
import timedelta
   >>>
   ns = 45900482044637 >>>
   print(timedelta(microseconds = round(ns, -3) // 1000))
         12: 45: 00.482045

Use:

long millis = TimeUnit.MILLISECONDS.convert(nanosecond, TimeUnit.NANOSECONDS);
Date date = new Date(millis);
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm aaa");
String dateFormatted = formatter.format(date);

Suggestion : 3

Edit. As another extension on Ignacio's answer, here are some scripts to enhance dmesg output on older systems.,Basically, dmesg is reported to have a new switch -T, --ctime.,To extend on Ignacio's answer, the entries contained in dmesg are typically also logged elsewhere on the system, via syslog, which will give you a "real" timestamp. Unless Ubuntu have changed the Debian-set default, the log entries should be in /var/log/kern.log.,Edit(2): Please note that -- as per Womble's comment below, -- this will only work if the machine was not hibernated etc. ( In that case, one shall better look at syslog configs at /etc/*syslog* and check the appropriate files. See also: dmesg vs /var/messages . )

Finally, for a single value like 600711.395348 one could do

ut = `cut -d' ' -f1 </proc/uptime`
ts = `date +%s`
date - d "70-1-1 + $ts sec - $ut sec + $(date +%:::z) hour + 600711.395348 sec" + "%F %T"

I know this is now old but dmesg now has a built in -e or --reatime option to display the time in the local time.

root @bbs: /var/log
# dmesg | tail - 1[50755952.379177] Out of memory in UB 1593: OOM killed process 3183(sbbs) score 0 vm: 747204 kB, rss: 242764 kB, swap: 88224 kB

root @bbs: /var/log
# dmesg - e | tail - 1[Feb20 17: 10] Out of memory in UB 1593: OOM killed process 3183(sbbs) score 0 vm: 747204 kB, rss: 242764 kB, swap: 88224 kB

On busybox, the 3 liner above didn't work, so here is my way to calculate it one off (replace 1628880.0 with your dmesg timestamp):

perl - e '@a=split(`/proc/uptime`);print scalar(localtime(time()+$a[0] - 1628880.0)."\n");'

Suggestion : 4

Last Updated : 31 Jul, 2019

Syntax:

dateObj = new Date(given_seconds * 1000);
hours = dateObj.getUTCHours();
minutes = dateObj.getUTCMinutes();
seconds = dateObj.getSeconds();

timeString = hours.toString().padStart(2, '0') + ':' +
   minutes.toString().padStart(2, '0') + ':' +
   seconds.toString().padStart(2, '0');

Suggestion : 5

parseObtains a Duration from a text string such as PnDTnHnMn.nS. This will parse a textual representation,betweenObtains an instance of Duration representing the duration between two instants. Obtains a Duration ,onRequestPermissionsResult (Fragment),compareToCompares this duration to the specified Duration. The comparison is based on the total length of the

boolean isThisRealtimeUpdateAboutThisLineRun(EdgeIteratorState edge, Instant now) {
   if (Duration.between(feedTimestampOrNow(), now).toHours() > 24) {
      return false;
   } else {
      return true;
   }
}
public static String elapsedTime(Date d1, Date d2) {
   try {
      Duration period = Duration.between(d1.toInstant(), d2.toInstant());
      // Note: this will become easier with Java 9, using toDaysPart() etc.
      long days = period.toDays();
      period = period.minusDays(days);
      long hours = period.toHours();
      period = period.minusHours(hours);
      long minutes = period.toMinutes();
      period = period.minusMinutes(minutes);
      long seconds = period.getSeconds();
      return days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
   } catch (java.lang.IllegalArgumentException e) {
      log.warn(e);
   }
   return "";
}
long hours = Duration.ofMillis(timeZone.getRawOffset()).toHours();
long millis = Duration.ofHours(hours).toMillis();
if (millis == timeZone.getRawOffset()) {
long hours = p.toHours() % 24;
long minutes = p.toMinutes() % 60;
long seconds = p.getSeconds() % 60;
  @Override
  public Argument build(Duration duration, ConfigRegistry config) {
     Duration d = duration;
     final boolean isNegative = d.isNegative();
     if (isNegative) {
        d = d.negated();
     }
     final long days = d.toDays();
     if (days > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(
           String.format("duration %s too large to be represented unambiguously as postgres interval",
              d));
     }
     d = d.minusDays(days);
     final int hours = (int) d.toHours();
     d = d.minusHours(hours);
     final int minutes = (int) d.toMinutes();
     d = d.minusMinutes(minutes);
     if (d.getNano() % 1000 != 0) {
        throw new IllegalArgumentException(
           String.format("duration %s too precise to represented as postgres interval", d));
     }
     double seconds = d.getSeconds() + d.getNano() / 1e9;
     final PGInterval interval = new PGInterval(0, 0, (int) days, hours, minutes, seconds);
     if (isNegative) {
        interval.scale(-1);
     }
     return ObjectArgument.of(interval, Types.OTHER);
  }
  }
System.out.println("Duration in hours: " + duration.toHours());

Suggestion : 6

Last Update: April 15, 2022

Why? Because when subtracting two dates using the Form Calculation widget, the default difference is in the unit of days. To convert a day into Epoch Time (seconds), we’ll multiply the absolute difference by 86400:

abs(Date1 - Date2) * 86400

To get the minutes result, use the formula: floor((EpochTime % 3600) / 60)

Given: EpochTime = 12345 = floor((12345 % 3600) / 60) = floor(1545 / 60) = floor(25.75) = 25

To get the result for hours, use the formula: floor(EpochTime / 3600)

Given: EpochTime = 12345 = floor(12345 / 3600) = floor(3.4291666667) = 3

Years Formula

floor(EpochTime / 31556926)

This one’s straightforward, just divide the Epoch Time by 31556926 then round down the quotient.

= floor(319080600 / 31556926) = floor(10.11127) = 10