-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0174b07
Showing
19 changed files
with
2,434 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
.idea/ |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "ut" | ||
version = "0.1.0" | ||
authors = ["yoshihitoh <yoshihito.arih@gmail.com>"] | ||
edition = "2018" | ||
|
||
[workspace] | ||
members = [ | ||
"timedelta", | ||
] | ||
|
||
[dependencies] | ||
chrono = "^0.4" | ||
clap = "^2.33" | ||
failure = "^0.1" | ||
regex = "^1" | ||
strum = "^0.15" | ||
strum_macros = "^0.15" | ||
timedelta = {version = "^0.1.0", "path" = "timedelta"} | ||
lazy_static = "^1.3" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Yoshihito | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
ut | ||
---- | ||
|
||
ut is a command line tool to handle a unix timestamp. | ||
|
||
### Installation | ||
|
||
clone the repository and build it. | ||
|
||
``` bash | ||
$ git clone https://github.com/yoshihitoh/ut | ||
$ cd ut | ||
$ cargo build --release | ||
$ ./target/relase ut --version | ||
ut 0.1.0 | ||
``` | ||
|
||
### Usage | ||
|
||
#### Generate a unix timestamp | ||
|
||
Generate a unix timestamp of the midnight of today. | ||
``` bash | ||
$ ut generate -b today | ||
1560870000 | ||
|
||
# You can use `-p` option to show it in millisecond. | ||
$ ut generate -b today -p ms | ||
1560870000000 | ||
``` | ||
|
||
You can specify time deltas with `-d` option. | ||
``` bash | ||
# 3days, 12hours, 30minutes later from the midnight of today. | ||
$ ut g -b today -d 3day -d 12hour -d 30minute | ||
1561174200 | ||
|
||
# You can use short name on time unit. | ||
$ ut g -b today -d 3d -d 12h -d 30min | ||
1561174200 | ||
``` | ||
|
||
#### Parse a unix timestamp | ||
|
||
Parse a unix timestamp and print it in human readable format. | ||
``` bash | ||
$ ut p $(ut g -b today) | ||
2019-06-19 00:00:00 (+09:00) | ||
|
||
# You can parse timestamp in milliseconds. | ||
$ ut p -p ms $(ut g -b today -p ms -d 11h -d 22min -d 33s -d 444ms) | ||
2019-06-19 11:22:33.444 (+09:00) | ||
``` | ||
|
||
### TODO | ||
- Add more information on README | ||
- CI/CD |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod generate; | ||
pub mod parse; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
use std::convert::TryFrom; | ||
|
||
use chrono::{Local, TimeZone, Utc}; | ||
use clap::{App, Arg, ArgMatches, SubCommand}; | ||
use regex::Regex; | ||
|
||
use timedelta::{ApplyDateTime, TimeDeltaBuilder}; | ||
|
||
use crate::error::{UtError, UtErrorKind}; | ||
|
||
mod request; | ||
use request::Request; | ||
|
||
fn validate_number(field_name: &str, min: i32, max: i32, text: &str) -> Result<(), String> { | ||
let number = text | ||
.parse::<i32>() | ||
.map_err(|_| format!("{} is not a number.", text))?; | ||
|
||
if number >= min && number <= max { | ||
Ok(()) | ||
} else { | ||
Err(format!( | ||
"{} must be between {} and {} . given {}: {}", | ||
field_name, min, max, field_name, text | ||
)) | ||
} | ||
} | ||
|
||
fn validate_ymd(ymd: String) -> Result<(), String> { | ||
let re = Regex::new(r"(\d{4})(\d{2})(\d{2})").expect("wrong regex pattern"); | ||
let caps = re.captures(&ymd).ok_or(format!( | ||
"format must be \"yyyyMMdd\". given format: {}", | ||
ymd | ||
))?; | ||
|
||
let y = caps.get(1).unwrap().as_str(); | ||
validate_number("year", 1900, 2999, y)?; | ||
|
||
let m = caps.get(2).unwrap().as_str(); | ||
validate_number("month", 1, 12, m)?; | ||
|
||
let d = caps.get(3).unwrap().as_str(); | ||
validate_number("day", 1, 31, d)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn validate_hms(hms: String) -> Result<(), String> { | ||
let re = Regex::new(r"(\d{2})(\d{2})(\d{2})").expect("wrong regex pattern"); | ||
let caps = re | ||
.captures(&hms) | ||
.ok_or(format!("format must be \"HHmmss\". given format: {}", hms))?; | ||
|
||
let h = caps.get(1).unwrap().as_str(); | ||
validate_number("hour", 0, 23, h)?; | ||
|
||
let m = caps.get(2).unwrap().as_str(); | ||
validate_number("minute", 0, 59, m)?; | ||
|
||
let s = caps.get(3).unwrap().as_str(); | ||
validate_number("second", 0, 59, s)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn command(name: &str) -> App<'static, 'static> { | ||
SubCommand::with_name(name) | ||
.about("Generate unix timestamp with given options.") | ||
.arg( | ||
Arg::with_name("BASE") | ||
.value_name("DATE") | ||
.help("Set base DATE from presets.") | ||
.next_line_help(true) | ||
.short("b") | ||
.long("base") | ||
.takes_value(true) | ||
.conflicts_with("YMD"), | ||
) | ||
.arg( | ||
Arg::with_name("YMD") | ||
.value_name("DATE") | ||
.help("Set the DATE in yyyyMMdd format.") | ||
.long("ymd") | ||
.takes_value(true) | ||
.validator(validate_ymd) | ||
.conflicts_with("BASE"), | ||
) | ||
.arg( | ||
Arg::with_name("HMS") | ||
.value_name("TIME") | ||
.help("Set the TIME in HHmmss format.") | ||
.long("hms") | ||
.takes_value(true) | ||
.validator(validate_hms), | ||
) | ||
.arg( | ||
Arg::with_name("TRUNCATE") | ||
.value_name("UNIT") | ||
.help("Set the UNIT to truncate the base DATE and TIME.") | ||
.next_line_help(true) | ||
.short("t") | ||
.long("truncate") | ||
.takes_value(true), | ||
) | ||
.arg( | ||
Arg::with_name("DELTA") | ||
.help("Set the timedelta consists of VALUE and UNIT.") | ||
.long_help( | ||
" | ||
Example: | ||
--delta=3day : 3 days later. | ||
-d 1y -d -10h : 10 hours ago in next year. | ||
", | ||
) | ||
.next_line_help(true) | ||
// TODO: long helpに使用例を追加 | ||
.short("d") | ||
.long("delta") | ||
.takes_value(true) | ||
.allow_hyphen_values(true) | ||
.multiple(true) | ||
.number_of_values(1), | ||
) | ||
.arg( | ||
Arg::with_name("PRECISION") | ||
.help("Set the precision of output timestamp.") | ||
.next_line_help(true) | ||
.short("p") | ||
.long("precision") | ||
.takes_value(true) | ||
.default_value("second"), | ||
) | ||
.arg( | ||
Arg::with_name("UTC") | ||
.help("Use utc date and time on given options relate to date and time.") | ||
.short("u") | ||
.long("utc"), | ||
) | ||
} | ||
|
||
fn generate<Tz: TimeZone>(request: Request<Tz>) -> Result<(), UtError> { | ||
let delta = request | ||
.deltas() | ||
.into_iter() | ||
.fold(TimeDeltaBuilder::default(), |b, d| { | ||
d.apply_timedelta_builder(b) | ||
}) | ||
.build(); | ||
|
||
match delta.apply_datetime(request.base()) { | ||
Some(dt) => { | ||
println!("{}", request.precision().to_timestamp(dt)); | ||
Ok(()) | ||
} | ||
None => Err(UtError::from(UtErrorKind::TimeUnitError)), | ||
} | ||
} | ||
|
||
pub fn run(m: &ArgMatches<'static>) -> Result<(), UtError> { | ||
match m.value_of("UTC") { | ||
Some(_) => generate(Request::<Utc>::try_from(m)?), | ||
None => generate(Request::<Local>::try_from(m)?), | ||
} | ||
} |
Oops, something went wrong.