I have a integer number say Int64 i. the data in this variable is both in minute format and seconds format. Ho can identify the integer number is in munutes or in seconds
suppose,
Int64 i = 0;
now values may be..(For date 2016-02-18 00:00:00:000)
i = 1140220800; //(value is in seconds)
//or
i = 19003680; //(value is in minutes i.e. 1140220800/60)
No how to identify data is in minutes or seconds ?
Well you could just assume that if the number if smaller than some threshold value which makes sense in your context then it's minutes, else it's seconds. For example:
int number = 19003680;
if(number < 42076800) // 2050-01-01 in minutes, 1971-03-05 in seconds.
{
// minutes
}
else
{
// seconds
}
However, this question goes against the principles of good programming - one variable should only have one meaning. If a variable can represent both seconds and minutes, there's a problem in the design of your data.
EDIT:
If you can't reasonably fence your data to where seconds and minutes don't overlap - that is, if you can have both large dates represented in seconds, and small dates represented in minutes, then sadly it's impossible to differentiate between the two.
Related
The main "Ping"/"Time" function for the game is this:
Net Code For Server - Time/Ping
Outside of the switch, TimeSync() functions like this:
private void TimeSync()
{
YGConnection.Send("Time", DateTime.UtcNow.Millisecond);
}
It sends the current UTCNow.Millisecond time to the server.
Finally, getTime() gets the current time + the offset of the server.
private double getTime()
{
return Math.Round((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds) + Offset;
}
It all works, but the numbers produced are like this:
Version 0.1.5 : Server time offset: -486335789940 - Ping: -943491433067
The server basically sends getTime() back, but without the offset. I'm wondering what is going on with the negative numbers and if there is anything I can do to fix that.
DateTime.Milliseconds is the number of milliseconds (thousandths of a second) within the current second. If you only compare Milliseconds, you will lose any information like the second it occurred in.
At midnight it is 00:00:00.000
500 milliseconds later it is 00:00:00.500
500 milliseconds later it is 00:00:01.000
If you are just comparing the milliseconds, the difference between the first two times will be 500.
The difference between the last two will be -500.
What you probably want to do is return a whole date/time value rather than just the milliseconds. If you subtract 2 DateTime objects, you get a TimeSpan object. From that you can find the TimeSpan.TotalMilliseconds will give you how many milliseconds there are between those two times regardless of how many seconds have elapsed.
I'm trying to create a clock for my game. My hours and seconds are both float values so I am using Math.Round to round them off to the nearest whole number. The problem is that the Hours and Seconds variables aren't changing at all. Am I using Math.Round wrong?
public void Update()
{
Hours = (float)Math.Round(Hours, 0);
ClockTime = Hours + ":" + Seconds;
if (Hours >= 24)
Hours = 0;
if (Seconds >= 60)
Seconds = 0;
}
In my update method for my day/night class.
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
clock.Hours += (float)elapsed;
clock.Update();
When I print the numbers on the screen, nothing is changing. If I take away the (float) cast to the Math.Round I get an error cannot convert double to float.
Don't use floating point in this case, there's absolutely no reason for an hour, minute or second to be non-integral.
What's almost certainly happening is that you're ending up with a float value like 59.9999 despite the fact you think you're rounding it.
There are real dangers in assuming floating point values have more precision than they actually do.
If you hold your number of seconds in an unsigned integral 32-bit type, you can represent elapsed time from now until about the year 2150 AD, should anyone still be playing your game at that point :-)
Then you simply use integer calculations to work out hours and seconds (assuming you're not interested in minutes as seems to be the case), pseudo-code such as:
hours = elapsed_secs / 3600
secs = elapsed_secs % 3600
print hours ":" seconds
Beyond that advice, what you're doing seems a tad strange. You are adding an elapsed seconds field (which I assume you're checked isn't always set to zero) to the hours variable. That's going to make gameplay a little difficult as time speeds by at three and a half thousand times its normal rate.
Actually, you should used DateTime to track your time and use the DateTime properties to get the hours and seconds correctly instead trying it yourself using float for seconds and hours. DateTime is long based and supports from fractions of milliseconds to millenias and of course seconds. It has all the functions built in to add milliseconds or years or seconds or ... correctly, which is actually rather difficult.
I am comparing two DateTimes to see if there is 10 or fewer minutes between them. If I do DateTimeA - DateTimeB, and A is on 4/1/13 and B is on 4/3/13 I won't get the desired results.
I am only worried about minutes. So DateTimeA takes place at 8:00 am and DateTimeB takes place at 12:20 pm, I would want the result to be 260 minutes.
(DateTimeA - DateTimeB).TotalMinutes % 24*60
Get the total number of minutes modulo the number of minutes in a day. That way you'll get rid of the different-day problem.
You will need to use DateTime.TimeofDay() To get the actual time values and then do arithmatic on them.
or use DateTime.Minute DateTime.Hour DateTime.Second. Here is a full explanation on DateTime objects: http://msdn.microsoft.com/en-us/library/system.datetime.aspx
(A - B).TotalMinutes
A-B will yield a Timespan, for which the total minutes can then be produced.
Out Time :
2013-03-08 15:00:00.000
In Time :
2013-03-08 11:21:03.290
I need to get Hours and Minutes separately for same date from above, when (Out Time - In Time).
How can I do that ?
I think you probably just want:
TimeSpan difference = outTime - inTime;
int hours = (int) difference.TotalHours;
int minutes = difference.Minutes;
Note that Minutes will give you "just the minutes (never more than 59)" whereas TotalHours (truncated towards zero) will give you "the total number of hours" which might be more than 23 if the times are more than a day apart.
You should also consider what you want to do if the values are negative - either consider it, or explicitly rule it out by validating against it.
The Subtract method on the DateTime class will allow you subtract that date from the other date.
It will give you a TimeSpan which will be the difference.
I'll leave it to you to work out the actual code.
http://msdn.microsoft.com/en-GB/library/8ysw4sby.aspx
You can use Hours property and Minutes
link : http://msdn.microsoft.com/en-us/library/system.datetime.hour.aspx
I may be being really stupid here, but my brain's gone blank.
I've got a slider bar (which uses Int32 values) but I want to use it to select a position in a music song (mm.ss)
I also want to output the value that the slider is displaying in to a label above it, so it's easier to see what the slider is set to.
Anyone have any suggestions?
I thought of trying to convert the int value in to a decimal then dividing by 60.
I'm doing this in C# by the way.
What does the int value represent? If it's the number of seconds through the song, you should use:
TimeSpan time = TimeSpan.FromSeconds(seconds);
string text = time.ToString(#"mm\.ss");
Using a decimal would be a really bad idea - a format with a number of seconds isn't the same as a fractional number of minutes. For example, 10.50 minutes as a fractional number of minutes is 10 minutes and 30 seconds, not 10 minutes and 50 seconds, which is what you want as far as I can tell.
TimeSpan is the natural way of representing a time duration in .NET... which is why that's the type which supports formatting in minutes and seconds.
Converting it into decimal would not display it the way that you want it. For example four and an half minute would display as 4:50, not 4:30.
Divide the time into minutes and seconds, and format them:
int minutes = time / 60;
int seconds = time % 60;
string formatted = minutes.ToString() + ":" + seconds.ToString("00");
You should represent the length of the song internally as seconds (which can be saved in an int). To actually display it, you do song_length / 60 to get the minutes (this is called integer division, it returns the result and discards the fraction, e.g. 100/60 = 1).
After that, you can get the seconds by song_length % 60 (% is the modulo operator).
Edit: didn't see the c# tag. Jon Skeet's answer is more appropiate in c#.
Edit: I misunderstood the scope of the question, but I'll leave this in case it's helpful.
Store the total length of the track in seconds.
Calculate the position of the slider as a fraction of the maximum possible slider value.
Multiply the track length (in seconds) with fractional value obtained in 2). This gives the offset from the start of the track in seconds.
Format the value from 3) for display.
Here is a contrived example to illustrate the process:
int maxSliderValue = 100;
int sliderValue = 18;
int trackLengthInSeconds = 248;
float sliderFraction = ((float)sliderValue)/((float)maxSliderValue);
int offsetInSeconds = Convert.ToInt32(Math.Floor(trackLengthInSeconds * sliderFraction));
TimeSpan offset = TimeSpan.FromSeconds(offsetInSeconds);
string displayValue = offset.ToString(#"mm\.ss");