- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- convert number string to date format
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2004 09:10 PM
09-26-2004 09:10 PM
e.g.
The input strings are
DATESTR=040927
TIMESTR=151421
I need to convert it to:
Mon Sep 27 15:14:21 SST 2004
Could anyone help on this. Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2004 09:28 PM
09-26-2004 09:28 PM
Solution#!/usr/bin/ksh
# time.ksh
### input ####
DATE=$1
TIME=$2
####
day=$(echo $DATE | cut -c 1,2)
mon=$(echo $DATE | cut -c 3,4)
date=$(echo $DATE | cut -c 5,6)
####
hour=$(echo $TIME | cut -c 1,2)
min=$(echo $TIME | cut -c 3,4)
sec=$(echo $TIME | cut -c 5,6)
####
DAY="Sun Mon Tue Wed Thur Fri Sat"
MON="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
# Array #
set -A DARR $DAY
set -A MARR $MON
year=$(date +"%Y")
zone=$(date +"%Z")
DD=${DARR[$day]}
MM=${MARR[$((mon-1))]}
echo "$DD $MM $date $hour:$min:$sec $zone $year"
# End #
exit 0
Save as,
time.ksh
$ksh time.ksh 040927 151421
You will get your desired output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2004 09:56 PM
09-26-2004 09:56 PM
Re: convert number string to date format
Thanks. It is a good one. But there is a little bug.
I got the output like this after I run the script.
Thu Sep 27 15:14:21 SST 2004
But it is suppose to be
Mon Sep 27 15:14:21 SST 2004
i.e the weekday goes wrong.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2004 10:19 PM
09-26-2004 10:19 PM
Re: convert number string to date format
date is followed as,
DAY="Sun Mon Tue Wed Thur Fri Sat"
00 01 02 03 04 05 06
IF you want to make that to Mon=04 then change it accordingly there in the DAY array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2004 03:59 AM
09-28-2004 03:59 AM
Re: convert number string to date format
Muthukumar, your script looks good but I hope you don't mind if I take it and correct some errors. Getting the day of the week is tricky. This is the best I have so far and its very convoluted. I'm sure there's a perl guru out there who could do this in 1 or 2 lines.
# The first 2 chars of DATESTR are the year, not the day.
# To get the day of week, you have to use a starting date.
# and proceed forward year by year, day by day to the date specified.
# Let's use Jan 1, 1900 which was a Monday
year=$(echo $DATESTR | cut -c 1,2)
mon=$(echo $DATESTR | cut -c 3,4)
dayofmonth=$(echo $DATESTR | cut -c 5,6)
if [[ $year -lt 5 ]]
then
# Assume that 00 thru 04 is 21st century, not 20th
year=20$year
else
year=19$year
fi
numyears=$(($year - 1900))
numleapdays=$((($year - 1900) / 4))
# Now adjust number of leap days for dates before March 1 of a leap year
if [[ $(($numyears % 4)) -eq 0 ]]
then
if [[ $mon -lt 3 ]]
then
numleapdays=$(($numleapdays - 1))
fi
fi
# Number of days to Jan 1 of the year
numdays=$((($numyears * 365) + $numleapdays))
set -A dayspermonth 0 31 59 90 120 151 181 212 243 273 304 334
numdays=$(($numdays + ${dayspermonth[$((mon-1))]} + $dayofmonth - 1))
DAY="Sun Mon Tue Wed Thur Fri Sat"
# Array #
set -A DARR $DAY
DD=${DARR[$((numdays % 7))]}
Mike D'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2004 04:17 AM
09-28-2004 04:17 AM
Re: convert number string to date format
It is good to have review on script and
1> We are asked to format as like "Mon Sep 27 15:14:21 SST 2004" from input's there DATESTR=040927
TIMESTR=151421
so that we have to calculate with normal command date +"%Y" for year 2004 and for time zone date +"%Z"
DATESTR=04 09 27
Day management is as like Sun Mon... etc.
Month as like in the month array Jan..
day is as like given in the ouptut
TIMESTR=151421
Just put : as 15:14:21
And required script has not be used to set more time here :-) It is just to display formation of "date" command there.
2> It is good to see your analysis on time and date. It is helpful.
-Best Regards-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2004 04:24 PM
09-28-2004 04:24 PM
Re: convert number string to date format
Thanks for your help.