@@ -3,7 +3,6 @@ use chrono_tz::Tz;
3
3
use minijinja:: { Error , ErrorKind , Value } ;
4
4
use std:: collections:: BTreeMap ;
5
5
use std:: fmt;
6
- use std:: str:: FromStr ;
7
6
use std:: sync:: Arc ;
8
7
9
8
/// A Python-like "pytz" timezone object that wraps a `chrono_tz::Tz`.
@@ -81,7 +80,7 @@ fn timezone(args: &[Value]) -> Result<Value, Error> {
81
80
} ) ?;
82
81
83
82
// Try to parse it as a Chrono Tz.
84
- match Tz :: from_str ( tz_name) {
83
+ match Tz :: from_str_insensitive ( tz_name) {
85
84
Ok ( tz) => Ok ( Value :: from_object ( PytzTimezone :: new ( tz) ) ) ,
86
85
Err ( _) => Err ( Error :: new (
87
86
ErrorKind :: InvalidArgument ,
@@ -116,3 +115,42 @@ impl minijinja::value::Object for PytzTimezone {
116
115
write ! ( f, "{}" , self . tz)
117
116
}
118
117
}
118
+
119
+ #[ cfg( test) ]
120
+ mod tests {
121
+ use crate :: modules:: pytz:: { timezone, PytzTimezone } ;
122
+ use minijinja:: Value ;
123
+ use chrono_tz:: Tz ;
124
+
125
+ #[ test]
126
+ fn test_valid_timezone_utc ( ) {
127
+ let args = vec ! [ Value :: from( "UTC" ) ] ;
128
+ let result = timezone ( & args) ;
129
+ assert ! ( result. is_ok( ) ) ;
130
+
131
+ let actual = result. unwrap ( ) ;
132
+ let expected = Value :: from_object ( PytzTimezone :: new ( chrono_tz:: UTC ) ) ;
133
+ assert_eq ! ( actual, expected) ;
134
+ }
135
+
136
+ #[ test]
137
+ fn test_valid_timezone_case_insensitive ( ) {
138
+ let args = vec ! [ Value :: from( "asia/ho_chi_minh" ) ] ;
139
+ let result = timezone ( & args) ;
140
+ assert ! ( result. is_ok( ) ) ;
141
+
142
+ let actual = result. unwrap ( ) ;
143
+ let expected = Value :: from_object ( PytzTimezone :: new ( Tz :: Asia__Ho_Chi_Minh ) ) ;
144
+ assert_eq ! ( actual, expected) ;
145
+ }
146
+
147
+ #[ test]
148
+ fn test_invalid_timezone ( ) {
149
+ let args = vec ! [ Value :: from( "Invalid/Zone" ) ] ;
150
+ let result = timezone ( & args) ;
151
+ assert ! ( result. is_err( ) ) ;
152
+
153
+ let err = result. unwrap_err ( ) ;
154
+ assert ! ( err. to_string( ) . contains( "Invalid timezone name" ) ) ;
155
+ }
156
+ }
0 commit comments