Reputation: 41
I need help to migrate a java method:
(int)System.currentTimeMillis(); //result -186983989 (java) return diferent values
But in C# return alway the same value:
DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan span = DateTime.UtcNow - Jan1st1970;
Int32 resultado = (int)span.TotalMilliseconds; //result is always -2147483648 and i need same as java
It is rare becouse in debug quickwatch display the correct value, but in execution resultado
is alway -2147483648
I need this -186983989 result, same as java.
Upvotes: 4
Views: 393
Reputation: 52645
To mimic the overflow behavior you could do somthing using the unchecked keyword but I don't know enough about Java to know if this correct
DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan span = DateTime.UtcNow - Jan1st1970;
unchecked{
int mm = Int32.MaxValue;
mm += (int)(span.TotalMilliseconds % Int32.MaxValue);
Console.WriteLine(mm);
}
As an aside -2147483648 = Int32.MinValue
Upvotes: 0
Reputation: 66439
You'll want to use Int64 (long). Int32 isn't large enough to hold the value.
Int32.MaxValue = 2,147,483,647
Int64.MaxValue = 9,223,372,036,854,775,808
http://forums.asp.net/post/1203789.aspx
Upvotes: 3