Vitaliy Honcharuk
Vitaliy Honcharuk

Reputation: 19

TimeSpan utc0 to timezone and some days

Hi i have some table with starttime end time and timezone

I get data in DateTime.Utc

http://prntscr.com/a84tr3

And how i can check if the date what comes is get caught in this timezome from DB and this day

0-Monday... 5-Friday

If we have 0-1 this means start time in 23:00 Sunday to 22:00 Monday

public static DateTime GetDateTimeWithExchangeTimeZoneSessionStart(DateTime tickDateTime, string exchangeTimeZone)
    {
        DateTime convertedDateTime = tickDateTime;
        switch (exchangeTimeZone)
        {
            case "5.5":
                convertedDateTime = convertedDateTime.AddHours(-5.5);
                break;
            case "4":
                convertedDateTime = convertedDateTime.AddHours(-4);
                break;
            case "-6":
                convertedDateTime = convertedDateTime.AddHours(6).AddDays(-1);
                break;
            case "0":
                break;

        }
        return convertedDateTime;
    }

But it's not good i think

Upvotes: 0

Views: 133

Answers (2)

Vitaliy Honcharuk
Vitaliy Honcharuk

Reputation: 19

i need check if the date what come - is in this days from DB How it's will be code with dateofday method and this data 0,1,2,3,4,5,6

maybe write some class with this sessionstart sessionend and settlementime fields - and check this in utc0 and this time 17:00 etc.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500165

Firstly, it's important to understand that what you've got isn't a time zone. It's a UTC offset (although the negation of what it would normally be...). A real time zone would need to indicate how that UTC offset varies over time (e.g. due to daylight saving time changes).

However, with the data you've got, it looks like all you need is:

double hours = double.Parse(exchangeTimeZone, CultureInfo.InvariantCulture);
TimeSpan offset = TimeSpan.FromHours(hours);
return tickDateTime - offset;

However:

  • It would be cleaner to use DateTimeOffset given that you really do have a DateTime and an offset
  • Be really, really careful around DateTime.Kind... basically, DateTime is somewhat broken.
  • (Shameless plug) Consider using my Noda Time project to make your code much clearer for this and all your other date/time code

Upvotes: 1

Related Questions