Skip to content

[BUG] DateTimeOffsetConverter calculates wrong offset #2582

@lucacivale

Description

@lucacivale

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

DateTimeOffsetConverter does not use TimeZoneInfo of DateTime to get the utc offset. Instead it uses the TimeZoneInfo of the current system time zone.

This is wrong because the system time zone can differ from the selected date time zone.

E.g
In germany we have winter time (GMT +1) from october to march and summer time (GMT +2) from april to september.
Let's say we are in germany and it's 1st feburary. The system time zone is GMT +1.

Using your sample project

  • pick 15th march => offset is 1
  • pick 15th july => offset is 1

This is wrong because the offset for 15th july should be 2 since it's summer time and not winter even though I'm currently in winter time.

I can create a PR for this but I don't know how to test this since we have to configure the system time zone right?

Expected Behavior

Get utc offset for the particular DateTime

var offset = localDateTime.Kind switch
{
	DateTimeKind.Local => TimeZoneInfo.Local.GetUtcOGetffset(value),
	DateTimeKind.Utc => TimeZoneInfo.Utc.GetUtcOffset(value),
	_ => TimeSpan.Zero,
};

Steps To Reproduce

Simple repro steps

  • Set system time zone to germany
  • Specify the DateTime.Kind in the converter
var localDateTime = DateTime.SpecifyKind(value, DateTimeKind.Local);

var offset = localDateTime.Kind switch
{
	DateTimeKind.Local => DateTimeOffset.Now.Offset,
	DateTimeKind.Utc => DateTimeOffset.UtcNow.Offset,
	_ => TimeSpan.Zero,
};
  • Open your DateTimeOffsetConverterPage
    • Select 15th march
      • Offset +1
    • Select 15th july
      • Offset +1

Link to public reproduction project repository

https://github.com/CommunityToolkit/Maui/tree/main/samples

Environment

- .NET MAUI CommunityToolkit: Main
- OS:
- .NET MAUI:9.0.40

Anything else?

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions