How to get Unix Time stamp as 00:00:00 - unix-timestamp

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"

Related

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

Why does having the time in GNU date break "- 1 day"?

This converts the given datetime to epoch time (seconds)
date -d"2015-07-24 11:29:00" +%s
// gives 1437762540
Now I want to do the same thing, but subtract a day. Normally, this is as simple as adding "- 1 day". However, instead of subtracting a day, it actually adds a day.
date -d"2015-07-24 11:29:00 - 1 day" +%s
// gives 1437848940 (notice, this value is great than the one above)
If I take away the time portion from my timestamp it works great. The time portion seems to break it however. I know I can do this in two separate steps and avoid this problem. However, I was hoping to do it in one command. Is this possible?
There is some ambiguity in the date command about how to interpret the - 1 token in your date string. It resolves it as a time zone specification
$ date -d "2015-07-24 11:29:00 -1"
Fri Jul 24 08:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-1"
Fri Jul 24 08:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 - 2"
Fri Jul 24 09:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-2"
Fri Jul 24 09:29:00 EDT 2015
$ TZ=UTC date -d "2015-07-24 11:29:00 -1"
Fri Jul 24 12:29:00 UTC 2015
(your results may vary depending on your TZ setting)
The day part is then interpreted to mean add one day
$ date -d "2015-07-24 11:29:00 - 1 day"
Sat Jul 25 12:29:00 EDT 2015
$ date -d "2015-07-24 11:29:00 UTC-1 + 1 day"
Sat Jul 25 12:29:00 EDT 2015
Add a timezone spec to your date string, as #amdixon suggests, to resolve the ambiguity and get the expected results.
For reference, the GNU coreutils info page for "Date input formats": https://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html
I don't have a precise answer. I do know that free-form datetime parsing is hard. Playing around with date:
$ date -d"2015-07-24 11:29:00 - 1 day" "+%F %T"
2015-07-25 08:29:00
$ date -d"2015-07-24 - 1 day" "+%F %T"
2015-07-23 00:00:00
$ date -d"11:29:00 - 1 day" "+%F %T"
2015-07-25 08:29:00
$ date -d"2015-07-24 - 1 day 11:29:00" "+%F %T"
2015-07-23 11:29:00
So, subtracting a day from a date seems to work, but subtracting a day from a time is problematic.

Get Unix timestamp (in seconds) for a hg commit

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

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

hp-ux how to get 2 hours ago bash datetime

we are using hp-ux servers
we need to get 2 hours ago datetime value in bash shell script ?
how can i do that any experiences ?
date -d -2hours; date --version
Thu Apr 14 02:38:08 CEST 2011
date (GNU coreutils) 7.4
I'm not a shell script expert, but you may want to check this site at unix.com. They provide this example for subtracting from dates:
# subtract from any date
date --date "$dte 3 days 5 hours 10 sec ago"
date --date "$dte -3 days -5 hours -10 sec"