There was a problem converting unixtime to DateTime.
I am passing in parameter 1663869600 this is September 22, 22. But in the code, after instrumentation, I get the date 1/20/1970 6:11:09 AM.
Why is that ?
I will convert the date in the following ways:
DateTime start = DateTimeOffset.FromUnixTimeMilliseconds(request.StartTime).DateTime;
var startUtc = DateTime.SpecifyKind(start, DateTimeKind.Utc);
and
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var startUtc = dtDateTime.AddMilliseconds(request.StartTime).ToLocalTime();
Unix time is number of seconds (not milliseconds) since the epoch (as #Gus correctly mentioned). So the solution is to use FromUnixTimeSeconds instead of FromUnixTimeMilliseconds and AddSeconds instead of AddMilliseconds.
DateTime start = DateTimeOffset.FromUnixTimeSeconds(1663869600).DateTime;
var startUtc = DateTime.SpecifyKind(start, DateTimeKind.Utc);
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var startUtc2 = dtDateTime.AddSeconds(1663869600).ToLocalTime();
Related
How come that s returns 7942 when it should be 13 minutes aka roughly 780 seconds?
var s = Math.Round((DateTime.Now - FromUnixTime(1589414482)).TotalSeconds);
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var date = DateTime.Now;
DateTime FromUnixTime(long unixTime)
{
return epoch.AddSeconds(unixTime);
}
DateTime.Now returns a local time. Unix Epoch times are based on UTC. That's your difference.
I have a little problem on a Unix Timestamp conversion in DateTime.
This is the Timestamp: 1521932400
I want convert it in DateTime, so I wrote this code:
public static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
return dtDateTime;
}
essentially I declare a DateTime and then add to that the value of the Timestamp, so in this case I'll get: 24/03/2018 23:00:00 but I should get: 25/03/2018 00:00:00.
What I did wrong?
Ok problem solved. I need to convert the DateTime returned from the conversion in my timezone, so I use:
public static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
//Convert in my timezone
TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(dtDateTime, timeInfo);
return userTime;
}
I have the follow code to get the local time in ms:
var dtNow = DateTime.Now;
var time = TimeSpan.FromMilliseconds((dtNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()).TotalMilliseconds);
long end_time = Convert.ToInt64(time.TotalMilliseconds);
The time object indicate to correct hour (11:20:00) but the ms object indicate on 12:20:00, Why its happend and how i can fix it?
Before the summer dst Its works perfecr.
Thanks!
Because your dtNow = DateTime.Now; is local and with (dtNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()) you're converting the time to local again .ToLocalTime()
Try:
var dtNow = DateTime.UtcNow;
My client application requires, from the server, "how many seconds between some value and 1970".
I'm testing this with the following code:
var span = (DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime()).TotalSeconds;
return span;
But if I convert the result from this unix time, I get something that's an hour later than now, so my client application is not behaving as expected.
What's going on?
Try with UTC times:
(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).TotalSeconds;
Is this what you're looking for?
var span = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
return span;
try:
TimeSpan span = DateTime.Now.Subtract(new DateTime(1970,1,1,0,0,0));
Then use span.TotalSeconds
What is the equivalent of PHP mktime and microtime in C#?
Here's for mktime (you'll have to verify timezones, though):
static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}
static int ConvertToUnixTimestamp(DateTime date)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan diff = date - origin;
return (int)diff.TotalSeconds;
}
microtime is basically the same, but you don't have to cast to int.
There are no direct equivalents, but they can easily be implemented... Since the UNIX timestamp is the number of seconds since January 1st 1970, it's easy to calculate :
public readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);
// equivalent to PHP mktime :
public int GetUnixTimestamp(DateTime dt)
{
TimeSpan span = dt - UnixEpoch;
return (int)span.TotalSeconds;
}
For microtime, you can use the DateTime.Tick property (1 tick = 100 nanoseconds, so 10 ticks = 1 microsecond)