Created
October 7, 2018 07:46
-
-
Save takase1121/e3121f99d9ed5ead093ee450ca29379b to your computer and use it in GitHub Desktop.
A small Rust program to change timezone to JST. Uses tzutil.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//A small program to change/revert timezone so I can play kancolle. | |
use std::fs; | |
use std::ffi::OsStr; | |
use std::process::Command; | |
fn main() { | |
//if .tzdata exists | |
if fs::read_dir(".").unwrap().any(|x|x.unwrap().file_name() == OsStr::new(".tzdata")) { | |
let last_tz = fs::read_to_string(".tzdata").unwrap(); | |
let current_tz = Command::new("tzutil") | |
.arg("/g") | |
.output() | |
.unwrap() | |
.stdout; | |
//if the original tz is different than the current (changed previously) then change back | |
if last_tz != String::from_utf8_lossy(¤t_tz) { | |
//set the tz back | |
let last_tz = last_tz.as_str(); | |
let res = Command::new("tzutil") | |
.args(&["/s", last_tz]) | |
.output() | |
.unwrap() | |
.stdout; | |
let res = String::from_utf8_lossy(&res); | |
if res == "" { | |
println!("Done reverting timezone."); | |
} else { | |
println!("Failed to revert timezone."); | |
} | |
} else { | |
//switch to tokyo's | |
let res = Command::new("tzutil") | |
.args(&["/s", "Tokyo Standard Time"]) | |
.output() | |
.unwrap() | |
.stdout; | |
let res = String::from_utf8_lossy(&res); | |
if res == "" { | |
println!("Done setting the timezone to Tokyo Standard Time."); | |
} else { | |
println!("Failed to set timezone."); | |
} | |
} | |
} else { | |
//run tzutil to get stuff and put it into a file | |
let cmd_out = Command::new("tzutil") | |
.arg("/g") | |
.output() | |
.unwrap() | |
.stdout; | |
fs::write(".tzdata", cmd_out).unwrap(); | |
//run a command to change tz | |
Command::new("tzutil") | |
.args(&["/s", "Tokyo Standard Time"]) | |
.spawn() | |
.unwrap(); | |
//tell people about it | |
println!("Done setting the timezone to Tokyo Standard Time."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am still new to Rust, so if anyone have suggestions to improve this please say it out!