Skip to content
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
80 changes: 40 additions & 40 deletions commands/convert-group/convertCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,47 +84,47 @@ public async Task SuggestUnitModalHandler(string type, SuggestUnitModal modal)
await Context.Interaction.ModifyOriginalResponseAsync(x => { x.Content = "✅ Suggestion made successfully!\n- This will be manually reviewed as soon as possible.\n- Thanks for the idea!"; x.Components = null; });
}

// [SlashCommand("timezones", "Convert time from one timezone to another.")]
// public async Task ConvertTime(
// [Summary("month", "The month for the time you want to convert.")][MinValue(1)][MaxValue(12)] int month,
// [Summary("day", "The day for the time you want to convert.")][MinValue(1)][MaxValue(31)] int day,
// [Summary("hour", "The hour for the time you want to convert, in 24-hour format.")][MinValue(0)][MaxValue(23)] int hour,
// [Summary("minute", "The minute for the time you want to convert.")][MinValue(0)][MaxValue(59)] int minute,
// [Summary("from-timezone", "The timezone to convert from.")] Timezone sourceTimezone,
// [Summary("to-timezone", "The timezone you want to convert to.")] Timezone destinationTimezone)
// {
// try
// {
// // Validate the day based on the month and current year
// if (day < 1 || day > DateTime.DaysInMonth(DateTime.UtcNow.Year, month))
// {
// await RespondAsync($"❌ Please enter a valid day between **1** and **{DateTime.DaysInMonth(DateTime.UtcNow.Year, month)}**.", ephemeral: true);
// return;
// }
[SlashCommand("timezones", "Convert time from one timezone to another.")]
public async Task ConvertTime(
[Summary("month", "The month for the time you want to convert.")][MinValue(1)][MaxValue(12)] int month,
[Summary("day", "The day for the time you want to convert.")][MinValue(1)][MaxValue(31)] int day,
[Summary("hour", "The hour for the time you want to convert, in 24-hour format.")][MinValue(0)][MaxValue(23)] int hour,
[Summary("minute", "The minute for the time you want to convert.")][MinValue(0)][MaxValue(59)] int minute,
[Summary("from-timezone", "The timezone to convert from.")] Timezone sourceTimezone,
[Summary("to-timezone", "The timezone you want to convert to.")] Timezone destinationTimezone)
{
try
{
// Validate the day based on the month and current year
if (day < 1 || day > DateTime.DaysInMonth(DateTime.UtcNow.Year, month))
{
await RespondAsync($"❌ Please enter a valid day between **1** and **{DateTime.DaysInMonth(DateTime.UtcNow.Year, month)}**.", ephemeral: true);
return;
}

// var sourceDateTime = new DateTime(DateTime.UtcNow.Year, month, day, hour, minute, 0, DateTimeKind.Unspecified);
// Console.WriteLine("Source time: " + sourceDateTime);

// var sourceTimestamp = Timestamp.FromDateTime(sourceDateTime, Timestamp.Formats.Exact, sourceTimezone);
// Console.WriteLine("Source timestamp: " + sourceTimestamp);

// var destinationDateTime = TimeConverter.ConvertBetweenTimezones(month, day, hour, minute, sourceTimezone, destinationTimezone);
// Console.WriteLine("Destination time: " + destinationDateTime);

// var destinationTimestamp = Timestamp.FromDateTime(destinationDateTime, Timestamp.Formats.Exact, destinationTimezone);
// Console.WriteLine("Destination timestamp: " + destinationTimestamp);

// await RespondAsync($"{TimeConversion.GetClosestTimeEmoji(destinationDateTime)} {sourceTimestamp} in {sourceTimezone.ToDisplayName()} is {destinationTimestamp} in {destinationTimezone.ToDisplayName()}.");
// }
// catch (TimeZoneNotFoundException)
// {
// await RespondAsync("❌ One of the specified timezones is invalid. Please check your input.", ephemeral: true);
// }
// catch (Exception ex)
// {
// await Logger.HandleUnexpectedError(Context, ex, false);
// }
// }
var sourceDateTime = new DateTime(DateTime.UtcNow.Year, month, day, hour, minute, 0, DateTimeKind.Unspecified);
Console.WriteLine("Source time: " + sourceDateTime);

var sourceTimestamp = Timestamp.FromDateTime(sourceDateTime, Timestamp.Formats.Exact, sourceTimezone);
Console.WriteLine("Source timestamp: " + sourceTimestamp);

var destinationDateTime = TimeConverter.ConvertBetweenTimezones(month, day, hour, minute, sourceTimezone, destinationTimezone);
Console.WriteLine("Destination time: " + destinationDateTime);

var destinationTimestamp = Timestamp.FromDateTime(destinationDateTime, Timestamp.Formats.Exact, destinationTimezone);
Console.WriteLine("Destination timestamp: " + destinationTimestamp);

await RespondAsync($"{TimeConversion.GetClosestTimeEmoji(destinationDateTime)} {sourceTimestamp} in {sourceTimezone.ToDisplayName()} is {destinationTimestamp} in {destinationTimezone.ToDisplayName()}.");
}
catch (TimeZoneNotFoundException)
{
await RespondAsync("❌ One of the specified timezones is invalid. Please check your input.", ephemeral: true);
}
catch (Exception ex)
{
await Logger.HandleUnexpectedError(Context, ex, false);
}
}

