@@ -609,7 +609,7 @@ public static class LootLockerTimezoneConverter
609609 { "Etc/GMT-14" , "Line Islands Standard Time" } ,
610610 } ;
611611
612- public static string ConvertWindowsToIana ( string windowsTimezone )
612+ public static string ConvertWindowsToIanaTzString ( string windowsTimezone )
613613 {
614614 if ( windowsTimezone == null )
615615 {
@@ -624,7 +624,7 @@ public static string ConvertWindowsToIana(string windowsTimezone)
624624 return "Etc/UTC" ;
625625 }
626626
627- public static string ConvertIANAToWindows ( string ianaTimezone )
627+ public static string ConvertIANAToWindowsTzString ( string ianaTimezone )
628628 {
629629 // Reverse dictionary for IANA to Windows
630630 var ianaToWindows = WindowsToIana . ToDictionary ( x => x . Value , x => x . Key ) ;
@@ -637,32 +637,103 @@ public static string ConvertIANAToWindows(string ianaTimezone)
637637 return "UTC" ;
638638 }
639639
640- // Function to convert to IANA timezone
641- public static string ConvertToIana ( string timezone )
640+ public static bool isValidIanaTimezone ( string timezone )
642641 {
643- if ( string . IsNullOrEmpty ( timezone ) )
644- return "Etc/UTC" ; // Default fallback
642+ return IanaToWindows . ContainsKey ( timezone ) ;
643+ }
644+
645+ public static bool isValidWindowsTimezone ( string timezone )
646+ {
647+ return WindowsToIana . ContainsKey ( timezone ) ;
648+ }
645649
646- // Check if it's already an IANA timezone
647- if ( WindowsToIana . ContainsValue ( timezone ) )
648- return timezone ;
650+ public static string convertUTCOffsetToIanaTzString ( int utcOffset )
651+ {
652+ utcOffset = Math . Clamp ( utcOffset , - 12 , 14 ) ;
653+ if ( utcOffset < 0 )
654+ {
655+ return $ "Etc/GMT+{ Math . Abs ( utcOffset ) } "; // Note the reversed sign for Etc/GMT
656+ }
657+ else if ( utcOffset > 0 )
658+ {
659+ return $ "Etc/GMT-{ utcOffset } "; // Note the reversed sign for Etc/GMT
660+ }
661+ else // utcOffset == 0
662+ {
663+ return "Etc/UTC" ;
664+ }
665+ }
649666
650- // Attempt to convert from Windows to IANA
651- return ConvertWindowsToIana ( timezone ) ;
667+ public static string convertGMTOffsetToIanaTzString ( int gmtOffset )
668+ {
669+ gmtOffset = Math . Clamp ( gmtOffset , - 14 , 12 ) ;
670+ if ( gmtOffset < 0 )
671+ {
672+ return $ "Etc/GMT-{ Math . Abs ( gmtOffset ) } ";
673+ }
674+ else if ( gmtOffset > 0 )
675+ {
676+ return $ "Etc/GMT+{ gmtOffset } ";
677+ }
678+ else // gmtOffset == 0
679+ {
680+ return "Etc/UTC" ;
681+ }
652682 }
653683
654- // Function to convert to Windows timezone
655- public static string ConvertToWindows ( string timezone )
684+ public static bool TryConvertStringToIanaTzString ( string timezone , out string ianaTimezone )
656685 {
657686 if ( string . IsNullOrEmpty ( timezone ) )
658- return "UTC" ; // Default fallback
687+ {
688+ ianaTimezone = "Etc/UTC" ; // Default fallback
689+ return false ;
690+ }
659691
660- // Check if it's already a Windows timezone
661- if ( WindowsToIana . ContainsKey ( timezone ) )
662- return timezone ;
692+ if ( int . TryParse ( timezone , out int offset ) )
693+ {
694+ ianaTimezone = convertUTCOffsetToIanaTzString ( offset ) ;
695+ return true ;
696+ }
697+ else if ( isValidIanaTimezone ( timezone ) )
698+ {
699+ ianaTimezone = timezone ;
700+ return true ;
701+ }
702+ else if ( isValidWindowsTimezone ( timezone ) )
703+ {
704+ ianaTimezone = ConvertWindowsToIana ( timezone ) ;
705+ return true ;
706+ }
707+ else
708+ {
709+ ianaTimezone = "Etc/UTC" ; // Default fallback
710+ return false ;
711+ }
712+ }
663713
664- // Attempt to convert from IANA to Windows
665- return ConvertIANAToWindows ( timezone ) ;
714+ public static bool TryConvertStringToWindowsTzString ( string timezone , out string windowsTimezone )
715+ {
716+ if ( string . IsNullOrEmpty ( timezone ) )
717+ {
718+ windowsTimezone = "UTC" ; // Default fallback
719+ return false ;
720+ }
721+
722+ else if ( isValidIanaTimezone ( timezone ) )
723+ {
724+ windowsTimezone = ConvertIANAToWindows ( timezone ) ;
725+ return true ;
726+ }
727+ else if ( isValidWindowsTimezone ( timezone ) )
728+ {
729+ windowsTimezone = timezone ;
730+ return true ;
731+ }
732+ else
733+ {
734+ windowsTimezone = "UTC" ; // Default fallback
735+ return false ;
736+ }
666737 }
667738 }
668739}
0 commit comments