Computing.Net > Forums > Unix > set variables from date conversion

set variables from date conversion

Reply to Message Icon

Original Message
Name: ShapeShifter224
Date: January 25, 2005 at 10:05:09 Pacific
Subject: set variables from date conversion
OS: hp-ux
CPU/Ram: unknown
Comment:

I have converted a given date to julian date, done some calculations (add, subtract,etc)and have converted the result back to gregorian date - but I can not figure out how to split the result back into the variables new_YY new_MM new_DD

In this example I am working with yesterday , but in other calcs it will be some given day - 60 or whatever. I have the calcs ok, but I just can not seem to figure out how to take the result and set it to new_YY, new_DD, new_MM.

Can someone help me out? Thanks :)

#!/bin/ksh
>
> TY=$(date '+%Y') # Year for today - MUST be YYYY
> TM=$(date '+%m') # Month for today
> TD=$(date '+%d') # Day for today
>
> JD=$(get_astro_JD TD TM TY) # today's Astro-Julian date
> # yesterday's date
> yesterdays_date_str=$(get_greg_from_JD $((JD-1)) )
> # parse yesterdays date string
> set - $(echo $yesterdays_date_str)
> echo $1 # month - this gives 01242005 MMDDYYYY
> echo $2 # day - this is not working
> echo $3 # year - this is not working
> # end script

I need to set new_YY, new_DD, new_MM from the yesterdays_date_str.

Brenda


Report Offensive Message For Removal


Response Number 1
Name: vgersh99
Date: January 25, 2005 at 10:35:48 Pacific
Reply: (edit)

new_YY=$(echo ${yesterdays_date_str} | sed -e 's/^.*\(....\)$/\1/g')

new_DD=$(echo ${yesterdays_date_str} | sed -e 's/^.*\(..\)....$/\1/g')

new_MM=$(echo ${yesterdays_date_str} | sed -e 's/^\(..\).*/\1/g')


Report Offensive Follow Up For Removal

Response Number 2
Name: ShapeShifter224
Date: January 25, 2005 at 11:02:35 Pacific
Reply: (edit)

thank you vgersh99 :)

how would you set YY to just be YY not CCYY?

thank you so much for your help!

Brenda


Report Offensive Follow Up For Removal

Response Number 3
Name: vgersh99
Date: January 25, 2005 at 11:17:47 Pacific
Reply: (edit)

new_YY=$(echo ${yesterdays_date_str} | sed -e 's/^.*\(..\)$/\1/g')


Report Offensive Follow Up For Removal

Response Number 4
Name: thepubba
Date: January 25, 2005 at 13:30:05 Pacific
Reply: (edit)

Since you're using the ksh, you can use the typeset built-in to cut the string into 2 pieces. An example script is:

#!/bin/ksh

Date=01242005
print $Date

typeset -R4 TheYear=$Date
typeset -L4 MonthYear=$Date

print $TheYear
print $MonthYear

typeset -R2 TheMonth=$MonthYear
typeset -L2 TheDate=$MonthYear
typeset -R2 TheShortYear=$TheYear

print $TheMonth
print $TheDate
print $TheShortYear

See if you can incorporate the typeset into your functions. You avoid having to make a call outside the shell (in the solution, it is to sed) when it is not necessary. Additionally, when you use Korn shell, you don't need to use echo (which is also an external function). Use print, which is a shell built-in just like the typeset command.

The sed command is very powerful and does the job. However, for trival tasks such as what you are trying to do, it is overkill. Additionally, if you are using a newer Korn shell (such as ksh93), you can easily cut up a string as follows:

DateString=01242005

TheMonth=${DateString:0:2}
TheDay=${DateString:2:2}
TheYear4=${DateString:4:4}
TheYear2=${DateString:6:2}

print $TheYear2
05
print $TheYear4
2005
print $TheMonth
01
print $TheDay
24

Jerry Lemieux



Report Offensive Follow Up For Removal

Response Number 5
Name: vgersh99
Date: January 25, 2005 at 13:56:16 Pacific
Reply: (edit)

agree with all of the above!

or [in ksh]:

DateString=01242005

month="${DateString%${DateString#??}}"
year="${DateString#${DateString%??}}"

vlad
+
#include<disclaimer.h> ----+


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: set variables from date conversion

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge