Add 1 hour in Current timestamp in KSH - unix

How to add 1 hour in unix timestamp
date +%Y%m%d_%H%M
I need to add 1 hour in above format

A literal answer to your question is to use
date --date="next hour" +%Y%m%d_%H%M
but I'm guessing that you actually want to display the time in another timezone. To display in UTC:
date --utc +%Y%m%d_%H%M
and in another timezone, e.g.
TZ="Europe/Stockholm" date +%Y%m%d_%H%M
assuming of course that system clock is setup correctly.

De GNU date makes live easy. Without GNU date you can manipulate your timezone:
echo "$(TZ=GMT+1 date +%Y%m%d_%H%M)"
Be careful with Daylight Saving Time.
You can remember this trick when you to get the date (without time) of yesterday. Just adding 24 hours to the timezone can give problems during the Daylight Saving Time. You can use a trick to find yesterday:
You are sure that yesterday is 20 or 30 hours ago. Which one? Well, the most recent one that is not today.
echo "$(TZ=GMT+30 date +%Y-%m-%d)\n$(TZ=GMT+20 date +%Y-%m-%d)" |
grep -v $(date +%Y-%m-%d) | tail -1
Above command is for ksh. When you use bash, you want echo -e.
Or use printf:
printf "%s\n%s\n" "$(TZ=GMT+30 date +%Y-%m-%d)" "$(TZ=GMT+20 date +%Y-%m-%d)" |
grep -v $(date +%Y-%m-%d) | tail -1

Related

Getting yesterday's date in Unix csh script

I currently have the below lines as part of my script:
#! /bin/csh
set thedate = `date "+%d_%m_%y"`
This sets thedate to today's date.
Does anyone know what I need to add to get the script to pick up yesterday's date instead?
I have tried
date -v "-1d"
date -d "-1 day"
but I get the errors
date: illegal option -- v
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
I have then tried
set thedate = `date "+%d_%m_%y" --date="1 day ago"`
which when echoing thedate, still gives today's date.
$(date "+%d_%m_%y" --date="1 day ago")
#!/bin/sh
thedate=`date "+%d_%m_%y" --date="1 day ago"`
echo "${thedate}"
Working fine for me.
I dont know why you are using C-shell. I dont have C-Shell in my target. use shell(/bin/sh) or bash (/bin/bash)
date -d "-1 days" +"%a %d/%m/%Y"
The -d "-1 days" makes sure that the date of yesterday gets calculated.
The +"%a %d/%m/%Y" takes care of the output format (name, day, month, year).

Last Month Date in Solaris CSH script

As pre requirement, I need to fetch last month date in Unix (solaris) csh.
set Lmit_Date=`date --date='1 month ago' +%Y%m%d`
above command will fetch last month date and working fine in Linux server. But our server is Solaris and mentioned command is not working.
Please can anyone suggest how I can fetch the last month date
The issue is due to the fact you are using a GNU date extension. --date is non standard.
Moreover, due to the fact month lengths are variable, the date displayed by GNU date might be unexpected, to say the least...
For example today is March 31 but "last month" date was March the 2nd according to GNU date:
$ date +%Y%m%d
20160331
$ date --date='1 month ago' +%Y%m%d
20160302
If you still want to either use GNU date on Solaris or find some workarounds, have a look to these replies:
https://stackoverflow.com/a/23507108/211665 and https://stackoverflow.com/a/17817087/211665
You should be able to compile coreutils for your solaris platform, which will provide you with the right date utility. But as coreutils overwrites core utilities as the name says, you may want to install this into a custom path and select the right date command through your special path, say "/opt/coreutils/bin/date".
The other method would be to calculate last month with a symbolic date output split
eval `date +"set YEAR=%Y; set MONTH=%m ;set DAY=%d"`
Now you can operate on "$YEAR", "$MONTH" and "$DAY". For example
let 'MONTH--'
if [ "$MONTH" -eq 0 ]; then MONTH=12; let 'YEAR--'; fi
set Lmit_Date=`date --date "$MONTH$DAY0000$YEAR" +"%Y%m%d"`
kind of. (I'm used to bash so I don't know if let is available here. But there are some other methods to shell calculations. There might be another keyword for csh).
Also you need to take care for number of days per month with the $DAY parameter.
function last_day {
y=`echo $1 | cut -f1 -d "-"`
m=`echo $1 | cut -f2 -d "-"`
d=`cal ${m} ${y} | nawk 'NF{A=$NF}END{print A}'`
echo "$y $m $d" | nawk '{printf("%s-%02d-%02d",$1,$2,$3);}'
} # last_day
last_day 2023-01-01
Will give you: 2023-01-31 in non-GNU Solaris.

Convert date string to unix timestamp without "date -d" in a shell script

I've been bashing my head into the wall for some time on this one.
I have a date string in my (ksh) script. It's not the current time, it's some arbitrary time.
How can I convert that date string into a Unix timestamp? I'm working on SunOS 5.10 and AIX, which don't have the date -d option, nor the MacOS date -j -f options.
I only need to do this conversion in one place in my code, so ideally I'd like to do it in one line, but if I have to create a function then so be it
I've messed around with Python and Perl to achieve this in one line. Python came the closest, but I couldn't get it to account for time zone, which I would really like. Any help would be much appreciated.
I lost whatever I had been trying to do with Python earlier, but looking back at it I've found a solution:
python -c 'import datetime, time;print time.mktime(datetime.datetime.strptime("08/22/2014/16", "%m/%d/%Y/%H").timetuple())'
This particular command outputs 1408737600, which is 4pm on August 22 2014 on the east coast.

Can I get UTC offset from Unix command line?

I'm writing an autoconf script that needs the current UTC offset. There's no obvious way to get this out of the date program. Is there any straightforward way to get this from a command-line utility, or should I write a test that gets the information and somehow captures it?
Try this, and see whether it works for you:
date +%z
For others doing ISO8601, you might pick some variant of:
date +%Y%m%dT%H%M%S%z # 20140809T092143-0700
date -u +%Y%m%dT%H%M%S%z # 20140809T162143+0000
date -u +%Y%m%dT%H%M%SZ # 20140809T162143Z
I like those because the lack of punctuation supports universal use. Note that the capital Z is 'hard-coded' for UTC - using %Z will put UTC or the other named timezone. If you prefer punctuation:
date +%Y-%m-%dT%H:%M:%S%z # 2014-08-09T09:21:43-0700
date +%Y-%m-%dT%H:%M:%S%:z # 2014-08-09T09:21:43-07:00 - NOT ALL SYSTEMS
date -u +%Y-%m-%dT%H:%M:%S%z # 2014-08-09T16:21:43+0000
date -u +%Y-%m-%dT%H:%M:%S%:z # 2014-08-09T16:21:43+00:00 - NOT ALL SYSTEMS
date -u +%Y-%m-%dT%H:%M:%SZ # 2014-08-09T16:21:43Z
Consult man strftime as supported formats vary. For instance, some systems support inserting colons into the offset using %:z, %::z, or %:::z - only two of my five systems do (Debian, Ubuntu do, but Mac, BusyBox, QNX do not).
And I often go back to https://en.wikipedia.org/wiki/ISO_8601 for reference.
Yes, date can do this:
[tomalak#lolphin:~] date -R
Mon, 02 May 2011 17:37:45 +0100
Or, more specifically:
[tomalak#lolphin:~] date -R | awk '{print $6}'
+0100
[tomalak#lolphin:~] date +%z
+0100
Reading date --help is very useful.

How can I calculate the date preceding a given date in unix?

I have two variables: X and Y.
The value of X will be a date given in the format mmddyy
and I want to calculate the date preceding that date and have it be returned in in the format yyyymmdd.
Let me give you an example. When X="091509" (mmddyy format) Y should be "20090914" (yyyymmdd format)
~$ date -d "20090101 -1 day"
Wed Dec 31 00:00:00 CET 2008
And if you want to retrieve the date in a custom format you just throw in some format strings.
~$ date -d "2009-09-15 -1 day" +%Y%m%d
20090914
As for your conversion you may use bash substring extraction (assuming you use bash of course). This also assumes that your input is consistent and "safe".
X="091509"
Y=`date -d "${X:4:2}${X:0:2}${X:2:2} -1 day" +%Y%m%d`
echo $Y
See http://www.walkernews.net/2007/06/03/date-arithmetic-in-linux-shell-scripts/
I like Tcl for date arithmetic, even though it's clunky for shell one-liners. Using Tcl 8.5:
x=091509
y=$(printf 'puts [clock format [clock add [clock scan "%s" -format "%%m%%d%%y"] -1 day] -format "%%Y%%m%%d"]' "$x" | tclsh)
Since the -d option doesn't exists on MacOS X or FreeBSD, here is the answer for them
Retrieving a date relative to current time
date -v -1d
For your question in particular, you'll need the -j and -f flags to use a specific date
date -v -1d -j -f %m%d%y 091509 +%Y%m%d
date -d "yesterday"

Resources