Skip to content

Instantly share code, notes, and snippets.

@takase1121
Created October 7, 2018 07:46
Show Gist options
  • Save takase1121/e3121f99d9ed5ead093ee450ca29379b to your computer and use it in GitHub Desktop.
Save takase1121/e3121f99d9ed5ead093ee450ca29379b to your computer and use it in GitHub Desktop.
A small Rust program to change timezone to JST. Uses tzutil.
//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(&current_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.");
}
}
@takase1121
Copy link
Author

I am still new to Rust, so if anyone have suggestions to improve this please say it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment