项目作者: yoshihitoh

项目描述 :
A command line tool to handle a unix timestamp.
高级语言: Rust
项目地址: git://github.com/yoshihitoh/ut-cli.git
创建时间: 2019-06-19T14:23:46Z
项目社区:https://github.com/yoshihitoh/ut-cli

开源协议:MIT License

下载


ut

ut is a command line tool to handle a unix timestamp.

Latest Version
ci
Dependabot

Motivation

There is a number of times to generate/parse unix timestamps.
I think date command exists to handle these situations. But there are a few problems that they are small, but vital for me.

  • cannot use same options between macOS and Linux.
  • hard to remember usage. (it might be happen because of above problem.)

That’s why I made a new command line tool ut-cli.

I hope ut-cli works well when developers need to use the command which requires timestamps like aws-cli.

Example usage

Search logs from specific time period.

  1. # from yesterday to today
  2. $ aws logs filter-log-events \
  3. --log-group-name <LOG_GROUP_NAME> \
  4. --log-stream-names <LOG_STREAM_NAMES> \
  5. --query <QUERY> \
  6. --start-time $(ut -p ms g -b yesterday) \
  7. --end-time $(ut -p ms g -b today)

Installation

If you have rust toolchain, ut-cli can be installed with cargo.

  1. $ cargo install ut-cli

or clone the repository and build it.

  1. $ git clone https://github.com/yoshihitoh/ut-cli
  2. $ cd ut-cli
  3. $ cargo build --release
  4. $ ./target/release/ut --version
  5. ut 0.1.7

Also there are pre-built binary for Linux, macOS and Windows.
See releases.

Usage

  1. ut-cli 0.1.7
  2. yoshihitoh <yoshihito.arih@gmail.com>
  3. A command line tool to handle unix timestamp.
  4. USAGE:
  5. ut [FLAGS] [OPTIONS] <SUBCOMMAND>
  6. FLAGS:
  7. -u, --utc Use utc timezone.
  8. -h, --help Prints help information
  9. -V, --version Prints version information
  10. OPTIONS:
  11. -o, --offset <OFFSET> Use given value as timezone offset.
  12. -p, --precision <PRECISION>
  13. Set the precision of output timestamp.
  14. SUBCOMMANDS:
  15. generate Generate unix timestamp with given options.
  16. help Prints this message or the help of the given subcommand(s)
  17. parse Parse a unix timestamp and print it in human readable format.

You can set options via envrionment variables.

name equiv option example
UT_OFFSET -o/—offset 09:00
UT_PRECISION -p/—precision millisecond
UT_DATETIME_FORMAT - %Y-%m-%d %H:%M

UT_DATETIME_FORMAT follows chrono’s datetime specifiers.
See the document for details.

  1. # Set variables.
  2. $ export UT_OFFSET='09:00' # Use JST(+9).
  3. $ export UT_PRECISION=millisecond # Use timestamps in milliseconds.
  4. # Generate a timestamp.
  5. $ ut g
  6. 1588059756238
  7. # Parse a timestamp.
  8. $ echo 1588059756238 | ut p
  9. 2020-04-28 16:42:36.238 (+09:00)
  10. # Change custom format and timezone.
  11. $ export UT_DATETIME_FORMAT="%m/%d/%Y"
  12. $ echo 1588059756238 | ut --offset=-7 p
  13. 04/28/2020

is equivalent to

  1. $ ut -o '09:00' -p millisecond p $(ut -o '09:00' -p millisecond g)

There are two subcommands available for now.

Generate a unix timestamp

Generate a unix timestamp of the midnight of today.

  1. $ ut generate -b today
  2. 1560870000
  3. # You can use `-p` option to show it in millisecond.
  4. $ ut -p ms generate -b today
  5. 1560870000000

You can specify time deltas with -d option.

  1. # 3days, 12hours, 30minutes later from the midnight of today.
  2. $ ut g -b today -d 3day -d 12hour -d 30minute
  3. 1561174200
  4. # You can use short name on time unit.
  5. $ ut g -b today -d 3d -d 12h -d 30min
  6. 1561174200
  7. # You can modify a timestamp with a timestamp argument.
  8. $ ut g -d 1min 1561174200
  9. 1561174260 # 1min(=60second) difference.

Parse a unix timestamp

Parse a unix timestamp and print it in human readable format.

  1. $ ut p $(ut g -b today)
  2. 2019-06-19 00:00:00 (+09:00)
  3. # You can parse timestamp in milliseconds.
  4. $ ut -p ms p $(ut -p ms g -b today -d 11h -d 22min -d 33s -d 444ms)
  5. 2019-06-19 11:22:33.444 (+09:00)

Change timezone

Local timezone

If you don’t set timezone options, ut command uses local timezone.

In Japan(UTC+9):

  1. $ ut g --ymd 2019-06-24
  2. 1561302000
  3. $ ut p 1561302000
  4. 2019-06-24 00:00:00 (+09:00)

You can use -u or --utc option to use UTC timezone.

  1. $ ut --utc p 1561302000
  2. 2019-06-23 15:00:00 (UTC)

You can use fixed offset timezone on any environment.

  1. # Generate PST timestamp
  2. $ ut -o -8 g --ymd 2019-06-24
  3. 1561363200
  4. # Parse as PST timestamp
  5. $ ut -o -8 p 1561363200
  6. 2019-06-24 00:00:00 (-08:00)
  7. # Parse as UTC timestamp
  8. $ ut -o 0 p 1561363200
  9. 2019-06-24 08:00:00 (+00:00)

TODO

  • Add more information on README