nicolas / debian.moreutils (public) (License: GPL-2, GPL-2+, Expat, BSD-2-Clause, Public Domain) (since 2018-09-25) (hash sha1)
Debian packaging of joeyh's moreutils

/ts (3ce73c9b1d051f42231f90a6ccad454acfdaba79) (3893 bytes) (mode 100755) (type blob)

#!/usr/bin/perl

=head1 NAME

ts - timestamp input

=head1 SYNOPSIS

ts [-r] [-i | -s] [format]

=head1 DESCRIPTION

ts adds a timestamp to the beginning of each line of input.

The optional format parameter controls how the timestamp is formatted,
as used by L<strftime(3)>. The default format is "%b %d %H:%M:%S". In
addition to the regular strftime conversion specifications, "%.S" and "%.s"
are like "%S" and "%s", but provide subsecond resolution
(ie, "30.00001" and "1301682593.00001").

If the -r switch is passed, it instead converts existing timestamps in
the input to relative times, such as "15m5s ago". Many common timestamp
formats are supported. Note that the Time::Duration and Date::Parse perl
modules are required for this mode to work. Currently, converting localized
dates is not supported.

If both -r and a format is passed, the existing timestamps are
converted to the specified format.

If the -i or -s switch is passed, ts timestamps incrementally instead. In case
of -i, every timestamp will be the time elapsed since the last timestamp. In
case of -s, the time elapsed since start of the program is used.
The default format changes to "%H:%M:%S", and "%.S" and "%.s" can be used
as well.

=head1 ENVIRONMENT

The standard TZ environment variable controls what time zone dates
are assumed to be in, if a timezone is not specified as part of the date.

=head1 AUTHOR

Copyright 2006 by Joey Hess <id@joeyh.name>

Licensed under the GNU GPL.

=cut

use warnings;
use strict;
use POSIX q{strftime};

$|=1;

my $rel=0;
my $inc=0;
my $sincestart=0;
use Getopt::Long;
GetOptions("r" => \$rel, "i" => \$inc, "s" => \$sincestart) || die "usage: ts [-r] [-i | -s] [format]\n";

if ($rel) {
	eval q{
		use Date::Parse;
		use Time::Duration;
	};
	die $@ if $@;
}

my $use_format=@ARGV;
my $format="%b %d %H:%M:%S";
if ($inc || $sincestart) {
	$format="%H:%M:%S";
	$ENV{TZ}='GMT';
}
$format=shift if @ARGV;

# For subsecond resolution, Time::HiRes is needed.
my $hires=0;
if ($format=~/\%\.[Ss]/) {
	require Time::HiRes;
	$hires=1;
}

my $lastseconds = 0;
my $lastmicroseconds = 0;

if ($hires) {
	($lastseconds, $lastmicroseconds) = Time::HiRes::gettimeofday();
} else {
	$lastseconds = time;
}


while (<>) {
	if (! $rel) {
		if ($hires) {
			my $f=$format;
			my ($seconds, $microseconds) = Time::HiRes::gettimeofday();
			if ($inc || $sincestart) {
				my $deltaseconds = $seconds - $lastseconds;
				my $deltamicroseconds = $microseconds - $lastmicroseconds;
				if ($deltamicroseconds < 0) {
					$deltaseconds -= 1;
					$deltamicroseconds += 1000000;
				}
				if ($inc) {
					$lastseconds = $seconds;
					$lastmicroseconds = $microseconds;
				}
				$seconds = $deltaseconds;
				$microseconds = $deltamicroseconds;
			}
			my $s=sprintf("%06i", $microseconds);
			$f=~s/\%\.([Ss])/%$1.$s/g;
			print strftime($f, localtime($seconds));
		}
		else {
			if ($inc || $sincestart) {
				my $seconds = time;
				my $deltaseconds = $seconds - $lastseconds;
				if ($inc) {
					$lastseconds = $seconds;
				}
				print strftime($format, localtime($deltaseconds));
			} else {
				print strftime($format, localtime);
			}
		}
		print " ".$_;
	}
	else {
		s{\b(
			\d\d[-\s\/]\w\w\w	# 21 dec 17:05
				(?:\/\d\d+)?	# 21 dec/93 17:05
				[\s:]\d\d:\d\d	#       (time part of above)
				(?::\d\d)?	#       (optional seconds)
				(?:\s+[+-]\d\d\d\d)? #  (optional timezone)
			|
			\w{3}\s+\d{1,2}\s+\d\d:\d\d:\d\d # syslog form
			|
			\d\d\d[-:]\d\d[-:]\d\dT\d\d:\d\d:\d\d.\d+ # ISO-8601
			|
			(?:\w\w\w,?\s+)?	#       (optional Day)
			\d+\s+\w\w\w\s+\d\d+\s+\d\d:\d\d:\d\d
						# 16 Jun 94 07:29:35
				(?:\s+\w\w\w|\s[+-]\d\d\d\d)?
						#	(optional timezone)
			|
			\w\w\w\s+\w\w\w\s+\d\d\s+\d\d:\d\d
						# lastlog format
		  )\b
		}{
			$use_format
				? strftime($format, localtime(str2time($1)))
				: concise(ago(time - str2time($1), 2))
		}exg;

		print $_;
	}
}


