[ACCEPTED]-How do computers figure out date information?-datetime

Accepted answer
Score: 10

Every computer has a system clock which 11 keeps track of date and time. On the lowest 10 level, date and time information it retrieved 9 from there. Above that add timezone information, etc. from 8 the operating system and you got a Date 7 object or something similar.

Depending on 6 your language/environment Date objects can 5 either perform date calculation themselves 4 or you have to use other functions to achieve 3 that. Those ensure that leap years get handled 2 correctly and no invalid date can be created.

But 1 maybe I got your question wrong.

Score: 6

Typically a computer is storing a count 21 of how many of a certain unit of time has 20 gone by since a specific time and date in 19 the past. In Unix systems, for example, this 18 could be the number of seconds since the 17 Unix Epoch, which is midnight, Jan 1st 1970 16 GMT. In Windows, this is the number of 100 15 ns intervals since 1601-01-0 (thanks Johannes Rössel). Or 14 it could be something as simple as number 13 of seconds since the computer was powered 12 on.

So from the number of units that have 11 gone by since that time/date, an OS can 10 calculate the number of years, months, days, etc 9 that have gone by. Of course all sorts of 8 fun stuff like leap years and leap seconds 7 have to be taken into account for this to 6 occur.

Systems such as NTP (Network Time Protocol) can be used to synchronize 5 a computer's internal count to atomic clocks 4 via an NTP server over a network. To do 3 this, they NTP takes into account the round 2 trip time and learns the sorts of errors 1 the link to the NTP server.

Score: 3

Date and time information is provided usually 4 by operating system, so it's a system call. Operating 3 system deals with realtime clock mounted 2 on computer mainboard and powered by small 1 battery (which lasts for years).

Score: 3

Well ... Most computers contain a "real-time clock", which 14 counts time on the human scale of seconds, minutes 13 etc. Traditionally, there is a small battery 12 on the motherboard, that lets the chip either 11 remember the time, or even keep counting 10 it, even when the rest of the computer is 9 powered off.

Many computers today use services 8 like the network time protocol to periodically query a centralized 7 high-precision clock, to set the current 6 time. In this way, even if the battery is 5 removed (or just fails), the computer will 4 still know what time and date it is, and 3 be able to update (to correct for errors 2 in the real-time chip's time-keeping) that 1 information as often as necessary.

Score: 3

Aside from the realtime clock, date calculations 11 are mostly a software library function.

Dates 10 are rather irregular and so behind the scenes 9 a mixture of approximations, corrections 8 and lookup-tables are used.

The representation 7 of a date can vary as well, usually some 6 (arbitrary) startdate is used. A common 5 system, also used by astronomers are the 4 Julian day numbers (not to be confused with the Julian calendar). Dates can 3 be stored as seconds-since-start or as days-since-start 2 (the latter is usually a floating point). Here 1 are some more algorithms.

Score: 2

A surprising amount of surprisingly complicated 10 code is required for date parsing, computation, creation 9 etc.

For example, in Java, dates are computed, modified, stored 8 etc via the Date, Calendar, and specifically and typically, the 7 Gregorian Calendar implementation of Calendar. (You can download the SDK/JDK and look at the source for 6 yourself.)

In short, what I took from a 5 quick perusal of the source is: Date handling 4 is non-trivial and not something you want 3 to attempt on your own. Find a good library 2 if at all possible, else you will almost 1 certainly be reinventing the square wheel.

Score: 1

Your computer has a system clock and the 3 BIOS has a timer function that can be updated 2 from your OS. Languages only take the information 1 from there and some can update it too.

Score: 1

Buy any of these books on Calendrical Calculations. They'll fill 2 you in on how the date libraries work under 1 the hood.

Score: 1

The date/time is often stored in terms of 11 times since a certain date. For example 10 ticks (100 nanosecond intervals) since January 9 1, 0001. It is also usueally stored in 8 reference to UTC. The underlying methods 7 in the OS, database, framework, application, etc. can 6 then convert these to a more usable representation. Back 5 in the day, systems would store component 4 parts of the date, day, month, year, etc 3 as a part of the data structure, but we 2 learned our lesson with the Y2K mess that 1 this probably isn't the best approach.

Score: 1

Most replies have been to do with how the 28 current date is obtained. i.e. from system 27 clock and so on.

If you want to know how 26 it is stored and used there are many different 25 implementations and it depends on the system.

I 24 believe a common one is the use of a 64 23 bit signed integer in T-sql the 01/01/1970 22 is 0 so negative numbers are pre 1970 and 21 positive on from that each increment adding 20 100 th of a second (think it's a 100th would 19 need to check).

Why 01/01/1970 you may ask 18 this is because the gregorian calendar is 17 on a 400 year cycle. 01/01/1970 being the 16 closes start of a cycle to the current date.

This 15 is because "Every year that is exactly divisible 14 by four is a leap year, except for years 13 that are exactly divisible by 100; the centurial 12 years that are exactly divisible by 400 11 are still leap years. For example, the year 10 1900 is not a leap year; the year 2000 is 9 a leap year." Makes it very complicated 8 I believe the 400 year cycle coincides with 7 the days of the week repeating as well but 6 would nee dto check. Basically it's very 5 complicated.

Internally it is incredibly 4 difficult to write the datetime library 3 accounting for all these variations such 2 as leap years, the fact there is no year 1 zero..... Not to mention UTC, GMT UT1 times.

Score: 1

We had occasion when debugging a client 10 problem to look at how SQL stores datetimes... fairly 9 interesting and makes pretty good sense 8 once you see it.

SQL uses 2 4 byte integers... The 7 first 4 bytes are the date in days since 6 Jan. 1st, 1753. I believe the maximum year 5 is supposed to be 9999, which doesn't exactly 4 line up to the number of available integers 3 in 4 bytes, but there you go. The second 2 4 bytes are the time in milliseconds since 1 midnight.

More Related questions