Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TimeZone parameter to BitDateRangePicker (#10293) #10297

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public partial class BitDateRangePicker : BitInputBase<BitDateRangePickerValue?>
private string _monthTitle = string.Empty;
private bool _showTimePickerAsOverlayInternal;
private bool _showMonthPickerAsOverlayInternal;
private TimeZoneInfo _timeZone = TimeZoneInfo.Local;
private CultureInfo _culture = CultureInfo.CurrentUICulture;
private CancellationTokenSource _cancellationTokenSource = new();
private DotNetObjectReference<BitDateRangePicker> _dotnetObj = default!;
Expand Down Expand Up @@ -423,6 +424,13 @@ private int _endTimeMinuteView
/// </summary>
[Parameter] public BitTimeFormat TimeFormat { get; set; }

/// <summary>
/// TimeZone for the DateRangePicker.
/// </summary>
[Parameter]
[CallOnSet(nameof(OnSetParameters))]
public TimeZoneInfo? TimeZone { get; set; }

/// <summary>
/// Whether or not the Text field of the DateRangePicker is underlined.
/// </summary>
Expand Down Expand Up @@ -607,7 +615,7 @@ protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(fa

//if (DateTime.TryParseExact(value, DateFormat ?? Culture.DateTimeFormat.ShortDatePattern, Culture, DateTimeStyles.None, out DateTime parsedValue))
//{
// result = new DateTimeOffset(parsedValue, DateTimeOffset.Now.Offset);
// result = new DateTimeOffset(parsedValue, _timeZone.GetUtcOffset(parsedValue));
// validationErrorMessage = null;
// return true;
//}
Expand Down Expand Up @@ -740,6 +748,7 @@ private void HandleOnValueChanged(object? sender, EventArgs args)

private void OnSetParameters()
{
_timeZone = TimeZone ?? TimeZoneInfo.Local;
_culture = Culture ?? CultureInfo.CurrentUICulture;

if (CurrentValue is not null)
Expand Down Expand Up @@ -835,7 +844,7 @@ private async Task SelectDate(DateTime selectedDate)
selectedDate = selectedDate.AddHours(hour);
selectedDate = selectedDate.AddMinutes(minute);

var selectedDateTimeOffset = new DateTimeOffset(selectedDate, DateTimeOffset.Now.Offset);
var selectedDateTimeOffset = new DateTimeOffset(selectedDate, _timeZone.GetUtcOffset(selectedDate));
if (curValue.StartDate.HasValue is false)
{
curValue.StartDate = selectedDateTimeOffset;
Expand Down Expand Up @@ -1423,12 +1432,13 @@ private string GetMonthCellCssClass(int monthIndex, int todayYear, int todayMont

private DateTimeOffset GetDateTimeOfDayCell(DateTime date)
{
return new(date, DateTimeOffset.Now.Offset);
return new(date, _timeZone.GetUtcOffset(date));
}

private DateTimeOffset GetDateTimeOfMonthCell(int monthIndex)
{
return new(_culture.Calendar.ToDateTime(_currentYear, monthIndex, 1, 0, 0, 0, 0), DateTimeOffset.Now.Offset);
var date = _culture.Calendar.ToDateTime(_currentYear, monthIndex, 1, 0, 0, 0, 0);
return new(date, _timeZone.GetUtcOffset(date));
}

private bool IsBetweenTwoSelectedDate(DateTime date)
Expand Down Expand Up @@ -1495,7 +1505,8 @@ private void UpdateTime()
var month = _culture.Calendar.GetMonth(date.Value.LocalDateTime);
var day = _culture.Calendar.GetDayOfMonth(date.Value.LocalDateTime);

return new DateTimeOffset(_culture.Calendar.ToDateTime(year, month, day, hour, minute, 0, 0), DateTimeOffset.Now.Offset);
var resultDate = _culture.Calendar.ToDateTime(year, month, day, hour, minute, 0, 0);
return new(resultDate, _timeZone.GetUtcOffset(resultDate));
}

private async Task HandleOnHourInputFocus(bool isStartTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,45 @@
</div>
</DemoExample>

<DemoExample Title="Standalone" RazorCode="@example7RazorCode" Id="example7">
<DemoExample Title="TimeZone" RazorCode="@example7RazorCode" CsharpCode="@example7CsharpCode" Id="example7">
<div>
Specifies the timezone used to interpret and display the selected date/time.
<br />
Remeber using this feature in different runtimes needs more investigations, for example,
there are different data available based on the OS the code is running on or different settings
enabled for the project (like InvariantTimezone in the project file,
<BitLink Href="https://learn.microsoft.com/en-us/aspnet/core/blazor/globalization-localization?view=aspnetcore-9.0#timezone-information"
Target="_blank">more info</BitLink>).
</div>
<br /><br />
<div>Defalt (local TimeZone):</div><br/>
<div class="example-content">
<BitDateRangePicker @bind-Value="@timeZoneDateRange1" />
</div>
<br />
<div>Selected date range: from @(timeZoneDateRange1?.StartDate?.ToString() ?? "-") to @(timeZoneDateRange1?.EndDate?.ToString() ?? "-")</div>

@{
TimeZoneInfo? timeZoneInfo = null;
var allTimeZones = TimeZoneInfo.GetSystemTimeZones();
if (allTimeZones.Count > 0)
{
timeZoneInfo = allTimeZones[0];
}
}

@if (timeZoneInfo is not null) {
<br /><br /><br /><br />
<div>"@timeZoneInfo.Id" TimeZone:</div><br/>
<div class="example-content">
<BitDateRangePicker TimeZone="timeZoneInfo" @bind-Value="@timeZoneDateRange2" />
</div>
<br />
<div>Selected date range: from @(timeZoneDateRange2?.StartDate?.ToString() ?? "-") to @(timeZoneDateRange2?.EndDate?.ToString() ?? "-")</div>
}
</DemoExample>

<DemoExample Title="Standalone" RazorCode="@example8RazorCode" Id="example8">
<div>Use the BitDateRangePicker in a standalone mode, allowing it to function independently without a surrounding form or container.</div>
<br />
<div class="example-content-standalone">
Expand All @@ -144,7 +182,7 @@
</div>
</DemoExample>

<DemoExample Title="ReadOnly" RazorCode="@example8RazorCode" CsharpCode="@example8CsharpCode" Id="example8">
<DemoExample Title="ReadOnly" RazorCode="@example9RazorCode" CsharpCode="@example9CsharpCode" Id="example9">
<div>The ReadOnly parameter makes the date range picker input non-editable, preventing users from manually changing the time value.</div>
<br />
<div class="example-content">
Expand All @@ -160,7 +198,7 @@
</div>
</DemoExample>

<DemoExample Title="Templates" RazorCode="@example9RazorCode" CsharpCode="@example9CsharpCode" Id="example9">
<DemoExample Title="Templates" RazorCode="@example10RazorCode" CsharpCode="@example10CsharpCode" Id="example10">
<div>Utilize various templates within the BitDateRangePicker, such as custom label templates, day cell templates, and year cell templates.</div>
<br />
<div class="example-content">
Expand Down Expand Up @@ -202,7 +240,7 @@
</div>
</DemoExample>

<DemoExample Title="Responsive" RazorCode="@example10RazorCode" Id="example10">
<DemoExample Title="Responsive" RazorCode="@example11RazorCode" Id="example11">
<div>Enable responsive design for the BitDateRangePicker, allowing it to adjust its layout and appearance based on the screen size.</div>
<br />
<div class="example-content">
Expand All @@ -213,7 +251,7 @@
</div>
</DemoExample>

<DemoExample Title="Style & Class" RazorCode="@example11RazorCode" CsharpCode="@example11CsharpCode" Id="example11">
<DemoExample Title="Style & Class" RazorCode="@example12RazorCode" CsharpCode="@example12CsharpCode" Id="example12">
<div>Explore styling and class customization for BitDateRangePicker, including component styles, custom classes, and detailed styles.</div>
<br /><br />
<div>Component's Style & Class:</div>
Expand Down Expand Up @@ -270,7 +308,7 @@
</div>
</DemoExample>

<DemoExample Title="RTL" RazorCode="@example12RazorCode" Id="example12">
<DemoExample Title="RTL" RazorCode="@example13RazorCode" Id="example13">
<div>Use BitDateRangePicker in right-to-left (RTL).</div>
<br />
<div dir="rtl">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ public partial class BitDateRangePickerDemo
Href = "#time-format-enum",
},
new()
{
Name = "TimeZone",
Type = "TimeZoneInfo?",
DefaultValue = "null",
Description = "TimeZone for the DateRangePicker."
},
new()
{
Name = "Underlined",
Type = "bool",
Expand Down Expand Up @@ -1009,4 +1016,7 @@ public partial class BitDateRangePickerDemo
StartDate = new DateTimeOffset(2024, 12, 8, 12, 15, 0, DateTimeOffset.Now.Offset),
EndDate = new DateTimeOffset(2024, 12, 12, 16, 45, 0, DateTimeOffset.Now.Offset),
};

private BitDateRangePickerValue? timeZoneDateRange1 = new();
private BitDateRangePickerValue? timeZoneDateRange2 = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,47 @@ public partial class BitDateRangePickerDemo
Culture=""CultureInfoHelper.GetFaIrCultureWithFingilishNames()"" />";

private readonly string example7RazorCode = @"
<BitDateRangePicker @bind-Value=""@timeZoneDateRange1"" />
<div>Selected date range: from @(timeZoneDateRange1?.StartDate?.ToString() ?? ""-"") to @(timeZoneDateRange1?.EndDate?.ToString() ?? ""-"")</div>

@{
TimeZoneInfo? timeZoneInfo = null;
var allTimeZones = TimeZoneInfo.GetSystemTimeZones();
if (allTimeZones.Count > 0)
{
timeZoneInfo = allTimeZones[0];
}
}

@if (timeZoneInfo is not null) {
<div>""@timeZoneInfo.Id"" TimeZone:</div><br/>
<BitDateRangePicker TimeZone=""timeZoneInfo"" @bind-Value=""@timeZoneDateRange2"" />
<div>Selected date range: from @(timeZoneDateRange2?.StartDate?.ToString() ?? ""-"") to @(timeZoneDateRange2?.EndDate?.ToString() ?? ""-"")</div>
}";
private readonly string example7CsharpCode = @"
private BitDateRangePickerValue? timeZoneDateRange1 = new();
private BitDateRangePickerValue? timeZoneDateRange2 = new();";

private readonly string example8RazorCode = @"
<BitDateRangePicker Label=""Basic DatePicker"" Standalone />
<BitDateRangePicker Label=""Disabled"" IsEnabled=""false"" Standalone />
<BitDateRangePicker Label=""Week numbers"" ShowWeekNumbers Standalone />
<BitDateRangePicker Label=""Highlight months"" HighlightCurrentMonth HighlightSelectedMonth Standalone />
<BitDateRangePicker Label=""TimePicker"" ShowTimePicker Standalone />";

private readonly string example8RazorCode = @"
private readonly string example9RazorCode = @"
<BitDateRangePicker Label=""Basic"" ReadOnly @bind-Value=""readOnlyDateRange"" />
<BitDateRangePicker Label=""Text input allowed"" ReadOnly AllowTextInput @bind-Value=""readOnlyDateRange"" />
<BitDateRangePicker Label=""Standalone"" ReadOnly Standalone @bind-Value=""readOnlyDateRange"" />
<BitDateRangePicker Label=""Standalone with TimePicker"" ReadOnly ShowTimePicker Standalone @bind-Value=""readOnlyDateRange"" />";
private readonly string example8CsharpCode = @"
private readonly string example9CsharpCode = @"
private BitDateRangePickerValue? readOnlyDateRange = new()
{
StartDate = new DateTimeOffset(2024, 12, 8, 12, 15, 0, DateTimeOffset.Now.Offset),
EndDate = new DateTimeOffset(2024, 12, 12, 16, 45, 0, DateTimeOffset.Now.Offset),
};";

private readonly string example9RazorCode = @"
private readonly string example10RazorCode = @"
<BitDateRangePicker>
<LabelTemplate>
Custom label <BitIcon IconName=""@BitIconName.Calendar"" />
Expand Down Expand Up @@ -117,16 +139,16 @@ public partial class BitDateRangePickerDemo
</span>
</YearCellTemplate>
</BitDateRangePicker>";
private readonly string example9CsharpCode = @"
private readonly string example10CsharpCode = @"
private CultureInfo culture = CultureInfo.CurrentUICulture;";

private readonly string example10RazorCode = @"
private readonly string example11RazorCode = @"
<BitDateRangePicker Label=""Responsive DateRangePicker""
Responsive
ShowWeekNumbers
Placeholder=""Select a date range"" />";

private readonly string example11RazorCode = @"
private readonly string example12RazorCode = @"
<style>
.custom-class {
overflow: hidden;
Expand Down Expand Up @@ -311,9 +333,9 @@ public partial class BitDateRangePickerDemo
DayPickerHeader = ""custom-day-header"",
WeekNumbersHeader = ""custom-week-header"",
YearMonthPickerWrapper = ""custom-year-picker"" })"" />";
private readonly string example11CsharpCode = @"
private readonly string example12CsharpCode = @"
private BitDateRangePickerValue? classesValue;";

private readonly string example12RazorCode = @"
private readonly string example13RazorCode = @"
<BitDateRangePicker Dir=""BitDir.Rtl"" />";
}
Loading