Determining if a UTC time is within DST - c#

In C#, given a UTC time, how can I determine whether this falls within DST in Houston, Texas, US?
var utcDateTime = new DateTime(2013,1,1,0,0,0,DateTimeKind.Utc);
//bool fallsWithinDstInAmerica = ...?

Get the appropriate TimeZoneInfo for Texas, then use IsDaylightSavingTime.
using System;
class Test
{
static void Main()
{
// Don't be fooled by the "standard" part - this is Central Time
var zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var winter = new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var summer = new DateTime(2013, 6, 1, 0, 0, 0, DateTimeKind.Utc);
Console.WriteLine(zone.IsDaylightSavingTime(winter)); // False
Console.WriteLine(zone.IsDaylightSavingTime(summer)); // True
}
}
Or using Noda Time, you can find the amount of daylight saving, and compare it with an offset of 0:
using System;
using NodaTime;
class Test
{
static void Main()
{
var zone = DateTimeZoneProviders.Tzdb["America/Chicago"];
var winter = Instant.FromUtc(2013, 1, 1, 0, 0);
var summer = Instant.FromUtc(2013, 6, 1, 0, 0);
Console.WriteLine(zone.GetZoneInterval(winter).Savings); // +00
Console.WriteLine(zone.GetZoneInterval(summer).Savings); // +01
Console.WriteLine(zone.GetZoneInterval(winter).Savings != Offset.Zero); // False
Console.WriteLine(zone.GetZoneInterval(summer).Savings != Offset.Zero); // True
}
}

Related

Convert unixtime to DateTime immediately

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();

Compare DateTimeOffset with daylight saving time

Here the DateTimeOffset instances represent the same time, but one instance has DST and other doesn't.
I would like to compare instances without DST, so the result would be True.
using System;
var today = DateTime.Today;
// Today YYYY-MM-01
var firstDayMonth = new DateTimeOffset(
new DateTime(today.Year, today.Month, 1, 0, 0, 0, today.Kind).AddHours(12));
// Add year offset from 2000
DateTimeOffset withYearOffset = firstDayMonth.AddYears(2000 - firstDayMonth.Year);
// Add month offset from 1
var withMonthOffset = withYearOffset.AddMonths(1 - withYearOffset.Month);
// Add day offset from 1
var calculated_dt_2000_1_1_12_0_0 = withMonthOffset.AddDays(1 - withMonthOffset.Day);
// 01.01.2000 12:00:00 +03:00
Console.WriteLine(calculated_dt_2000_1_1_12_0_0);
var dt_2000_1_1_12_0_0 = new DateTimeOffset(new DateTime(2000, 1, 1).AddHours(12));
// 01.01.2000 12:00:00 + 02:00
Console.WriteLine(dt_2000_1_1_12_0_0);
// False
Console.WriteLine(calculated_dt_2000_1_1_12_0_0.Equals(dt_2000_1_1_12_0_0));
Converting to UTC solves the problem.
var calculated_dt_2000_1_1_12_0_0 = withMonthOffset.ToUniversalTime().AddHours(firstDayMonth.Offset.Hours);
var dt_2000_1_1_12_0_0_withOffset = new DateTimeOffset(new DateTime(2000, 1, 1).AddHours(12));
var dt_2000_1_1_12_0_0 = dt_2000_1_1_12_0_0_withOffset.ToUniversalTime().AddHours(dt_2000_1_1_12_0_0_withOffset.Offset.Hours);

Time difference from anywhere in the world

On my website i want to ensure if the time gap is under 1 minute of DateTime st of the below function, an action can be taken otherwise it would be declined. Here's the code for that
DateTime st = MyDate.Value; // current value from database: 2019-12-05 13:20:15.478
DateTime now = DateTime.Now;
TimeSpan span = now.Subtract(st);
int expMin = 1;
if (span.Minutes < expMin)
{
// Do something
}
else
{
// Ignore
}
So based on the above date value the service cant be accessed after 2019-12-05 13:21:15.478
This works locally and on my server but I've seen some reports where some users are accessing the service from another country. Is there another way i should be ensuring the time, no matter which country the user is from, can't be accessed after the one minute timespan?
You need to use DateTimeOffset to consider the timezone of your users.
I assume that "MyDate" is passed by the client. See the following example.
var myDate = new DateTimeOffset(2019, 12, 05, 13, 20, 15, 478, new TimeSpan(0, 2, 0, 0, 0));
var now = new DateTimeOffset(2019, 12, 05, 14, 20, 45, 478, new TimeSpan(0, 3, 0, 0, 0));
var span = now.Subtract(myDate);
int expMin = 1;
if (span.Minutes < expMin)
{
Console.WriteLine("Do something");
}
else
{
Console.WriteLine("Ignore");
}

Add one hour to summer dst

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;

PHP mktime() and microtime() equivalent in C#

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)

Categories

Resources