-
-
Notifications
You must be signed in to change notification settings - Fork 0
Datetool Examples
Datetools was created to simplify date/time manipulations on the command line.
Datemath provides a way of calculating a specific time with respect to the current time.
30 Days from now is:
datemath --days 30
Just late the date tool you won't lose any formatting features for scripting. In fact all of the same commands are supported with the -f (--format) switch:
- %a locale’s abbreviated weekday name (e.g., Sun)
- %A locale’s full weekday name (e.g., Sunday)
- %b locale’s abbreviated month name (e.g., Jan)
- %B locale’s full month name (e.g., January)
- %c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
- %C century; like %Y, except omit last two digits (e.g., 20)
- %d day of month (e.g, 01)
- %D date; same as %m/%d/%y
- %e day of month, space padded; same as %_d
- %F full date; same as %Y-%m-%d
- %g last two digits of year of ISO week number (see %G)
- %G year of ISO week number (see %V); normally useful only with %V
- %h same as %b
- %H hour (00..23)
- %I hour (01..12)
- %j day of year (001..366)
- %k hour ( 0..23)
- %l hour ( 1..12)
- %m month (01..12)
- %M minute (00..59)
- %n a newline
- %N nanoseconds (000000000..999999999)
- %p locale’s equivalent of either AM or PM; blank if not known
- %P like %p, but lower case
- %r locale’s 12-hour clock time (e.g., 11:11:04 PM)
- %R 24-hour hour and minute; same as %H:%M
- %n a newline
- %s seconds since 1970-01-01 00:00:00 UTC
- %S second (00..60)
- %T time; same as %H:%M:%S
- %u day of week (1..7); 1 is Monday
- %U week number of year, with Sunday as first day of week (00..53)
- %V ISO week number, with Monday as first day of week (01..53)
- %w day of week (0..6); 0 is Sunday
- %W week number of year, with Monday as first day of week (00..53)
- %x locale’s date representation (e.g., 12/31/99)
- %X locale’s time representation (e.g., 23:13:48)
- %y last two digits of year (00..99)
- %Y year
So continuing with examples; 90 minutes ago in a different format:
datemath --minutes=-90 --format="%Y%m%dT%H%M%S"
Another way of looking back 90 minutes:
datemath --seconds=-5400
Combine parameters to achieve a specific time:
datemath --years=2 --days=3 --hours=2
Block until a specific period of time has been reached unlike sleep which blocks for a specific period of time. Dateblock is similar to how crons work (and support the cron formatting).
Unblock on every minute divisible by 5.. For example... if the time is: 12:02:00, 12:05:00 would be the first 5 minute interval of the hour. You could preform this task as such:
dateblock --minute=/5
With respect to the above example... you'll unblock at different times depending on when you called the command. Hence... if you call it at 12:05:00; your already at the top of the 5 minute interval, so you'll block until 12:10:00 (the next minute divisible by 5).
Once the tool unblocks it gracefully returns to the command line (exit code of 0). When scripting, you can schedule start times of operations easier (just like cron does) this way.
You can use the cron formatting as well; the only difference is the 'second' column is added and is to be considered when you create the entry... hence if we stick with the same example above, the following is equivalent:
dateblock --cron='0 /5'
So is the following: `dateblock --cron='0 /5 * * * *' The cron (--cron|-c) switch allows one to specify the standard cron formatting:
day of week (0 - 6) (Sunday=0) -----------------------+ month (1 - 12) ------------------------------------+ | day of month (1 - 31) --------------------------+ | | hour (0 - 23) -------------------------------+ | | | min (0 - 59) -----------------------------+ | | | | sec (0 - 59) --------------------------- | | | | | | | | | | | - - - - - - * * * * * *
So just how complex can this tool be... Well it can be as complex as you want it and you can even test your results (and not actually block for an unknown period of time by testing it beforehand with the -t (--test) switch which will cause dateblock to 'not block' but just output the current time and the calculated time it would unblock at.
dateblock --cron='0 /5' -t Current Time : 2013-10-20 19:47:22 (Sun) Block Until : 2013-10-20 19:50:00 (Sun)
Just like cron the modulus is just one of the operator types you can use" A variety of syntax is accepted by this tool such as:
- x (Value) where 'x' is represented numerically.
- /x (or /x) (Modulus) where 'x' is represented numerically.
- x-y (or y-x) (Range) where 'x' and 'y' are are represented numerically.
- x,y (Separator) where 'x' and 'y' are are represented numerically.
Note: All variations of the syntax mentioned above can be mixed if separated using the 'comma' (Separator) operator. i.e.: */a,b,c-d,/e is valid. However: x-y-z is not valid, nor is /x/y or /x-y. All values must be within the range of it's time type. Thus 400-4000 would never work since no time constraint even resides within that range.