Sponsored Content
Top Forums Shell Programming and Scripting date arthemetic - really urgent Post 302095355 by Perderabo on Monday 6th of November 2006 12:00:29 PM
Old 11-06-2006
This uses my datecalc script which is available on this site.

Code:
#! /usr/bin/ksh

#
# Initialize a few things

alias datecalc=./datecalc
MJan=1 MFeb=2 MMar=3 MApr=4 MMay=5 MJun=6
MJul=7 MAug=8 MSep=9 MOct=10 MNov=11 MDec=12

#
# Get current date and time and load into Now array removing leading zeros

set +A Now $(date '+%Y %m %e %k %M')
Now[1]=${Now[1]#0}
Now[4]=${Now[4]#0}
echo "Now = ${Now[0]} and ${Now[1]} and ${Now[2]} and ${Now[3]} and ${Now[4]}"

#
# Here is the timestamp
timestamp="Nov 3 11:04"

#
# Parse timestamp into the input array removing leading zeros from the numbers

IFS=": "
set +A input $(echo $timestamp)
input[2]=${input[2]#0}
input[3]=${input[3]#0}
echo "input = ${input[0]} and ${input[1]} and ${input[2]} and ${input[3]}"

#
# Load TS array with month, day, hour, and min of timestamp and remove leading zeros
# Guess the timestamp year

eval TS[1]=\$M${input[0]}
TS[2]=${input[1]}
TS[3]=${input[2]}
TS[4]=${input[3]}
(( ${TS[1]} > ${Now[1]} )) && ((TS[0]=${Now[0]}-1)) || TS[0]=${Now[0]}
echo "TS = ${TS[0]} and ${TS[1]} and ${TS[2]} and ${TS[3]} and ${TS[4]}"

#
#  Get number of days expressed as minutes
days=$(datecalc -a ${Now[0]} ${Now[1]} ${Now[2]} - ${TS[0]} ${TS[1]} ${TS[2]})
echo days = $days
((minutes = days * 60 * 24))
echo minutes = $minutes

#
#  Get the minutes past midnight for each date and then calc the delta
((MPM_Now=${Now[3]}*60+${Now[4]} ))
echo Now $MPM_Now ${Now[3]} ${Now[4]}
((MPM_TS=${TS[3]}*60+${TS[4]} ))
echo TS $MPM_TS ${TS[3]} ${TS[4]}
((MPM_delta=MPM_Now-MPM_TS))
echo delta = $MPM_delta

#
# Calc final answer
((minutes=minutes+MPM_delta))
echo And the answer is  that $timestamp was $minutes minutes ago
exit 0

And a sample run:
Code:
$ ./tsdate
Now = 2006 and 11 and 6 and 13 and 13
input = Nov and 3 and 11 and 4
TS = 2006 and 11 and 3 and 11 and 4
days = 3
minutes = 4320
Now 793 13 13
TS 664 11 4
delta = 129
And the answer is that Nov 3 11 04 was 4449 minutes ago

This is a little tricky, so I left the debug statements in so you can see what's going on.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

date problem -- URGENT

Hi All, When i run date command at unix prompt, i get the output in format Tue Aug 29 12:36:18 GMT 2006 when i write the shell script i should be able to export the date output with format as YYYY/MM/DD-HH:MM:SS into some variable(ex : dateop). and when i say echo ${dateop} i should... (9 Replies)
Discussion started by: rkrgarlapati
9 Replies

2. UNIX for Advanced & Expert Users

URGENT,URGENT- Need help tape drive installation

Hi, I am trying to attach tape drive to sun V890 running Solaris 9 on it. I have installed HBA(qlogic) in slot 1 of 0-8 slots and booted the system. I do not see HBAin prtdiag output. The tape drive is not attached to HBA. The tape drive I am going to attach is Sony AIT3. 1.How can I make... (3 Replies)
Discussion started by: sriny
3 Replies

3. Shell Programming and Scripting

time arthemetic - really urgent

guys.. i currently use this logic to calculate date and time. export day=`date +%d` export month=`date +%m` export day_len=${#day} export mon_len=${#month} export hr_min=0000 if ] || ] then month=0$month day=0$day export time_search=$month$day$hr_min else export... (5 Replies)
Discussion started by: ragha81
5 Replies

4. Linux

date subtraction(URGENT)

Hi all, I need the date subtraction fuctionality using shell commands. For example: date1:Wed Apr 5 08:35:21 IST 2006 date2:Tue Apr 4 10:35:44 IST 2006 I need the date subtraction result like " 22 hours 23 seconds". Please guide me to complete this task. Can you please help me ASAP. ... (3 Replies)
Discussion started by: uday123
3 Replies

5. Shell Programming and Scripting

Need to zip the files date wise --urgent Help needed

Hi all, this is my first post, i need to write a script to zip the files with datewise below are the log files. -rw------- 1 root sso 85316156 May 24 22:11 core_test_smservaz_104_104_1243217459_8896 -rw------- 1 root sso 90413304 May 25 22:12 core_test_smservaz_104_104_1243303895_20912... (4 Replies)
Discussion started by: lcschandu
4 Replies

6. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

7. Shell Programming and Scripting

URGENT Reading a file and assessing the syntax shell script URGENT

I am trying to write a shell script which takes an input file as an arguement in the terminal e.g. bash shellscriptname.sh input.txt. I would like for the file to be read line by line each time checking if the .txt file contains certain words or letters(validating the syntax). If the line being... (1 Reply)
Discussion started by: Gurdza32
1 Replies

8. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

9. UNIX for Beginners Questions & Answers

Urgent for date increment

hi all, i would like to increment the date variable i am using for((i=20190731;i<=20190801;i++)) do done after 20190731 it should be 20190801 but this taking as 20190732,20190733.... kindly help me to solve this (3 Replies)
Discussion started by: prathaban
3 Replies
IDATE(3)								 1								  IDATE(3)

idate - Format a local time/date as integer

SYNOPSIS
int idate (string $format, [int $timestamp = time()]) DESCRIPTION
Returns a number formatted according to the given format string using the given integer $timestamp or the current local time if no time- stamp is given. In other words, $timestamp is optional and defaults to the value of time(3). Unlike the function date(3), idate(3) accepts just one char in the $format parameter. PARAMETERS
o $format - The following characters are recognized in the $format parameter string +------------------+---------------------------------------------------+ |$format character | | | | | | | Description | | | | +------------------+---------------------------------------------------+ | | | | B | | | | | | | Swatch Beat/Internet Time | | | | | | | | d | | | | | | | Day of the month | | | | | | | | h | | | | | | | Hour (12 hour format) | | | | | | | | H | | | | | | | Hour (24 hour format) | | | | | | | | i | | | | | | | Minutes | | | | | | | | I (uppercase i) | | | | | | | returns 1 if DST is activated, 0 otherwise | | | | | | | | L (uppercase l) | | | | | | | returns 1 for leap year, 0 otherwise | | | | | | | | m | | | | | | | Month number | | | | | | | | s | | | | | | | Seconds | | | | | | | | t | | | | | | | Days in current month | | | | | | | | U | | | | | | | Seconds since the Unix Epoch - January 1 1970 | | | 00:00:00 UTC - this is the same as time(3) | | | | | | | | w | | | | | | | Day of the week ( 0 on Sunday) | | | | | | | | W | | | | | | | ISO-8601 week number of year, weeks starting on | | | Monday | | | | | | | | y | | | | | | | Year (1 or 2 digits - check note below) | | | | | | | | Y | | | | | | | Year (4 digits) | | | | | | | | z | | | | | | | Day of the year | | | | | | | | Z | | | | | | | Timezone offset in seconds | | | | +------------------+---------------------------------------------------+ o $timestamp - The optional $timestamp parameter is an integer Unix timestamp that defaults to the current local time if a $timestamp is not given. In other words, it defaults to the value of time(3). RETURN VALUES
Returns an integer. As idate(3) always returns an integer and as they can't start with a "0", idate(3) may return fewer digits than you would expect. See the example below. ERRORS
/EXCEPTIONS Every call to a date/time function will generate a E_NOTICE if the time zone is not valid, and/or a E_STRICT or E_WARNING message if using the system settings or the $TZ environment variable. See also date_default_timezone_set(3) CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.1.0 | | | | | | | Now issues the E_STRICT and E_NOTICE time zone | | | errors. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 idate(3) example <?php $timestamp = strtotime('1st January 2004'); //1072915200 // this prints the year in a two digit format // however, as this would start with a "0", it // only prints "4" echo idate('y', $timestamp); ?> SEE ALSO
date(3), getdate(3), time(3). PHP Documentation Group IDATE(3)
All times are GMT -4. The time now is 03:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy