Java 计算Borland Starteam服务器8中的客户端-服务器时间差

Java 计算Borland Starteam服务器8中的客户端-服务器时间差,java,time,starteam,Java,Time,Starteam,问题。我需要一种通过Starteam Java SDK 8.0查找Starteam服务器时间的方法。服务器的版本是8.0.172,因此方法server.getCurrentTime()不可用,因为它仅在服务器版本9.0中添加 动机。我的应用程序需要在特定日期使用视图。所以,如果客户端(应用程序正在运行的地方)和服务器之间的系统时间存在差异,那么获取的视图就不准确。在最坏的情况下,客户端请求的日期是服务器的未来日期,因此操作会导致异常。 我对SDK不太熟悉,但通过查看API,如果服务器位于已知时区

问题。我需要一种通过Starteam Java SDK 8.0查找Starteam服务器时间的方法。服务器的版本是8.0.172,因此方法
server.getCurrentTime()
不可用,因为它仅在服务器版本9.0中添加

动机。我的应用程序需要在特定日期使用视图。所以,如果客户端(应用程序正在运行的地方)和服务器之间的系统时间存在差异,那么获取的视图就不准确。在最坏的情况下,客户端请求的日期是服务器的未来日期,因此操作会导致异常。

我对SDK不太熟悉,但通过查看API,如果服务器位于已知时区,为什么不创建一个OLEDate对象,其日期将是客户端的时间,并根据服务器的时区进行适当滚动?

经过一些调查,我没有找到比使用临时物品更清洁的解决方案。我的应用程序请求项目的创建时间,并将其与本地时间进行比较。以下是我用来获取服务器时间的方法:

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

如果您的StarTeam服务器位于Windows机器上,并且您的代码将在Windows机器上执行,则您可以退出并执行NET time命令以获取该机器上的时间,然后将其与本地时间进行比较

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.");
    }

这与时区无关。在现实世界中,服务器和客户端的系统时钟可能不同步,我的应用程序需要计算时间偏移才能正常运行。好吧,我的StarTeam服务器在Unix机器上。但即使它在Windows上运行,我也不想绑定到特定的平台。无论如何,你的回答给出了另一个可能的解决方案。谢谢
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.