Where is the function taking the current time?

Asked

Viewed 95 times

5

I have a function that returns the current time. Where does this value come from? Phone time? Server?

Just follow my lead:

public static Date getAgora()
    {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }
  • Just one detail: if you want the current date/time as one java.util.Date, don’t need to create a Calendar. Use only new Date() is enough.

1 answer

8


In the first instance comes from the operating system that controls the time of the moment for the entire system. It allows this time to be changed in various ways, for example to be synchronized with an external clock.

This operating system clock is governed by the clock hardware, which unlike the name is not exactly a clock but a system of operations synchronization. The operating system uses this timing system to calculate how much time has passed and know what is the current time.

This clock is responsible for issuing pulses that say an operation can be performed, all components of the hardware must do what they know how to do according to these pulses. The pulse depends on the speed set for the equipment as a whole to work. This 2ghz that you read in the hardware specs means there will be 2 billion pulses per second. If you didn’t have this, each component would operate according to its need and capacity and the system would be all messed up. The processor has a pulse counter that can be consulted by the OS.

So the operating system knows that after there were 2 billion pulses it spent 1 second. Of course he can calculate fractions of that, but he can’t get to the accuracy of one pulse, not even close to that, because the calculation itself takes several pulses. The best he can do is a few hundred pulses (in practice in the most common scenarios he usually stays in the thousands).

Precisely because it does not have its own updated time it is necessary to initialize (it can take a data recorded somewhere on the hardware) and even external synchronization. Operating system control is not exactly accurate and external synchronization is useful.

In general the hardware systems have a mechanism to maintain this control even when the equipment is off using some form of battery (it stores the last known time and increases the pulses without having to be all system on)but in systems that are guaranteed to depend on external connection this is not even necessary since every boot can synchronize with the external source (I do not think that is the case of mobile phones, there is no network guarantee available).

  • I mean, even with a time zone, he gets the hardware clock system, right? So I don’t have to worry about the time zone, as much as it’s not exactly necessary.

  • 2

    Time zone has nothing to do with it, this is a human concept determined by other factors and the exact way is determined by the implementation and configuration of the operating system, so the change is determined by software, if the hardware no longer has a real time, let alone have a time zone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.