Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Taxel · Jan 09, 2016 at 03:11 PM · unity 5nodejstimestamp

Nodejs and Unity: Incorrect timing

Hey guys,

apparently Unity and Node.js are treating / calculating time differently. On the node.js side, I start a new round by having two variables, timestampLastRound and timestampNextRound. They are initialized as Date.now() and Date.now() + ROUND_LENGTH * 1000 (because the round length is in seconds and JS timestamps are in milliseconds). When Unity asks for the timings, it sends a GET request to the server (running on the same machine, so this should be pretty much instantaneous, right?) http://localhost/time is the url, and the server answers with a JSON Object which looks like this:

  {
         nextRound: Math.round(timestampNextRound / 1000), //unity uses seconds instead of milliseconds anyway and it seemed a little easier to read to just transmit seconds
         roundCount: roundCount,
         roundLength: ROUND_LENGTH
   }

On the Unity side of things, the JSON Object is first parsed by the class JSONObject (not written by me), and then converted like so:

 public override void FromJSON(JSONObject json)
         {
             int nextRoundTimestamp = 0;
             json.GetField(ref nextRoundTimestamp, "nextRound");
             nextRound = ConvertFromUnixTimestamp(nextRoundTimestamp);
 
             json.GetField(ref roundCount, "roundCount");
             json.GetField(ref roundLength, "roundLength");
         }
 
         /// <summary>
         /// Converts a Unix timestamp into a System.DateTime
         /// </summary>
         /// <param name="timestamp">The Unix timestamp in seconds to convert</param>
         /// <returns>DateTime obtained through conversion</returns>
         public static DateTime ConvertFromUnixTimestamp(double timestamp)
         {
             DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
             origin = origin.AddSeconds(timestamp);
             return DateTime.SpecifyKind(origin, DateTimeKind.Utc);
             
         }

Here's my problem: If I now output nextRound - System.DateTime.Now, formatted in minutes and seconds, I find out that the next timestep will be in 1 minute and 30 seconds, whereas the rounds are only 60 seconds long and the server correctly shows, that there is at most a minute left until the next round. Compensating for 30 seconds would be easy, but I want to know why this happens. Also it's not always 30 seconds off, sometimes it's 37 seconds, sometimes it's less.

Does anyone see why this happens and how I can fix it?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameTestAutomationExample 1 Answer

Line Renderer looks different in Game View to Scene View 0 Answers

Get Asset/Prefab Preview In Unity 5 2 Answers

Rendering 2 different images in HMD for left and right eye using StreamVR 0 Answers

How To Remove Fullscreen Option Or Fix Resizing Bug? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges