Skip to content

Commit

Permalink
Merge branch 'release-0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
KalenAnson committed Aug 22, 2018
2 parents 1219f65 + 5e8d21f commit 815d384
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
75 changes: 39 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,117 +1,120 @@
# ts
`ts` is a simple UNIX Timestamp utility for the shell. It can get the current UNIX timestamp, convert a timestamp into localized time and diff timestamps.
# tsc
`tsc` is a simple UNIX Timestamp utility for the shell. It can get the current UNIX timestamp, convert a timestamp into localized time and diff timestamps.
## Examples
1. Get current UNIX timestamp: (I realize this is trivial with `date`, but how often have _you_ googled this.)
1. Get current UNIX timestamp: (I realize this is trivial with `date(1)`, but how often have _you_ googled this.)

$ ts
$ tsc
1534947844

2. Convert timestamp to local time:

$ ts 1534950044
$ tsc 1534950044
1534950044 -> Wed Aug 22 11:00:44 EDT 2018

`ts` also accepts input from `stdin` in same format as its arguments (`ts ts ...`)
`tsc` also accepts input from `stdin` in same format as its arguments (`timestamp timestamp ...`)

$ echo 1534947844 | ts
$ echo 1534947844 | tsc
1534947844 -> Wed Aug 22 10:24:04 EDT 2018

3. Diff timestamps (calculates the duration of the smallest and the greatest arguments):

$ ts 1534947844 1534947904
$ tsc 1534947844 1534947904
1534947844 -> Wed Aug 22 10:24:04 EDT 2018
1534947904 -> Wed Aug 22 10:25:04 EDT 2018
Duration: 00:01:00

`ts` is also a first class utility, so it plays nicely with pipelines. Here we diff a timestamp and 'now', calculating the duration in elapsed time.
`tsc` is also a first class utility, so it plays nicely with pipelines. Here we diff a timestamp and 'now', calculating the duration in elapsed time.

$ ts 1534947844 | ts
$ tsc 1534947844 | tsc
1534947844 -> Wed Aug 22 10:24:04 EDT 2018
1534948588 -> Wed Aug 22 10:36:28 EDT 2018
Duration: 00:12:24

4. Convert and diff multiple timestamps:

$ ts 1534947844 1534947904 1534948588
$ tsc 1534947844 1534947904 1534948588
1534947844 -> Wed Aug 22 10:24:04 EDT 2018
1534947904 -> Wed Aug 22 10:25:04 EDT 2018
1534948588 -> Wed Aug 22 10:36:28 EDT 2018
Duration: 00:12:24

## Installation
1. Clone this repo in your projects `/` development directory. I will demonstrate installation using `~/Development/ts`
1. Clone this repo in your projects directory. For demonstration purposes, I will be using `~/Development/tsc` in the example commands below.

git clone https://github.com/kalenanson/ts ~/Development
git clone https://github.com/kalenanson/tsc ~/Development/

2. `ts` needs to be located in your shell's `$PATH`, so you have a couple of options. Either copy the `ts.sh` into a `/bin` directory or create a symbolic link. The first is more secure, the second is more flexible. I'll demonstrate both options and assume that `/usr/local/bin` is already in your shell's path.
2. If you want to add the `tsc.sh` command to your path could either alias the shell script, copt the script into your path, or create a symbolic link in your path. You have several options. Aliasing will need to make it into your dot files to be permanent, copying the file is the most secure and the symbolic link is the most flexiable as the command will stay updated when you update the repo. I'll demonstrate the obvious options and assume that `/usr/local/bin` is already in your shell's path.

# Aliasing (simplest), add to your .bashrc as well
alias tsc=~/Development/tsc/tsc.sh
# OR
# Copy into `/usr/local/bin`
sudo cp ts.sh /usr/local/bin/ts
sudo cp tsc.sh /usr/local/bin/tsc
# OR
# Create a soft link in /usr/local/bin
sudo ln -s ~/Development/ts/ts.sh /usr/local/bin/ts
sudo ln -s ~/Development/tsc/tsc.sh /usr/local/bin/tsc

3. If the above worked then you should now be able to invoke `ts` as follows:
3. If the above worked then you should now be able to invoke `tsc` as follows:

$ ts
$ tsc
1534947844

If you get an error, check your shell's `$PATH` and verify that you see the `ts` link / file in the place you put in #2 above. Also verify permissions on the link / file. It should have something like `755`.
If you get an error, check your shell's `$PATH` and verify that you see the `tsc` link / file in the place you put in #2 above. Also verify permissions on the link / file. It should have something like `755`.

## Usage
### Simple Usage
Simple usage is as follows:

ts -hv [timestamp ...]
tsc -hv [timestamp ...]

Where `-h` and `-v` are for help and version respectively.

When no arguments are passed to `ts` it prints the current UNIX timestamp and exits.
When no arguments are passed to `tsc` it prints the current UNIX timestamp and exits.

