Skip to content

Commit 7f12301

Browse files
committed
Remove dependency on local timezone for unit test
1 parent ceea0a6 commit 7f12301

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

.travis.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ matrix:
1111
- rust: nightly
1212
fast_finish: true
1313

14-
env:
15-
global:
16-
- TZ=Asia/Kolkata
17-
18-
before_script:
19-
- echo 'Asia/Kolkata' | sudo tee /etc/timezone
20-
- sudo dpkg-reconfigure --frontend noninteractive tzdata
21-
- sudo ln -snf /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
2214

2315
script:
2416
- date

src/converter.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ fn _chrono(input: &str) -> String {
149149

150150
#[cfg(test)]
151151
mod converter_tests {
152+
use chrono::DateTime;
153+
152154
#[test]
153155
fn test_new() {
154156
match super::Converter::new(Some("Random/str"), None) {
@@ -182,6 +184,32 @@ mod converter_tests {
182184
outputs: Vec<&'a str>,
183185
};
184186

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+
185213
let testcases = vec![
186214
TestCase {
187215
timezone: Some("Asia/Kolkata"),
@@ -197,9 +225,8 @@ mod converter_tests {
197225
"2018-08-08 18:02:15 +0530",
198226
],
199227
},
200-
// This test can pass only if local timezone is Asia/Kolkata
201228
TestCase {
202-
timezone: None,
229+
timezone: Some("Asia/Kolkata"),
203230
format: Some("%Y-%m-%d %H:%M:%S"),
204231
inputs: vec!["2018-11-03 22:39:33 Some random log"],
205232
outputs: vec!["2018-11-04 04:09:33 Some random log"],
@@ -238,18 +265,8 @@ mod converter_tests {
238265
TestCase {
239266
timezone: None,
240267
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],
253270
},
254271
];
255272

0 commit comments

Comments
 (0)