Print Batch Time in Milliseconds - windows

I am aware that one can print batch time up to the centisecond with #echo %time%. Is there a similar command to get milliseconds as well?

There is a resource kit utility named Timethis that provides up to the millisecond time measurements :
TimeThis : Command Line : dir
TimeThis : Start Time : Wed Oct 24 12:49:56 2012
TimeThis : End Time : Wed Oct 24 12:49:56 2012
TimeThis : Elapsed Time : 00:00:00.093

Use wmic os get LocalDateTime. That's also the reliable way to get locale-independent date and time
for /F "usebackq tokens=2 delims==" %%i in (
`wmic os get LocalDateTime /VALUE`
) do #set ctime=%%i
echo milliseconds: %ctime:~15,3%

(Edit: doh, didn't notice that this has the windows tag. Leaving the answer here in case anyone else clicked on this for Linux)
If you are asking to time the execution length of a program or script, precede the command with time, for example:
time ls -R
and this will give you execution time in milliseconds:
real 0m0.667s
user 0m0.000s
sys 0m0.144s
If you are asking for the current date and time:
Take a look at:
man date
For example,
date +"%X %N"
This will give you the current time in Hour:Minutes:Seconds and then the Nanoseconds
Hope this answers your question

Gnu gdate can make this very simple; try the code below in a batch file, for instance. I use it to create unique filenames for batch processes invoked in command windows, and on my system under Windows 7 it's evident that, at least in a command window, two immediately successive uses of gdate never give identical results.
gdate +"%%Y%%m%%d%%H%%M%%S%%N"
gdate +"%%Y%%m%%d%%H%%M%%S%%N"
set i=
set T=
for /f %%i in ('gdate +%%Y%%m%%d%%H%%M%%S%%N') do set UniqueTime=%%i
echo %UniqueTime%
But I actually use something more like the line below, discarding the last five digits because for my purposes they never seem to matter: the preceding part is always unique. This lets the unique file names be shorter.
gdate +"%%Y%%m%%d%%H%%M%%S%%N" | sed "s/.....$//"
Pretty obviously, the same expression works under Linux, so one can also use it there.

ptime utility is "5ms or better" accurate
ptime echo hallo
ptime 1.0 for Win32, Freeware - http://www.pc-tools.net/
Copyright(C) 2002, Jem Berkes <jberkes#pc-tools.net>
=== echo hallo ===
hallo
Execution time: 0.022 s

Related

Check whether a Windows certificate is valid by date - using CMD

My target it to check whether a certificate (I have its exact name, in the example below it is "Dell") in any Windows computer is valid.
The way I think I can do it, is by taking the expiration date, and compare it with the current date.
So, first, I want to take the expiration date ("not after" field).
The way I did it:
certutil -verifystore Root Dell | findstr/n ^^ | findstr "^[6]:" | for /f "tokens=3-5 delims= " %f in ('more') do #echo %f %g %h
Output: 3/8/3020 8:47 PM
But note this is matching my regional date format. I need it to be generic- in a way it would work for all possible formats
Now, for any Windows computer I have the date.
Next step, is to get the current date with %date% variable, and calculate the seconds (?) difference between the two dates. With that result I could know whether the certificate is expired or not.
I can't find how to do so. Any advice?
Few notes:
No PowerShell
only 1 cmd command
The result of this 1 command should be the seconds difference of the 2 dates
Any other way to verify a certificate is acceptable. The way above is the only 1 I know

Calculate 15 minutes ago in shell

I have a time-stamp like 7:00:00, which means 7am.
I would like to write a short command that returns 06:45:00, or simply 06:45, preferably using date command so that I can avoid long shell script. Do you have any elegant solution?
I'm also looking for a 24h format. For example, 12:00:00 - 15 minutes = 11:45:00.
With GNU date, use 7:00:00 AM - 15 minutes as d (--date) string :
% date -d '7:00:00 AM - 15 minutes' '+%H:%M'
06:45
+%H:%M sets the output format as HH:MM.
On BSD variants Date has a -v flag which can be used to take the current timestamp and display the result of a positive or negative adjustment.
This will subtract 15mins from the current timestamp:
date -v -15M

How to get current timestamp of realtime-clock (RTC)?

I would like to get the current timestamp of realtime clock in my BASH script. The hwclock command can print out the current time, but it is not timestamp.
I have considered parsing hwclock's output and then convert the results to a timestamp, but it seems the hwclock output will vary if the current locale gets changed. So if I give my script to my clients, they could use different locales than mine, and my parsing results could be wrong (and of course the timestamp will be wrong).
So my question is, what would be the best way to get timestamp from RTC? Thanks in advance.
When you call hwclock -D, you get an output like the following which contains the timestamp:
hwclock from util-linux 2.27.1
Using the /dev interface to the clock.
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
...got clock tick
Time read from Hardware Clock: 2018/09/27 09:03:46
Hw clock time : 2018/09/27 09:03:46 = 1538039026 seconds since 1969
Time since last adjustment is 1538039026 seconds
Calculated Hardware Clock drift is 0.000000 seconds
Thu 27 Sep 2018 11:03:45 CEST .749554 seconds
The following command parses this output to extract the actual timestamp:
sudo hwclock -D | grep "Hw clock time" | sed "s/.* = \([0-9]\+\) seconds .*/\1/"

Text-Message Gateways & Incrementing Bash Variable Daily

I have a bash script that is sending me a text daily, for 100 days.
#! /bin/bash
EMAIL="my-phone-gateway#address.net"
MESSAGE="message_content.txt"
mail $EMAIL < $MESSAGE
Using crontab, I can have the static $MESSAGE sent to me every day.
Other than hard-coding 100 days of texts ;)
How could I implement a variable counter such that I can have my texts say:
"Today is Day #1" on the first day, "Today is Day #2" on the second day, etc. ?
Note: The location of the requested text within the $MESSAGE file doesn't matter. Last line, first line, middle, etc.
The only requirement for an answer here is that I know what day it is relative to the first, where the first day is the day the script was started.
Of course, bonus awesome points for the cleanest, simplest, shortest solution :)
For our nightly build systems, I wrote a C program that does the calculation (using local proprietary libraries that store dates as a number of days since a reference date). Basically, given a (non-changing) reference date, it reports the number of days since the reference date. So, the cron script would have a hard-wired first day in it, and the program would report the number of days since then.
The big advantage of this system is that the reference date doesn't change (very often), so the script doesn't change (very often), and there are no external files to store information in.
There probably are ways to achieve the same effect with standard Unix tools, but I've not sat down and worked out the portable solution. I'd probably think it terms of using Perl. (The C program only works up to 2999 CE; I left a note in the code for people to contact me about 50 years before it becomes a problem for the Y3K fix. It is probably trivial.)
You could perhaps work in terms of Unix timestamps...
Create a script 'days_since 1234567890' which treats the number as the reference date, gets the current time stamp (from date with appropriate format specification; on Linux, date '+%s' would do that job, and it works on Mac OS X too), takes the difference and divides by 86,400 (the number of seconds in a day).
refdate=1234567890
bc <<EOF
scale=0
($(date '+%s') - $refdate) / 86400
EOF
An example:
$ timestamp 1234567890
1234567890 = Fri Feb 13 15:31:30 2009
$ timestamp
1330027280 = Thu Feb 23 12:01:20 2012
$ refdate=1234567890
$ bc <<EOF
> scale=0
> ($(date '+%s') - $refdate) / 86400
> EOF
1104
$
So, if the reference date was 13th Feb 2009, today is day 1104. (The program bc is the calculator; its name has nothing to do with Anno Domini or Before Christ. The program timestamp is another homebrew of mine that prints timestamps according to a format that can be specified; it is a specialized variant of date originally written in the days before date had the functionality, by which I mean in the early 1980s.)
In a Perl one-liner (assuming you specify the reference date in your script):
perl -e 'printf "%d\n", int((time - 1234567890)/ 86400)'
or:
days=$(perl -e 'printf "%d\n", int((time - 1234567890)/ 86400)')
The only way to accomplish this would be to store the date in a file, and read from that file each day. I would suggest storing the epoch time.
today=$(date +%s)
time_file="~/.first_time"
if [[ -f $time_file ]]; then
f_time=$(< "$time_file")
else
f_time=$today
echo "$f_time" > "$time_file"
fi
printf 'This is day: %s\n' "$((($today - $f_time) / 60 / 60 / 24))"
Considering that your script is running only once a day, something like this should work:
#!/bin/bash
EMAIL="my-phone-gateway#address.net"
MESSAGE="message_content.txt"
STFILE=/tmp/start.txt
start=0
[ -f $STFILE ] && start=$(<$STFILE)
start=$((start+1))
MESSAGE=${MESSAGE}$'\n'"Today is Day #${start}"
echo "$start" > $STFILE
mail $EMAIL < $MESSAGE
A simple answer would be to export the current value to an external file, and read that back in again later.
So, for example, make a file called "CurrentDay.dat" that has the number 1 in it.
Then, in your bash script, read in the number and increment it.
e.g. your bash script could be:
#!/bin/bash
#Your stuff here.
DayCounter=$(<CurrentDay.dat)
#Use the value of DayCounter (i.e. $DayCounter) in your message.
DayCounter=$((DayCounter + 1))
echo $DayCounter > CurrentDay.dat
Of course, you may need to implement some additional checks to avoid something going wrong, but that should work as is.