[SlashCommand("qr-code", "Bob will convert a link or text to a QR code.")]
public async Task ConvertToQRCode(string content, QRCodeConverter.ErrorCorrectionLevel errorCorrectionLevel = QRCodeConverter.ErrorCorrectionLevel.L)
Expand Down
5 changes: 3 additions & 2 deletions commands/convert-group/helpers/timeConversion.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using TimeZoneConverter;

namespace Bob.Commands.Helpers
{
Expand Down Expand Up @@ -68,10 +69,10 @@ public static string GetClosestTimeEmoji(DateTime time)
TimeSpan closestTime = TimeSpan.Zero;
double smallestDifference = double.MaxValue;

// Find the closest time in the TimeEmojis dictionary.
foreach (var entry in TimeEmojis)
{
double difference = Math.Abs((inputTime - entry.Key).TotalMinutes);
double diff = Math.Abs((inputTime - entry.Key).TotalMinutes);
double difference = Math.Min(diff, 1440 - diff);
if (difference < smallestDifference)
{
smallestDifference = difference;
Expand Down
5 changes: 3 additions & 2 deletions general-helpers/time/TimezoneConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using TimeZoneConverter;

namespace Bob.Time.Timezones
{
Expand Down Expand Up @@ -64,7 +65,7 @@ public static DateTime ConvertToUtcTime(int month, int day, int hour, int minute

// Get the corresponding time zone ID from the TimezoneMappings
var timeZoneId = TimezoneMappings[timezone];
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
var timeZoneInfo = TZConvert.GetTimeZoneInfo(timeZoneId);

// Convert the local time to UTC, treating the input time as being in the specified time zone
var utcDateTime = TimeZoneInfo.ConvertTimeToUtc(localDateTime, timeZoneInfo);
Expand Down Expand Up @@ -95,7 +96,7 @@ public static DateTime ConvertBetweenTimezones(int month, int day, int hour, int
Console.WriteLine("Source time in UTC: " + sourceTimeInUtc);

// Get the destination time zone
var destinationTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(destinationTimezoneId);
var destinationTimeZoneInfo = TZConvert.GetTimeZoneInfo(destinationTimezoneId);

// Convert from UTC to the destination time zone.
return TimeZoneInfo.ConvertTimeFromUtc(sourceTimeInUtc, destinationTimeZoneInfo);
Expand Down
5 changes: 3 additions & 2 deletions general-helpers/time/timestamps.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Bob.Time.Timezones;
using TimeZoneConverter;

namespace Bob.Time.Timestamps
{
Expand Down Expand Up @@ -70,8 +71,8 @@ public static string FromDateTime(DateTime dateTime, Formats format, Timezone? t

// Adjust for the provided timezone if specified
var timeZoneInfo = timeZone.HasValue
? TimeZoneInfo.FindSystemTimeZoneById(TimeConverter.GetTimezoneId(timeZone.Value))
: TimeZoneInfo.Local; // Default to local timezone
? TZConvert.GetTimeZoneInfo(TimeConverter.GetTimezoneId(timeZone.Value)) // fix
: TimeZoneInfo.Local;

// Convert the DateTime to the target timezone
var adjustedDateTime = TimeZoneInfo.ConvertTime(dateTime, timeZoneInfo);
Expand Down
1 change: 1 addition & 0 deletions main.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version=" 8.0.8" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.54" />
<PackageReference Include="TimeZoneConverter" Version="7.2.0" />
<PackageReference Include="UnitsNet" Version="5.60.0" />
<Reference Include="System.Net.Http" />
<PackageReference Include="ColorHelper" Version="1.8.1" />
Expand Down
6 changes: 6 additions & 0 deletions packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
"SkiaSharp": "2.88.6"
}
},
"TimeZoneConverter": {
"type": "Direct",
"requested": "[7.2.0, )",
"resolved": "7.2.0",
"contentHash": "4Poqu79dTmacVETqUJ53g3x4U21V/cmWPSlehdsVE4Q00akavvlRq+SRjcg/CmfO9ibOCjRl6TeUV7hkC5RC4w=="
},
"UnitsNet": {
"type": "Direct",
"requested": "[5.60.0, )",
Expand Down
Loading