One simple way is to pass in the time from outside the method:
doSomething(long currentTime) {
//...stuff
startTime = currentTime;
//... more stuff
}
Your production code that calls the method can pass in the real value for System.currentTimeMillis(), whereas your unit test(s) can pass in a specific known value.
Of course, this isn't quite the same as your original. Another possibility that is a little more complex but should mimic the behaviour of your of your original more closely:
doSomething(ITimeGetter timeGetter) {
//...stuff
startTime = timeGetter.GetCurrentTime;
//... more stuff
}
Define the interface:
interface ITimeGetter
{
long GetCurrentTime();
}
Two implementations, one for the production code:
class RealTimeGetter implements ITimeGetter
{
long GetCurrentTime()
{
return System.currentTimeMillis();
}
}
One for the unit tests:
class TimeGetterForTests implements ITimeGetter
{
long GetCurrentTime()
{
// return a known value.
}
}
Instantiate an ITimeGetter appropriate to your scenario and pass it in to the method you want to test.
Dates and timezones are tricky things, and generally no matter what kind of technology stack you are working on there is rarely a turn key solution.
Most databases will store a date or timestamp as little more than the exact date or timestamp that was given to it. When talking with a database through queries they are generally under the assumption that all dates and timestamps represent a point in time under the same time zone. In other words, if I write a query to see if Aug 2nd 2012 4:10pm is before the same date at 5:10pm then that will evaluate to true returning me that record. Suppose the record was saved as 4:10pm Eastern Standard Time when my database server is in Pacific Standard Time. The database is time zone agnostic and now my query is not returning the data I expect.
Convert all application dates and times to UTC or GMT before persisting
Languages like Java, Javascript and C# treat dates and times a bit differently. They will generally measure time as a number of milliseconds from a given universal point in time. This is the universal time as that specific number of milliseconds from 0 represents a number of different dates or times in different timezones at any given point. Most of these languages generally have a solid Date and Time API or a good third party library can be found that make viewing this millisecond count in a number of timezones as painless as possible.
Unless otherwise specified, if I use a standard data access API in a common language, most will take a date object and convert it into the date and time of the servers default timezone in the specified database format. Figure out how your language data access API and database stores dates and how it interprets them from the database and try to store your dates at the database level in GMT. Likewise when querying make sure that Date objects or variables in your language are representing a millisecond count that is equivalent to the GMT date time stored in the database.
Persist Time Zones for Dates that are used in Business Logic
What I mean by this is that perhaps if you have a Modified Date column in your database, then it may not be important to business logic, however a column Appointment Date probably will be important.
Somewhere in your schema you need to determine where locale specific data is stored and store the Time Zone to define that locale in the appropriate parent table. Your business logic that is considering dates, or your presentation logic that is preparing dates for display must be able to fetch the appropriate Time Zone so that when I am comparing 4:10pm to 5:10pm, that I am comparing dates from the appropriate universal time. 4:10pm EST and 4:10pm PST are seperate points in universal time. Again a good Date and Time API or third party library makes date comparisons easy, when you understand the underlying nature of how dates work in that programming language.
Business Logic Should be refactored to be time zone aware.
Presentation Logic Should be refactored to present a display value that is appropriately displaying the correct data per the appropriate given Time Zone.
One final thought to consider when performing this work is to write exhaustive unit tests that thoroughly exhaust many types of Date and Time combinations to make sure that all business logic works as expected.
Best Answer
Pretty much all computers use this or a similar variation of Unix time.
Typically, a computer includes one or more hardware devices that count time, usually based on a crystal oscillator (the same as used in most modern clocks). There are serveral variations/standards for such devices:
These timer devices usually have a separate battery that allows them to continue running when the computer is switched off. The computer interacts with them in two ways:
However, the crystal oscillators used in the hardware timers have limited precision and over time drift away from the correct time. For applications where it's important to have an accurate clock (or just to avoid the hassle of having to manually adjust it), computers regularly synchronize their time via the Network Time Protocol with a time server, which is connected (directly or indirectly) to a high-precision atomic clock.