read a PCAP file with Lua script - time

I have a Lua script to read a PCAP file. For a packet, I can get its time with the expression
os.date("%Y/%m/%d %X", last_pinfo.abs_ts)
The result is like "2012/05/24 11:32:50", but I want the result like "May 24, 2012 11:32:50.476690000". Is that possible?

Lua's os.date follows the same format as C's strftime function.
However, time in standard Lua only goes accurate up to seconds. You'll have to add the decimal part of the seconds from another source, eg LuaSocket's socket.gettime():
require 'socket'
time=socket.gettime()
print(os.date("%B %d, %Y %H:%M:%S")..select(2,math.modf(time),time)

Related

How to generate date in this format (2022-04-29T06:07:28.158Z) in linux

Looking for solution to generate date in the below format:
2022-04-29T06:07:28.158Z
Have to use in bash script.
It looks like you want to have the milliseconds included (rounded to three places), so add %3N to pmf's suggestion from the comments:
date -u +%FT%T.%3NZ
Example output:
2022-04-29T16:23:39.905Z

How to get the last time a file was modified in Unix

I am trying to get the last time the date a file was modified. I used a variable for date and a variable for time.
This will get the date and time but I want to use -r using the date command to make a reflection of when the date was last modified. Just not sure how I go about using it in my variables.
How would I go about doing this?
Here are my variables:
DATE="$(date +'%m/%d/%Y')"
TIME="$(date +'%H:%M')"
I tried putting the -r after and before the time and date.
Though people might tell you, you should not parse the output of ls, simply that can easily break if your file name contains tabs, spaces, line breaks, your user decides to simply specify a different set of ls options, the ls version you find is not behaving like you expected...
Use stat instead:
stat -c '%Y'
will give you the seconds since epoch.
Try
date -d "#$(stat -c '%Y' $myfile)" "+%m/%d/%Y"
to get the date, and read through man date to get the time in the format you want to, replacing '%F' in the command line above:
date -d "#$(stat -c '%Y' $myfile)" "+%H:%M"
EDIT: used your formats.
EDIT2: I really don't think your date format is wise, because it's just so ambiguous for anyone not from the US, and also it's not easily sortable. But it's a cultural thing, so this is more of a hint: If you want to make your usable for people from abroad, either use Year-month-day as format, or get the current locale's setting to format dates.
I think you are looking for
ls -lt myfile.txt
Here in 6th column you will see when file was modified.
Or you could use stat myfile.txt to check the modified time of a file.
I know this is a very old question, but, for the sake of completeness, I'm including an additional answer here.
The original question does not specify the specific operating system. stat differs significantly from SysV-inspired Unixes (e.g. Linux) and BSD-inspired ones (e.g. Free/Open/NetBSD, macOS/Darwin).
Under macOS Big Sur (11.5), you can get the date of a file with a single stat command:
stat -t '%m/%d/%Y %H:%M' -f "%Sm" myfile.txt
will output
04/10/2021 23:22
for April 10, 2021.
You can easily put that in two commands, one for the date, another for the time, of course, to comply with the original question as formulated.
Use GNU stat.
mtime=$(stat --format=%y filename)

Get timestamp including milliseconds

I use this command a lot on OS X to create a timestamp for archiving purposes:
date -n +%Y%m%d%H%M%S
This gives an answer in this format:
20130625230005
I'd like to add milliseconds to the end of this string. Is it possible to get that with Ruby from the command line?
It's unfortunately not possible to do this on OS X by adding something to the command above: Add milliseconds to timestamp (bash, unix)
DateTime#strftime has a %L format specifier for milliseconds:
$ ruby -e "puts Time.now.strftime('%Y%m%d%H%M%S%L')"
20130625141141827
Update: To answer the question in your comment, yes, this is possible. Clicking on the documentation link above, there's %N for fractional second digits. To remove the last digit from above, use %2N rather than %L (%3N is equivalent to %L). To get just deciseconds, use %1N.

Convert HH:MM:SS.mm to seconds in bash

I am running some gnu time scripts which generates output of the form
mm:ss.mm (minutes, seconds and miliseconds, for example 1:20.66)
or hh:MM:ss (hours, minutes and seconds, for example 1:43:38).
I want to convert this to seconds (in order to compare them and plot them in a graphic).
Which is the easiest way to do this using bash?
$ TZ=utc date -d '1970-01-01 1:43:38' +%s
6218
Assuming you can run the GNU date command:
date +'%s' -d "01:43:38.123"
If the script is generating "mm:ss.mm" you'll need to add "00:" to the beginning, or date will reject it.
If you're on a BSD system (including Mac OS X), you need to run date -j +'%s' "0143.38" unless you have GNU date installed with MacPorts or Homebrew or something.
And if you want pure Bash you can do something like
IFS=: read h m s <<<"${hms%.*}"
seconds=$((10#$s+10#$m*60+10#$h*3600))
The 10# part is mandatory to specify that the numbers are given in radix 10. Without this, you'd get errors if h, m or s is 08 or 09 (as Bash interprets numbers with a leading 0 in octal).

Cshell Script date issue

Is there a way to add date in the name of the file... we can add current date in this manner date '+%Y%m%d' but i want to add "filename_date_1-2-2011_thru_31-2-2011.txt" Is it possible to do that??????????
If you have a sufficiently advanced version of the date command and you know a Unix timestamp for the start and end dates, then you can use:
(MacOS X) date -r 1234567890 "+%d-%m-%Y" to obtain 13-02-2009.
(GNU) date -d 2/13/2009 "+%d-%m-%Y" to obtain 13-02-2009 again.
If you don't want the leading zeroes on the day of month, then you need to use '%e` instead of '%d' on Linux (but that puts a space in place of the zero). It is not clear that there's a format specifier for day-of-month without a leading zero on MacOS X; nor is it clear that there's a way to format month of year as a single-digit number for January to September on either platform.
You get the format into your C shell script using back-ticks around the date commands.
Consider reading Csh Programming Considered Harmful and heeding its advice.

Resources