How to pass time value cross platform?

ssdarvinssdarvin Member Darvin TingOrganization: Donghai Securities Co.LtdProject: Warrants Market Making System
Hi, everybody
I'm trying passing time value using ICE. However, the presentations of time are different in different platform. For example, the DateTime type in C# records the ticks from 0001/01/01/00:00:00.0000000; the IceUtil::time records the ticks from 00:00:00 UTC on 1 Jan. 1970.
I plan to use a long type in Slice to representate the ticks of a time value. But, How can I unify the different standards?

Another question is, how to set an absolute time value in a IceUtil::time object, instead using static function now()?

Thank you!

Comments

  • xdmxdm La Coruña, SpainAdministrators, ZeroC Staff Jose Gutierrez de la ConchaOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    Using a long with milliseconds Since 1 Jan 1970 seems right approach if you don't need to represent dates before this.

    You will need some helper functions to handle dates in different languages, in C++ you can use IceUtil::Time.

    in C# you can use something like:
    public static double localDateTimeToTimestamp(DateTime date)
    {
        return (double)(date - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()).TotalMilliseconds;
    }
    
    public static DateTime timestampToLocalDateTime(double mseconds)
    {
        DateTime date = new System.DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        date = date.AddMilliseconds(mseconds);
        return date.ToLocalTime();
    }
    

    To set and absolute time use the static methods in IceUtil::Time
    IceUtil::Time oneMinute = IceUtil::Time::seconds(60);
    

    See The C++ Time Class - Ice 3.4 - ZeroC
  • ssdarvinssdarvin Member Darvin TingOrganization: Donghai Securities Co.LtdProject: Warrants Market Making System
    Thanks for your valuable advices!
    There is another question about IceUtil::time. Can I initialize a IceUtil::time object with a date?
    And, if I want to represent dates before 1970, can I use a long with milliseconds Since 1 Jan 0001? How to translate it to a IceUtil::time object?
  • xdmxdm La Coruña, SpainAdministrators, ZeroC Staff Jose Gutierrez de la ConchaOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    There is another question about IceUtil::time. Can I initialize a IceUtil::time object with a date?
    There isn't, you need to use one of the static methods that takes seconds ,miliSeconds or microSeconds since 1970-1-1 UTC
    And, if I want to represent dates before 1970, can I use a long with milliseconds Since 1 Jan 0001? How to translate it to a IceUtil::time object?
    You can use negative values, the functions localDateTimeToTimestamp, timestampToLocalDateTime should already handle negative values without problems.
    IceUtil::Time t = IceUtil::Time::seconds(-(24 * 3600 * 365)); // One year before 
    
  • ssdarvinssdarvin Member Darvin TingOrganization: Donghai Securities Co.LtdProject: Warrants Market Making System
    I think this problem could be solved now. Thank you!
Sign In or Register to comment.