Get Current date in epoch from Unix shell script - unix

How to get the current date value in epoch i.e., number of days elapsed since 1970-1-1. I need solution in unix shell script.

The Unix Date command will display in epoch time
the command is
date +"%s"
https://linux.die.net/man/1/date
Edit: Some people have observed you asked for days, so it's the result of that command divided by 86,400

Update: The answer previously posted here linked to a custom script that is no longer available, solely because the OP indicated that date +'%s' didn't work for him. Please see UberAlex' answer and cadrian's answer for proper solutions. In short:
For the number of seconds since the Unix epoch use date(1) as follows:
date +'%s'
For the number of days since the Unix epoch divide the result by the number of seconds in a day (mind the double parentheses!):
echo $(($(date +%s) / 60 / 60 / 24))

echo $(($(date +%s) / 60 / 60 / 24))

echo `date +%s`/86400 | bc

Depending on the language you're using it's going to be something simple like
CInt(CDate("1970-1-1") - CDate(Today()))
Ironically enough, yesterday was day 40,000 if you use 1/1/1900 as "day zero" like many computer systems use.

Related

meaning and output of date command in Unix

I am facing difficulty in understanding the below date command and its output in Unix if pass the value of 'end' as 130?
Thanks in advance.
date --date='{{ end }} day' +%s
The date will give the current date time.
With --date='{{ end }} day' you are adding end number of days to current date time.
The +%s outputs the overall Unix timestamp after adding end number of days.
Refer here for more options.
You specify intended date format with --date option:
Which accepts rich set of format options : Please go through this
If you put 130 as value of end unix variable then you will get Date and time after 130 days followed by seconds as well because +%s prints seconds.

Calculate a date in behind 24 hours Hive

My demand is really so silly, so basically I need to go back in time 24 hours in a timestamp column in Hive.
So far, I have tried two different ways but it's not going thru:
select
recordDate, --original date
cast(date_sub(cast(recorddate as timestamp),1) as timestamp), -- going one day behind without hour
cast((cast(cast(recorddate as timestamp) AS bigint)-1*3600) as timestamp) -- crazy year
from mtmbuckets.servpro_agents_events limit 10;
My output looks:
I appreciate the support you can give me.
thanks
There is not straight forward function in hive .
1 Create UDF to do so .
or
Convert date in no of second and do you calculation( -24 *60*60) sec then change back int to data.
use from_unixtime and unix_timestamp to achieve below code.
select from_unixtime(unix_timestamp(recorddate) - 86400)
from mtmbuckets.servpro_agen ts_events limit 10;;
From_unixtime
Convert time string with given pattern to Unix time stamp (in seconds) The result of this function is in seconds.
Unix_timestamp
Converts time string in format yyyy-MM-dd HH:mm:ss to Unix timestamp (in seconds), using the default timezone and the default locale, return 0 if fail: unix_timestamp('2009-03-20 11:30:01') = 1237573801

how to set up a job to have two run windows? Please see sample job below.

How can I set up a Autosys job to run at two separate intervals? Please see the sample job below.
This job needs to run between 00:06 - 00:56 and then again between 04:06 - 04:56. The start_mins can be same.
You can't. Your example requires two jobs. One with run_window: 00:06 - 00:56 and the second with run_window: 04:06 - 04:56.
Syntax This attribute has the following format: run_window:
"time-time" time-time Defines the interval during which a job may
start in the format "hh:mm-hh:mm", where hh denotes hours (in 24-hour
format) and mm denotes minutes. You must enclose the interval in
quotation marks (") and separate the beginning and ending times with a
hyphen (-). The specified interval can overlap midnight, but cannot
encompass more than 24 hours. Limits: Up to 20 alphanumeric characters
If you are running the Job exactly once between 00:06 - 00:56 and 04:06 - 04:56 time frames you can try to set starttime as 00.06 and 04.06 and set
max_run_alarm: 50

Convert unix timestamp to human readable time in mysql

We are trying to convert unix timestamp to human readable time when running mysql commands.
For the unix date we have this working command
select FROM_UNIXTIME(registered) AS "ResolutionDateLine" from tickets
which gives us an readable date like
2012-12-03 09:41:00
But we do also have unix timestamp "seconds" that we need to convert, using the same line as above we get 1970-01-01 01:00:00 but the actual value should be 89 days, 23 hours, 22 minutes and 34 seconds.
Then we tried
select FROM_UNIXTIME(firstresponsetime, "%dd, %Hh, %Im") AS "Response" from tickets
with this result:
01d, 00h, 12m
Does anyone know how to convert this correctly in the mysql command?
Use SEC_TO_TIME (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time) to convert a duration in seconds to a HH:mm:ss notation.
select sec_to_time(3500);
results in
00:58:20
Your will be like
select FROM_UNIXTIME(firstresponsetime, '%d-%m-%Y %H:%i:%s') AS Response from tickets
or you can customize it by change second parameter.
for more please check below link:-
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_from-unixtime

Get yesterday date in Unix - KSH script

The below command is used for getting the yerterdays date in Unix Ksh on HP UX
DATE_STAMP=`TZ=CST+24 date +%m/%d/%Y`
Can somebody let me know what does "CST + 24 date " in above command do?
That command sets the timezone to CST+24 and returns the date in that timezone.
if you are looking for a command to find out yesterday's date, you are better of using the TZ trick esp. if you are in a timezone that observes DST.
use perl one liner instead.
#this takes local time and substracts a day(24*60*60 seconds) and formats the time.
echo `perl -e 'use POSIX; print strftime "%m/%d/%Y%", localtime time-86400;'`
Just a guess on your command - since its yesterday at CST+24 timezone the command returns yesterday's date and if you use CST-24, it retunrs tomorrow's date since the date translates to tomorrows date at CST-24 timezone.
VARIABLE=VALUE COMMAND means that you set the environment variable VARIABLE to VALUE but not persistent but only for the executed command COMMAND.
In your example that means: Execute the date command with the environment variable TZ set to CST+24 (which is Central Standard Time plus 24 hours).
Check out this page http://www.kodkast.com/blogs/unix-shell-scripting/how-to-get-yesterdays-date where you can find out yesterday's date as well as any other previous date in unix shell scripting.

Resources