2

I wish to learn how accurate is the time that is been shown when typing date +%s%N in the terminal.

I know this gives us the epoch time in nanoseconds but are the numbers real?. Does it represent the moment i hit enter in nanoseconds or is it fake at some accuracy and below ;say milliseconds ?

2 Answers 2

4

Does it represent the moment i hit enter in nanoseconds?

Well that couldn't be. Many things happen between your hit on Enter and the command outputting :

  • Your shell interprets the command and executes /bin/date.
  • The kernel creates a process, gives it a PID, sets its parent, gives some address space, registers the process, and so on...
  • The /bin/date program executes. It probably requires some memory to be allocated. It calls several time-related kernel routines, which cause the process to alternate several times (just like any other you'd say...) between user mode and kernel mode.
  • The command output is sent to stdout, and you can see it in your terminal, hourray!

Basically, you could put it this way :

  • You hit Enter.
  • [a few milliseconds pass, see above]
  • The date program calls the magical routines in time.h, switching into kernel mode for each of them. Here, the OS gets the current time from the hardware clocks.
  • [a few milliseconds pass, see above]
  • You get the output.

Those intermediate times may vary according to several things, they're barely measurable. As an example, if your CPUs have many processes to handle, then your date process will have to sleep longer between each of its execution phases.

One could go even further by adding :

  • You get the output.
  • Your eyes blink (100-400 milliseconds says Wikpedia).
  • You actually see the output.
  • Your brain interprets and understands it.

Adding a few more milliseconds to the count.

2
  • So lets say that it is impossible to syncronize two machines/computers in a nanosecond level? It doesn't has to be real time..i just want them syncronized..
    – prieston
    Jul 1, 2014 at 16:52
  • I think it mostly depends on what you're trying to achieve at the application level. This might be worthy of another question opened, but maybe not specifically on Ask Ubuntu. Jul 1, 2014 at 19:30
2

Of course it isn't. Everything you do takes a measurable amount of time. From hitting enter, to the keyboard reacting to it, passing it on to the shell, which will run date, to displaying the output on your terminal and your reading it. To get a better idea, have a look at this very nice Q&A on what exactly happens when you hit a key on your keyboard.

If we reuse the diagrams from that post:

Input

      +-------------------+               +-------------+
----->| terminal emulator |-------------->| application |
      +-------------------+               +-------------+
keypress                    character or
                           escape sequence

Output

+-------------+               +-------------------+
| application |-------------->| terminal emulator |
+-------------+               +-------------------+
               character or
               escape sequence

Each of the arrows and each of the boxes shown above represent processes that take a measurable amount of time. In the case of the date program, the time you see is the time it got when it queried the system for the current time. The process would be something like

You hit enter --> `date` is executed -> queries current time

Again, each of the above steps (and other, smaller ones, run strace date to get an idea) take time so no, the returned time will not be the exact moment you hit Enter.

By the time the result is printed on your terminal, the time (at the nanosecond accuracy) will have changed again. What you see is the that was returned by the system when date asked.

However, you can actually rest assured that it is accurate enough for anything you might want to do on your computer. If you require accuracy at the nanosecond level, you're probably going to need custom-built hardware.

1
  • So lets say that it is impossible to syncronize two machines/computers in a nanosecond level? It doesn't has to be real time..i just want them syncronized..
    – prieston
    Jul 1, 2014 at 17:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .