Get Unix timestamp (in seconds) for a hg commit - date

Given a commit with the date Mon Aug 18 21:05:38 2014 +0200, how can I get the Unix timestamp of it in seconds?
The following command produces a number that discards the number (presumably because the timezone information of date got discarded):
$ hg log -l1 --template '{date(date, "%s")}\n'
1408392338
$ date -d#1408392338
Mon Aug 18 22:05:38 CEST 2014
I am effectively looking for the equivalent of the git command that produces the commit date as a Unix timestamp:
git log -n1 --pretty=%ct

The simplest solution is to take the first field produced from the template format {date|hgdate}, which is already in UTC. The second field in this format just gives the timezone, and can be discarded by applying Mercurial's word function within the format specifier. Here is a test of this format against the system date command and your own (correct, but longer) answer, on Mercurial 3.7.3:
$ date +%s && hg commit -m 'ok'
1539258496
$ TZ=UTC hg log -l1 --template '{date(date|localdate, "%s")}\n'
1539258496
$ hg log -l1 -r. --template '{date|hgdate}'
1539258496 -7200
$ hg log -l1 --template '{word(0, date|hgdate)}'
1539258496
The correctness is also confirmed by the description in the hg manpage of the hgdate filter:
hgdate Date. Returns the date as a pair of numbers: "1157407993 25200" (Unix timestamp, timezone offset).
(Side note: The timezone offset, a little confusingly, is negated: I'm in UTC+2, but the offset is given as -7200 seconds. However, this is only important if you want the local time. If you're after UTC, you will be ignoring the offset in any case.)
Overall, the date|hgdate variant only saves a few characters, but personally I find it conceptually cleaner and easier to understand. It may also be a little more robust against potential future bugs in Mercurial, since it involves fewer parsing and formatting operations.

The requested timestamp was one that is independent of the timezone, so UTC time. As date(..., "%s") produces a number which is relative to the current timezone, one should request a UTC output by combining the localdate filter with the TZ environment variable to set a timezone:
TZ=UTC hg log -l1 --template '{date(date|localdate, "%s")}\n')

You can get date of changeset in any format, which understand -d option of date and use it in date -d -F
You can use keyword with filter
Sample
hg log -r tip -T "date: {date}\nhgdate: {date|hgdate}\nISO-date: {date|isodate}\n"
date: 1390885140.0-21600
hgdate: 1390885140 -21600
ISO-date: 2014-01-28 10:59 +0600
I think (too lazy to test), result of expression 1390885140-21600 will give you correct timestamp

Related

Display the date modified of a file in another directory?

I want to display the date modified of a file that is in another directory.
e.g. I am in /some/directory and I grep -rHl "foo" which returns a list of files. I am curious about the date modified of /a/completely/different/directory/result.txt without having to go to that directory and list the files.
Can this be done?
Could use stat from GNU Coreutils:
stat -c %y /path/to/file
output:
2020-12-08 15:43:01.306251716 +0100
Or ls from GNU Coreutils:
ls --full-time /path/to/file
output:
rw------- 1 user user 759 2020-12-08 15:43:01.306251716 +0100 /path/to/file

I am using Sun OS, I want my script to read date from a file(in format %Y%m%d) and add 1 day to that date

I am working in Sun OS environment, I want to add a functionality to my existing unix ksh script where it allows to read a date(in %Y%m%d format) from a file and add 1 day and rewrite the same into that file. [please note: not adding day to current date instead i want to add 1 day to i/p date present in a file].
Eg:DateFile.dat
20200620
I want my script to change it to 20200621 at the end of run.
My code as below:
#!/bin/ksh
ip_dte</home/{file_Path}
echo $ip_dte
dte_add=`TZ=AEST-24 "$ip_dte"`
echo $dte_add
Something like that ? :
#!/bin/ksh
# Starting date, YYYYMMDD (yes I should verify the format :) )
FIRST_DATE=$1
[[ -z $FIRST_DATE ]] && FIRST_DATE=$(date +"%Y%m%d")
# 1 day in seconds
PERIOD=86400
# Transform the starting date in second and add 1 day
SECOND_DATE=$(( $(date -d "$FIRST_DATE" +"%s")+$PERIOD ))
# Transform the second date from second to human date format
print "Second date is $(date -d #"$SECOND_DATE" +"%Y%m%d")"
We can let date do the calculations and formatting for us:
$ date1='20200415'
$ date -d "${date1}+1 day" # add 1 day, use default output format
Thu Apr 16 00:00:00 UTC 2020
$date -d "${date1}+1 day" '+%Y%m%d' # add 1 day, change to YYYYMMDD format
20200416
$ date2=$(date -d "${date1}+1 day" '+%Y%m%d') # save new date in variable in YYYYMMDD format
$ echo "${date2}"
20200416
Here's a ksh fiddle of the above.
The other answers will work if you have ‎CSWcoreutils or SUNWgnu-coreutils installed. You may have to run gdate or /usr/gnu/bin/date.
But if you have a recent version of Solaris, ksh will be ksh93, and you can use the %T format in printf:
$ cat ddd
20200620
$ ip_dte=`cat ddd`
$ printf "%(%Y%m%d)T\n" "$ip_dte tomorrow"
20200621
If you have perl with the Time::Local module on the old Solaris server, try this:
#!/bin/ksh
ip_dte=20200531 # or read date from file into ip_dte
echo $ip_dte
timestamp=`perl -MTime::Local=timelocal -e '($y,$m,$d) = $ARGV[0] =~ /(\d\d\d\d)(\d\d)(\d\d)/; $m=$m-1; print timelocal(1,1,1,$d,$m,$y);' $ip_dte`
dte_add=`perl -MPOSIX=strftime -e 'print strftime("%Y%m%d", localtime($ARGV[0] +86400));' $timestamp`
echo $dte_add

