@@ -149,6 +149,8 @@ fn _chrono(input: &str) -> String {
149
149
150
150
#[ cfg( test) ]
151
151
mod converter_tests {
152
+ use chrono:: DateTime ;
153
+
152
154
#[ test]
153
155
fn test_new ( ) {
154
156
match super :: Converter :: new ( Some ( "Random/str" ) , None ) {
@@ -182,6 +184,32 @@ mod converter_tests {
182
184
outputs : Vec < & ' a str > ,
183
185
} ;
184
186
187
+ fn convert_utc_to_localtimezone ( input : & str , format : & str ) -> String {
188
+ use chrono:: TimeZone ;
189
+
190
+ let local_timezone = super :: Local :: now ( ) . timezone ( ) ;
191
+ return super :: Utc
192
+ . datetime_from_str ( input, format)
193
+ . unwrap ( )
194
+ . with_timezone ( & local_timezone)
195
+ . format ( format)
196
+ . to_string ( ) ;
197
+ } ;
198
+
199
+ fn convert_to_localtimezone ( input : & str , format : & str ) -> String {
200
+ let local_timezone = super :: Local :: now ( ) . timezone ( ) ;
201
+ return DateTime :: parse_from_str ( input, format)
202
+ . unwrap ( )
203
+ . with_timezone ( & local_timezone)
204
+ . format ( format)
205
+ . to_string ( ) ;
206
+ } ;
207
+
208
+ let local_timezone_case_1 =
209
+ convert_utc_to_localtimezone ( "2002-10-02 15:00:00" , "%Y-%m-%d %H:%M:%S" ) ;
210
+ let local_timezone_case_2 =
211
+ convert_to_localtimezone ( "2012-07-24T23:14:29-0700" , "%Y-%m-%dT%H:%M:%S%z" ) ;
212
+
185
213
let testcases = vec ! [
186
214
TestCase {
187
215
timezone: Some ( "Asia/Kolkata" ) ,
@@ -197,9 +225,8 @@ mod converter_tests {
197
225
"2018-08-08 18:02:15 +0530" ,
198
226
] ,
199
227
} ,
200
- // This test can pass only if local timezone is Asia/Kolkata
201
228
TestCase {
202
- timezone: None ,
229
+ timezone: Some ( "Asia/Kolkata" ) ,
203
230
format: Some ( "%Y-%m-%d %H:%M:%S" ) ,
204
231
inputs: vec![ "2018-11-03 22:39:33 Some random log" ] ,
205
232
outputs: vec![ "2018-11-04 04:09:33 Some random log" ] ,
@@ -238,18 +265,8 @@ mod converter_tests {
238
265
TestCase {
239
266
timezone: None ,
240
267
format: None ,
241
- inputs: vec![
242
- "Fri, 28 Nov 2014 12:00:09 +0300" ,
243
- "2012-07-24T23:14:29-0700" ,
244
- "2002-10-02T15:00:00Z" ,
245
- "2002-10-02 15:00:00" ,
246
- ] ,
247
- outputs: vec![
248
- "Fri, 28 Nov 2014 14:30:09 +0530" ,
249
- "2012-07-25T11:44:29+0530" ,
250
- "2002-10-02T20:30:00Z" ,
251
- "2002-10-02 20:30:00" ,
252
- ] ,
268
+ inputs: vec![ "2002-10-02 15:00:00" , "2012-07-24T23:14:29-0700" ] ,
269
+ outputs: vec![ & local_timezone_case_1, & local_timezone_case_2] ,
253
270
} ,
254
271
] ;
255
272
0 commit comments