value of variable based on time of the day


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting value of variable based on time of the day
# 1  
Old 04-12-2009
value of variable based on time of the day

i would need some help in setting the value of a variable (TIME_NOW) depending on the time of the day ...e.g.

if today's date is 12th April 2009 and if the current time is between midnight [00:00:00] and 16:59:59 hrs then the TIME_NOW should be yesterday's date i.e. TIME_NOW=11
else if the current time between 17:00:00 [hh mm ss] hrs and midnight [00:00:00] .... then the TIME_NOW should be set to today's date i.e. TIME_NOW=12
i tried the following logic

Code:
calculate_time_now ()

{



        typeset -i Date=$(date +%H)
    if (( $Date >= 17 ))
    then

        time_now=$(date +%d)

    else

        time_now=$(TZ=PST+24 date +%d)

    fi

        echo $time_now

}

but at some time around midgnight ... it is not working [at 0038 hrs on 11th april 2009 ... it calculated time_now as 10 {instead of 11}].

Please advise.
# 2  
Old 04-12-2009
Keep it simple method?

Code:
TESTHOUR=`date +%H`
TODAYSDATE=`date +%d`

if [ ${TESTHOUR} -ge 17 ]; then
  TIMENOW=${TODAYSDATE}
else
  TIMENOW=`expr ${TODAYSDATE} - 1`
fi
echo ${TIMENOW}

# 3  
Old 04-13-2009
Quote:
Originally Posted by TonyFullerMalv
Keep it simple method?

Code:
TESTHOUR=`date +%H`
TODAYSDATE=`date +%d`


That will fail if the date changes between the two calls to date; more reliable (and more efficient) is:

Code:
eval "$(date +"TESTHOUR=%H TODAYSDATE=%d")"

Quote:
Code:
if [ ${TESTHOUR} -ge 17 ]; then
  TIMENOW=${TODAYSDATE}
else
  TIMENOW=`expr ${TODAYSDATE} - 1`


First, there is no need to use expr; the Unix shell has arithmetic builtin:

Code:
TIMENOW=$(( $TODAYSDATE - 1 ))

However, that will fail if $TODAYSDATE is the first day of the month.
Quote:
Code:
fi
echo ${TIMENOW}

# 4  
Old 04-14-2009
Thanks for your help. It was enligthening.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting a Date value based on day

Hi, I have a scenario like this. I get a file on anyday of the week, so the file name is like this. FILE_NAME_YYYYMMDD I want to get the tuesday date before this day. For example FILE_NAME_20180413 I should get the date as 20180410 FILE_NAME_20180404 I should get the date as 20180403... (5 Replies)
Discussion started by: dnat
5 Replies

2. Shell Programming and Scripting

Awk: time intervals based on epoch time

I have a list of epoch times delimited by "-" as follows: 1335078000 - 1335176700 1335340800 - 1335527400 1335771300 - 1335945600 1336201200 - 1336218000 The corresponding dates are: 20120422 1000 - 20120423 1325 20120425 1100 - 20120427 1450 20120430 1035 - 20120502 1100 ... (3 Replies)
Discussion started by: alex2005
3 Replies

3. Shell Programming and Scripting

Get day of week from epoch time

Need assistance . Below code gives me the date but I wanted output as day of the week (wday) . Code: use Time::Local; my $time=timelocal(1,2,3,9,11,2013); $theTime = localtime($time); print "$theTime\n"; Result: Mon Dec 9 03:02:01 2013 Wanted output as only Mon (2 Replies)
Discussion started by: ajayram_arya
2 Replies

4. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

5. UNIX for Advanced & Expert Users

Need to get the time difference for all the transactions in a day

Hello Experts, I need to evaluate my API performance, so need a script to get the time difference for all the transaction that has gone through my application in a day. The challenge is the multi threaded logs, so I cant just get all the Telephone Numbers and check the entering and existing... (5 Replies)
Discussion started by: samjna
5 Replies

6. Shell Programming and Scripting

Picking contents value of file at run time based on variable values

HI, I have to write a unix script and need your help. in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC like this there will be any type of value in the vcariable. there is a unix directory... (2 Replies)
Discussion started by: manish8484
2 Replies

7. AIX

Time of Day Question

I have a question i need clarification. We had a problem on a p520 system and had to pull the battery on the service processor card. That did the trick and system booted. We forgot to set the time in the service processor before booting. Once AIX was booted we changed the date and time there. ... (1 Reply)
Discussion started by: ryan0911
1 Replies

8. Shell Programming and Scripting

How to? Every day, same time... script...

I manage to find and proces one script, now i need to automate this script for every day at the same time... (2 Replies)
Discussion started by: voltaza
2 Replies

9. UNIX for Dummies Questions & Answers

I want to seperate my data by time of day

Hi, I'm a newbie to unix. I have a txt file with my data, a list of events. Each event comes with a unix time. I want to seperate a months worth of events into those that occur during the day, and those at night. I have no clue how to go about this, please can someone push me in the right... (6 Replies)
Discussion started by: pmasterkim
6 Replies
Login or Register to Ask a Question