2008-09-24 78 views
1

問題。我需要一種通過Starteam Java SDK 8.0查找Starteam服務器時間的方法。服務器的版本是8.0.172,所以方法Server.getCurrentTime()不可用,因爲它僅在服務器版本9.0中添加。在Borland Starteam服務器中計算客戶端 - 服務器時間差異8

動機。我的應用程序需要在特定日期使用視圖。因此,如果客戶端(應用程序運行的地方)和服務器之間的系統時間有所不同,那麼獲得的視圖並不準確。在最糟糕的情況下,客戶端的請求日期將在服務器上運行,因此操作會導致異常。

回答

2

經過一番調查後,我還沒有找到比使用臨時物品更清潔的解決方案。我的應用請求該項目的創建時間並將其與本地時間進行比較。下面是我用它來獲得服務器時間的方法:

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(); 
} 
-1

<stab_in_the_dark> 我不熟悉的SDK,但是從看API如果服務器是一個已知的時區,爲什麼不能創建和OLEDate對象,其日期將是客戶端的時間揉捻適當地根據服務器的時區? </stab_in_the_dark>

+0

這不是關於時區。現實世界中的服務器和客戶端的系統時鐘可能不會同步,我的應用需要計算時間偏移以正確運行。 – wheleph 2008-09-24 12:32:43

1

如果StarTeam服務器是在Windows中,你的代碼將在Windows中被執行,你可以掏出並執行NET時間命令在該機器上獲取時間,然後將其與當地時間進行比較。

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." 
+0

好吧,我的StarTeam服務器在Unix機器上。但即使它在Windows上運行,我也不想綁定到特定的平臺。無論如何,你的回答給出了另一種可能的解決方案謝謝。 – wheleph 2008-10-28 21:00:27

1

我們需要拿出尋找與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.