Subtracting time from file creation/modification date in OSX

I am trying to shift the dates of a series of files by 9 hours. I've reached as far as this:
for i in *.MOV; do touch -r "$i" -d "-9 hours" "$i"; done
This should work in recent systems, but the touch command in OSX seems to be a bit outdated and not to support the -d switch.
I'm using Snow Leopard. Any idea on the best option for doing this with a single line command? I don't want to create a script for this.
Ok, sorted it out. OSX comes with a gtouch command, that knows the -d switch. It's part of GNU coreutils. See the comments below for information regarding availability on specific MacOS versions.
For more information on using relative dates with the -d switch see the manual.
Looking at the Wikipedia Page for Touch, it appears you're accustomed to the GNU version of Touch. Which MacOS isn't using.
For what you want to do, look into the "SetFile" command, which gets installed with XCode tools. You have -d and -m options, which reset the Created and Modified dates & times respectively.
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/SetFile.1.html
Donno OS X, but it should be easy enough to
get curr time stamp on the file
convert it to seconds
subtract 9 hours (9*60*60 secs) from it
convert it back to the format accepted by touch's -t option
run touch command
All this of course can be done in a single for loop on command line.
Here are simple examples from WikiPedia showing back and forth conversion.
# To convert a specific time stamp to Unix epoch time (seconds since 1970-01-01):
date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
# 1240596879
# To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:
date -d "UTC 1970-01-01 1240596879 secs"
# Fri Apr 24 13:14:39 CDT 2009
# Or:
date -ud #1000000000
# Sun Sep 9 01:46:40 UTC 2001
# or: Haven't tested this but should work..
date -d #1000000000 +%y%m%d%%H%M%S
# 010909014640

Resources