Borland Starteam Server 8의 클라이언트-서버 시계 차이를 계산하십시오
문제
문제. Starteam Java SDK 8.0을 통해 STARTEAM 서버 시간을 찾는 방법이 필요합니다. 서버의 버전은 8.0.172 따라서 메소드입니다 Server.getCurrentTime()
서버 버전 9.0에서만 추가되었으므로 사용할 수 없습니다.
동기 부여. 내 응용 프로그램은 특정 날짜에 뷰를 사용해야합니다. 따라서 클라이언트 (앱이 실행중인 경우)와 서버간에 시스템 시간에 약간의 차이가있는 경우 뷰가 정확하지 않습니다. 최악의 경우 클라이언트의 요청 된 날짜는 서버의 미래에 있으므로 작업이 예외입니다.
해결책
일부 조사 후 임시 품목을 사용하는 것보다 깨끗한 솔루션을 찾지 못했습니다. 내 앱은 항목의 생성 시간을 요청하고 현지 시간과 비교합니다. 서버 시간을 얻는 데 사용하는 방법은 다음과 같습니다.
public Date getCurrentServerTime() {
Folder rootFolder = project.getDefaultView().getRootFolder();
Topic newItem = (Topic) Item.createItem(project.getTypeNames().TOPIC, rootFolder);
newItem.update();
newItem.remove();
newItem.update();
return newItem.getCreatedTime().createDate();
}
다른 팁
STARKEAM 서버가 Windows 상자에 있고 코드가 Windows 상자에서 실행되는 경우 순 시간 해당 기계의 시간을 가져 오도록 명령 한 다음 현지 시간과 비교하십시오.
net time \\my_starteam_server_machine_name
돌아와야합니다 :
"Current time at \\my_starteam_server_machine_name is 10/28/2008 2:19 PM"
"The command completed successfully."
Codecollab과 함께 사용할 서버 시간을 찾는 방법을 생각해 내야했습니다. 다음은 임시 파일을 만들지 않고 수행하는 방법의 (긴) C# 코드 샘플입니다. 해상도는 1 초입니다.
static void Main(string[] args)
{
// ServerTime replacement for pre-2006 StarTeam servers.
// Picks a date in the future.
// Gets a view, sets the configuration to the date, and tries to get a property from the root folder.
// If it cannot retrieve the property, the date is too far in the future. Roll back the date to an earlier time.
DateTime StartTime = DateTime.Now;
Server s = new Server("serverAddress", 49201);
s.LogOn("User", "Password");
// Getting a view - doesn't matter which, as long as it is not deleted.
Project p = s.Projects[0];
View v = p.AccessibleViews[0]; // AccessibleViews saves checking permissions.
// Timestep to use when searching. One hour is fairly quick for resolution.
TimeSpan deltaTime = new TimeSpan(1, 0, 0);
deltaTime = new TimeSpan(24 * 365, 0, 0);
// Invalid calls return faster - start a ways in the future.
TimeSpan offset = new TimeSpan(24, 0, 0);
// Times before the view was created are invalid.
DateTime minTime = v.CreatedTime;
DateTime localTime = DateTime.Now;
if (localTime < minTime)
{
System.Console.WriteLine("Current time is older than view creation time: " + minTime);
// If the dates are so dissimilar that the current date is before the creation date,
// it is probably a good idea to use a bigger delta.
deltaTime = new TimeSpan(24 * 365, 0, 0);
// Set the offset to the minimum time and work up from there.
offset = minTime - localTime;
}
// Storage for calculated date.
DateTime testTime;
// Larger divisors converge quicker, but might take longer depending on offset.
const float stepDivisor = 10.0f;
bool foundValid = false;
while (true)
{
localTime = DateTime.Now;
testTime = localTime.Add(offset);
ViewConfiguration vc = ViewConfiguration.CreateFromTime(testTime);
View tempView = new View(v, vc);
System.Console.Write("Testing " + testTime + " (Offset " + (int)offset.TotalSeconds + ") (Delta " + deltaTime.TotalSeconds + "): ");
// Unfortunately, there is no isValid operation. Attempting to
// read a property from an invalid date configuration will
// throw an exception.
// An alternate to this would be proferred.
bool valid = true;
try
{
string testname = tempView.RootFolder.Name;
}
catch (ServerException)
{
System.Console.WriteLine(" InValid");
valid = false;
}
if (valid)
{
System.Console.WriteLine(" Valid");
// If the last check was invalid, the current check is valid, and
// If the change is this small, the time is very close to the server time.
if (foundValid == false && deltaTime.TotalSeconds <= 1)
{
break;
}
foundValid = true;
offset = offset.Add(deltaTime);
}
else
{
offset = offset.Subtract(deltaTime);
// Once a valid time is found, start reducing the timestep.
if (foundValid)
{
foundValid = false;
deltaTime = new TimeSpan(0,0,Math.Max((int)(deltaTime.TotalSeconds / stepDivisor), 1));
}
}
}
System.Console.WriteLine("Run time: " + (DateTime.Now - StartTime).TotalSeconds + " seconds.");
System.Console.WriteLine("The local time is " + localTime);
System.Console.WriteLine("The server time is " + testTime);
System.Console.WriteLine("The server time is offset from the local time by " + offset.TotalSeconds + " seconds.");
}
산출:
Testing 4/9/2009 3:05:40 PM (Offset 86400) (Delta 31536000): InValid
Testing 4/9/2008 3:05:40 PM (Offset -31449600) (Delta 31536000): Valid
...
Testing 4/8/2009 10:05:41 PM (Offset 25200) (Delta 3): InValid
Testing 4/8/2009 10:05:38 PM (Offset 25197) (Delta 1): Valid
Run time: 9.0933426 seconds.
The local time is 4/8/2009 3:05:41 PM
The server time is 4/8/2009 10:05:38 PM
The server time is offset from the local time by 25197 seconds.
<stab_in_the_dark>
나는 그 SDK에 익숙하지 않지만 서버가 알려진 시간대에있는 경우 API를 보면 서버의 시간대에 따라 클라이언트의 시간이 적절하게 롤링되는 날짜가 생성되지 않는 Oledate 객체를 작성하지 않겠습니까?</stab_in_the_dark>