help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] set time


From: Greg Wooledge
Subject: Re: [Help-bash] set time
Date: Thu, 13 Sep 2012 14:39:11 -0400
User-agent: Mutt/1.4.2.3i

On Thu, Sep 13, 2012 at 03:51:22PM +0000, Max wrote:
> Is there any *direct* command to shift time in bash (using date or another,
> preferably not too unpopular, console binary)?
> 
> Say, it's 09:00 now and the command makes it 11:30 *without* me having 
> specified
> "11:30" but only provided the command the 2h30m interval.

Date arithmetic can't be done easily.  Bash doesn't provide any commands
for it, and neither does POSIX.

Many systems do provide tools that can do it, however.  GNU date(1)
is one of the more widely available ones:

imadev:~$ gdate; gdate -d 'now + 2 hours 30 minutes'
Thu Sep 13 14:34:34 EDT 2012
Thu Sep 13 17:04:34 EDT 2012

imadev:~$ gdate -d 'Sep 12 12:00:00 EDT 2012 + 1 hour 27 minutes'
Wed Sep 12 13:27:00 EDT 2012

Various BSD date(1) commands have similar extensions.  See the man pages
on your system to determine whether they exist, and how to use them if
they do.  Otherwise, you may have to install something.

I don't recommend writing date arithmetic code yourself unless you are
absolutely forced to do so.

> Note that any "indirect" methods may need complicated ways to keep
> track of the milliseconds lost while running them.

You're starting to confuse me here.  Are you performing date arithmetic
with static inputs (e.g. times read from a log file), or is the current
system clock one of the inputs?

If you want to perform a sequence of date arithmetic operations with the
current time being one of the inputs, it's best to capture the current
time *once* up front and then use that repeatedly as needed, rather than
capturing the current time multiple times and having it drift over the
course of the script.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]