Mode Type Size Ref File
100644 blob 44 5d425843f23db3bb6970a55c953f345e3a8c8fe1 .gitattributes
100644 blob 17989 b7b5f53df1412df1e117607f18385b39004cdaa2 COPYING
100644 blob 1113 4f8af10c6b1b101e9fb3046bc3164ae1d6f5ab2d Makefile
100644 blob 1181 1bb90af3a954062b79c41840a150e8580dfec37a README
100755 blob 806 83a4eed00f82e3bcc81856149b47cffc4091f9aa check-isutf8
100755 blob 1330 f70091a75952981a4585a46cac6f1c73d37f0c70 chronic
100755 blob 3047 7f311d7aa58631795fcad87aa8fcc0f416a01796 combine
040000 tree - e0e5c3e9323435fc50b3822aa1ae90513ca34636 debian
100644 blob 5102 d2f68a1ac365a0c8df88d4b64a400f63fffe4c69 errno.c
100644 blob 3877 8d9b4acf28e4e85244dc43dce7fbf35bb2171f80 errno.docbook
100644 blob 13634 4210b75832b3d83f063daf7e6fff977a5478b1a4 ifdata.c
100644 blob 7339 47f414301c47a69a81694c3b5affd71261207d49 ifdata.docbook
100644 blob 3027 ff648cc55865f5bf9af76b9622b52b08a1b489fb ifne.c
100644 blob 2465 e9c45692b7b95e77ea0184732f883d278c9415f6 ifne.docbook
100644 blob 7598 971112457c834ca97fdecb5880dc7c749dc8ca44 isutf8.c
100644 blob 3088 f0d0dbe51a3e1afc2616807d631bcd15ccd07cbe isutf8.docbook
100644 blob 5471 4925409bd548b058f07defe913724868801040df lckdo.c
100644 blob 3637 3fbf797b177d6933ce106cce2e8368e5094702ab lckdo.docbook
100644 blob 5790 c5245316bf1ff46260e10caf09703f3dc7bc7db7 mispipe.c
100644 blob 2566 d65d1a45b1b47ba8d6fc7c87211cf4b26eb0feed mispipe.docbook
100644 blob 7131 af85a334d4b2e3c5750af05eedb14bba435c67be parallel.c
100644 blob 3992 87e58787b21e94d007f4f62b038296599e995f2f parallel.docbook
100644 blob 1188 a8565c0245914cfa8bc8fb2c46dcb83c7f1dd8ab pee.c
100644 blob 2324 f554ad05e29361c56ececa40f00a402559f4be9e pee.docbook
100644 blob 7301 a53a2cf1906998c91533f5f5435ceeeeb1a7cd59 physmem.c
100644 blob 9200 09d3971fdb020de4da825625bd6dbbec8521d803 sponge.c
100644 blob 2865 31bc6dbc244e5e5313a2b34871877878fce24827 sponge.docbook
100755 blob 3893 3ce73c9b1d051f42231f90a6ccad454acfdaba79 ts
100755 blob 4746 c7f12392fe90ef5c31ebf4a2952922127559073c vidir
100755 blob 1403 2bf640dbd13f46dea540cbc49b8295d07d25a883 vipe
100755 blob 2518 98d1445a5f8106f04be690f85d802a7f6decfd13 zrun
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/nicolas/debian.moreutils

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/nicolas/debian.moreutils

Clone this repository using git:
git clone git://git.rocketgit.com/user/nicolas/debian.moreutils

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main