When arguments are passed to `ts` it expects one or more timestamps that are space delineated. For example:
When arguments are passed to `tsc` it expects one or more timestamps that are space delineated. For example:

ts 1534947844 1534947904 1534948588
tsc 1534947844 1534947904 1534948588

When passed a single argument, `ts` will attempt to convert the value to a date time using `date(1)`. If successful you should see the timestamp followed by a date. For example:
When passed a single argument, `tsc` will attempt to convert the value to a date time using `date(1)`. If successful you should see the timestamp followed by a date. For example:

$ ts 1534950044
$ tsc 1534950044
1534950044 -> Wed Aug 22 11:00:44 EDT 2018

When passed multiple arguments, `ts` will preform conversions and tally the total duration or difference between the largest and smallest timestamp and also print out a duration. For example:
When passed multiple arguments, `tsc` will preform conversions and tally the total duration or difference between the largest and smallest timestamp and also print out a duration. For example:

$ ts 1534947844 1534947904
$ tsc 1534947844 1534947904
1534947844 -> Wed Aug 22 10:24:04 EDT 2018
1534947904 -> Wed Aug 22 10:25:04 EDT 2018
Duration: 00:01:00

Duration is printed in total hours, minutes, seconds in the format `h:m:s`.

### Advanced Usage
`ts` is pipeline friendly so here is its expected behavior in a pipeline:
`tsc` is pipeline friendly so here is its expected behavior in a pipeline:

something | ts
something | tsc

When reading from `stdin`, `ts` needs to be invoked without arguments. It will attempt to read a series of space delineated arguments from its `stdin` stream all on the same line (i.e. no newlines present). If arguments are present, `ts` will ignore `stdin` and process the arguments instead.
When reading from `stdin`, `tsc` needs to be invoked without arguments. It will attempt to read a series of space delineated arguments from its `stdin` stream all on the same line (i.e. no newlines present). If arguments are present, `tsc` will ignore `stdin` and process the arguments instead.

ts | something
tsc | something

When `ts` is in front of a pipe, its behavior changes and it will echo its arguments along __adding the current UNIX timestamp as the last value__, delineated by spaces. A contrived example:
When `tsc` is in front of a pipe, its behavior changes and it will echo its arguments along __adding the current UNIX timestamp as the last value__, delineated by spaces. A contrived example:

$ echo 1534947844 1534947904 1534948588 | ts | cat
$ echo 1534947844 1534947904 1534948588 | tsc | cat
1534947844 1534947904 1534948588 1534950177

The advanced behavior of `ts` was developed for fun and to facilitate some of the following simplified operations:
The advanced behavior of `tsc` was developed for fun and to facilitate some of the following simplified operations:

1. Diff from a timestamp to current time

$ ts 1534947844 | ts
$ tsc 1534947844 | tsc
1534947844 -> Wed Aug 22 10:24:04 EDT 2018
1534950475 -> Wed Aug 22 11:07:55 EDT 2018
Duration: 00:43:51

2. Pipeline of fury

$ ts 1534947844 | ts | ts | ts | ts | ts | ts | ts
$ tsc 1534947844 | tsc | tsc | tsc | tsc | tsc | tsc | tsc
1534947844 -> Wed Aug 22 10:24:04 EDT 2018
1534950565 -> Wed Aug 22 11:09:25 EDT 2018
1534950565 -> Wed Aug 22 11:09:25 EDT 2018
Expand Down
16 changes: 8 additions & 8 deletions ts.sh → tsc.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
# ø
# Timestamp helper
version="0.3.0"
# UNIX Timestamp conversion helper
version="0.5.0"
stamps=""
# Seconds conversion helper
convertsecs() {
Expand All @@ -12,20 +12,20 @@ convertsecs() {
}
# Help
show_help () {
printf "ts UNIX timestamp utility\n\n"
printf "tsc UNIX timestamp conversion utility\n\n"
printf "Usage:\n\n"
printf "\tts -hv [timestamp ...] \n\n"
printf "\ttsc -hv [timestamp ...] \n\n"
printf "Options:\n\n"
printf "\t-h -?\t\t\tShows this help\n"
printf "\t-v \t\t\tPrint version and exit\n\n"
printf "Usage:\n\n"
printf "\tts (no arguments)\tPrint the current timestamp to stdout\n"
printf "\tts [timestamp]\t\tConvert timestamp to local time\n"
printf "\tts [ts ... ts]\t\tConvert timestamps and calculate duration\n"
printf "\ttsc (no arguments)\tPrint the current timestamp to stdout\n"
printf "\ttsc [timestamp]\t\tConvert timestamp to local time\n"
printf "\ttsc [ts ... ts]\t\tConvert timestamps and calculate duration\n"
}
# Version
show_version () {
printf "ts version: %s\n" "$version"
printf "tsc version: %s\n" "$version"
}
################################################################################
### Process Args
Expand Down

0 comments on commit 815d384

Please sign in to comment.