How to get Unix Time stamp as 00:00:00

Is there any command to get Timestamp as 00:00:00
Basically i need Current Date with Time stamp as 00:00:00 in unix
I tried
$(date '+%Y-%m-%d' 00:00:00)
But i need actual command in unix for it instead of appending 0
If you want a string in the format 2020-02-05 00:00:00 then how about just using echo to print the date output of the date command and then appending the time?
Like
echo "$(date '+%Y-%m-%d') 00:00:00"

How to convert a long date to short date from SSL certificates | Unix KSH

I'm wondering if is possible to convert a date as show Oct 31 00:00:00 2013 GMT to 10-31-2013.
I'm getting the date as follow:
NotBeforeDate=$(openssl x509 -noout -in ${CERTIFICATE} -dates | grep "notBefore")
The date that I'm getting is Oct 31 00:00:00 2013 GMT and I wanted to convert it to 10-31-2013.
There's any command that could do that? Or do I have to do it all manually?
If so, the best way is create my own function and send the long date as parameter and return a short date.
The openssl command will make the NotBeforeDate variable to have the value (at least in the bash version I'm using):
notBefore=Oct 31 00:00:00 2013 GMT
So, first we need to remove the notBefore= part:
dateStr=${NotBeforeDate/notBefore=/}
Then you can use the date command:
date --date="$dateStr" --utc +"%m-%d-%Y"
The --date option tells the command to use the dateStr value, --utc tells that the date is in UTC (as specified by GMT part) and +"%m-%d-%Y" formats the date to the desired format.
The output is:
10-31-2013
PS: the options can vary according to your Linux version.
You can check all the available ones with date --help or man date.
For example, the long options --date and --utc might not be available, but the equivalent short versions might be (just an example, I'm not sure if date command has such variations between different unix versions):
date -d "$dateStr" -u +"%m-%d-%Y"
Unfortunately I don't have the exact same environment you're using (ksh in unix), but that should work.
The -d options seems to be GNU specific, so if it's not available, you'll have to manually parse the string. Assuming that dateStr has the value Oct 31 00:00:00 2013 GMT, you can run:
printf '%s\n' "$dateStr" | awk '{ printf "%02d-%02d-%04d\n", (index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)+2)/3, $2, $4}'
The output is:
10-31-2013
This script works for me when checking a url with certificate.
date --date="$(echo | openssl s_client -servername $DOMAIN -connect $DOMAIN:$PORT | openssl x509 -noout -enddate | awk -F '=' '{print $NF}' )" --iso-8601
PORT is 443 by default.
Not to duplicate the explanations for the date command in previous answers, here's a compact Bash function that reads a certificate file and outputs either the start or end date in either ISO format or as a Unix timestamp (useful for integer comparison), and can be used on Linux or macOS:
# cert_date <date_type> <out_format> <cert_file> [openssl_opts...]
# date_type: start|end
# out_format: iso|unix
cert_date() {
local type=$1 out=$2 args date opts fmt
[ "$type" == start ] && args=(-startdate) || args=(-enddate)
date=$(openssl x509 "${args[#]}" -noout -in "${#:3}")
[[ "$OSTYPE" == darwin* ]] && opts=(-juf "%b %d %T %Y %Z") || opts=(-ud)
[ "$out" == unix ] && fmt="%s" || fmt="%Y-%m-%dT%TZ"
date "${opts[#]}" "${date/*=/}" +"$fmt"
}
Of course, you can enhance by changing fmt to suit your output format needs, and even add a case statement to accept more formats.
Examples:
cert_date start iso ${CERTIFICATE}
cert_date end unix ${CERTIFICATE} -inform der
I made a bash OneLiner from the ideas above. Maybe it is useful for somebody:
(Use 'cut -c 10-' when greeping notAfter)
date -d "`openssl x509 -dates -noout -in /path/to/cert.pem | grep notBefore | cut -c 11-`" +%Y.%m.%d
I know this post is old but I search how to convert date and especially the end date. And inspired by the command lines posted here I made this one :
date --date="$(echo | openssl x509 -noout -in my_file.crt -enddate | cut -d'=' -f 2)" --utc +'%Y-%m-%d %H:%M:%S'
Cutting is easier because as I only want the end date, I use the "enddate" parameter of the openssl command
In new version of openssl this option is build in:
openssl x509 -noout -in ${CERTIFICATE} -enddate -dateopt iso_8601
notBefore=2021-04-15 09:23:07Z
It returns date and time, however cutting the time part should be no challenge.
And it looks like, you really need the most recent version of openssl: --dateopt option does not work

Calculating number of days given date in UNIX

I have tried to calculate number of days from January 1st to given date in same year.
Option -d for UNIX command isn't working
date -d
date: illegal option -- d
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]
I'm using this script but is too long.
Is there a simple way to calculate a nuber of days?
EDIT
Result of a script:
$ ksh datecalc -a 2013 2 5 - 2013 1 1
$ 35
OK so this may be a bit far fetched, but mysql client (or other DB clients) can come in handy for this as they have reliable and well documented date functions.
$ mysql ..... --silent -e "select datediff('2013-02-05', '2013-01-01') from dual;"
35
$
where ..... are your connection options.
If you have Perl installed, you can do:
perl -MPOSIX=strftime -le 'print strftime("%j",localtime)'
For a specific day, e.g. Feb 5:
perl -MPOSIX=strftime -le '
#d=(2013,2,5);
print strftime("%j",0,0,0,$d[2],$d[1]-1,$d[0]-1900)
'
036