Get timestamp including milliseconds - ruby

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.

Related

Simplify complex command, put it into a variable

date +'%A %B %d' | sed -e 's/\(^\|[^[:digit:]]\+\)0\+\([[:digit:]]\)/\1\2/g
I like the output of the above command, which strips leading zeroes off days of the month produced by the date command, in the case of numerals less than 10. It's the only way I've thus far found of producing single digit dates from the date command's output for the day of the month, which otherwise would be 01, 02, 03, etc.
A couple of questions in this regard. Is there a more elegant way of accomplishing the stated goal of stripping off zeroes? I do know about date's %e switch and would like to use it, but with numerals 10 and greater it has the undesirable effect of losing the space between the month name and the date (so, July 2 but July10).
The second question regards the larger intended goal of arriving at such an incantation. I'm putting together a script that will scrape some data from a web page. The best way of locating the target data on the page is by searching on the current date. But the site uses only single digits for the first 9 days of the month, thus the need to strip off leading zeroes. So what's the best way of getting this complex command into a variable so I can call it within my script? Would a variable within a variable be called for here?
RESOLUTION
I'll sort of answer my own question here, though it is really input from Renaud Pacalett (below) that enabled me to resolve the matter. His input revealed to me that I'd not understood very well the man page, particularly the part where is says "date pads numeric fields with zeroes," and below that where it is written "- (hyphen) do not pad the field." Had I understood better those statements, I would have realized that there is no need for the complex sed line through which I piped the date output in the title of this posting: had I used there %-d instead of just %d there would have been no leading zeroes in front of numerals less than 10 and so no need to call sed (or tr, as suggested below by LMC) to strip them off. In light of that, the answer to the second question about putting that incantation into a variable becomes elementary: var=$(date +'%A %B %-d') is all that is needed.
I may go ahead and mark Renaud Pacalet's response as the solution since, even though I did not implement all of his suggestions into the latest incarnation of my script, it proved crucial in clarifying key requirements of the task.
If your date utility supports it (the one from GNU coreutils does) you can use:
date +'%A %B %-d'
The - tells date to not pad the numeric field. Demo:
$ date -d"2021/07/01" +'%A %B %-d'
Thursday July 1
Not sure I understand your second question but if you want to pass this command to a shell script (I do not really understand why you would do that), you can use the eval shell command:
$ cat foo.sh
#!/usr/bin/env bash
foo="$(eval "$1")"
echo "$foo"
$ ./foo.sh 'date -d"2021/07/01" +"%A %B %-d"'
Thursday July 1
Please pay attention to the double (") and simple (') quotes usage. And of course, you will have to add to this example script what is needed to handle errors, avoid misuses...
Note that many string comparison utilities support one form or another of extended regular expressions. So getting rid of these leading zeros or spaces can be as easy as:
grep -E 'Thursday\s+July\s+0*1' foo.txt
This would match any line of foo.txt containing
Thursday<1 or more spaces>July<1 or more spaces><0 or more zeros>1

zsh on macOS date modify output of given date (without script)

I‘m aware of date +%u to get the day of the week for today.
I‘d like to get that integer for any arbitrary date i input - if possible in the format I choose (e.g. %YYmmdd)
ok, found it finally:
date -j -f %Y%m%d +%u 20200910
this is, because date on macOS doesn't take a switch for putting in custom date (fyi for those folks, how try to make -v work, like me^^)
in addition, -f affects only input format (it's literally the second word in the man page, but I managed to overlook more than once)
-j is needed to use -f without setting the date.
hope this will spare someone time in the future ;)
edit:
it seems to be important, to specify input format before output format (see comment from #chepner below)
(also be careful with quotes)
$ date +%u -d "2020-09-10"
4

short date in bash PS1 prompt

You can use \d in the your PS1 confuration to display a long date ie. Tues 18 May, but how can I get it to display it in a format like 18.05.2012 for example?
Try including \D{%d.%m.%Y}. You can use any time format supported by strftime(3).
Try this:
PS1="\$(date +%d.%m.%Y) > "
export PS1
Use \D{format} where format is a strftime format code.
For example:
$ export PS1='\D{%d.%m.%Y}$ '
08.02.2012$
Rather than telling the shell to execute the date command each time, you would rather use the built-in format. Hence you can also use (though a little variation from what you have asked)
\D{%F %T}
to give you date and time.
date in format : YYYY-MM-DD
and time in format hh:mm:ss.
you can try this that display time:
$ PS1="\n\t \u#\h:\w# "
08:18:57 user#localhost:/home/user#
I'm not sure how to apply this to PS1 specifically (maybe someone can edit this question to replace this part with the best working bit from other questions).
BUT you can get short date formatted according to your current locale with:
date +%x

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