format date/time presentation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting format date/time presentation
# 1  
Old 04-17-2002
format date/time presentation

1) i am trying to print the timeframe of a log file using the start/end date/time being extracted from the file itself. my problem is how to convert the following numeric date format to the one that i prefer:

Start time: 0204161129
End time : 0204171431

into:

Start time: Apr 16 2002 11:29 AM
End time : Apr 17 2002 14:31 PM

I have a script that is working, but with a horrible 40+ lines into it, and i wanted to, at least, improve it.

2) also, for the "man date" description below, how can i get all those variables, and the value they represent? is there a command # date '+....' for a variable to specify AM or PM on the date/time? i think this will be a very good info too.

thanks a lot!
==============================================
DESCRIPTION
The date utility writes the date and time to standard output
or attempts to set the system date and time. By default,
the current date and time will be written.

Specifications of native language translations of month and
weekday names are supported. The month and weekday names
used for a language are based on the locale specified by the
environment variable LC_TIME; see environ(5).

The following is the default form for the "C" locale:

%a %b %e %T %Z %Y
# 2  
Old 04-18-2002
1) It's not very clear where the century is supposed to come from. But I'll assume that high values get a 19 while low values get a 20.
Code:
#! /usr/bin/ksh

input=0204161309

set -A months Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
echo $input | sed 's/\(..\)/\1 /g' | read yr mo dy hr mn
((yr<50)) && cn=20 || cn=19
((hr<12)) && ss=am || ss=pm
((hr=hr%12))

print  ${months[mo-1]} $dy ${cn}${yr} ${hr}:${mn} $ss

2) "man strftime" has the complete list. And
date "+%p"
will output am or pm.
# 3  
Old 04-18-2002
i'm getting this error on line 18. also, do i have to put $ sign on $hr and $mo, considering they are variables?
what does ${months[$mo-1]} do with reference to the sed command?

thanks...

15 (($hr<12)) && ss=AM || ss=PM
16 ($hr=$hr%12))
17
18 print ${months[$mo-1]} $dy $yr ${hr}:${mn} $ss


[18]: months: subscript out of range
# 4  
Old 04-18-2002
I cut and pasted that script in after testing it. Don't fiddle with it and it will work. It it still fails after you put it back the way it is supposed to be, put a line
echo mo = $mo
just before the print and post the entire script as you are running it and the results of the echo mo.
# 5  
Old 04-18-2002
Thanks, i had to cut & paste the script into notepad then into my tty. It's working.
# 6  
Old 04-19-2002
Perderabo, can you please provide explanation on the text in bold ?
I would like to have a concrete analysis of the script so i can use them
for in the future? Thanks...

#! /usr/bin/ksh

input=0204161309

set -A months Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
echo $input | sed 's/\(..\)/\1 /g' | read yr mo dy hr mn
((yr<50)) && cn=20 || cn=19
((hr<12)) && ss=am || ss=pm
((hr=hr%12))

print ${months[mo-1]} $dy ${cn}${yr} ${hr}:${mn} $ss
# 7  
Old 04-19-2002
The sed command put a space after every two characters, this is how I split the fields up so that I could then read them into seperate variables.

Hmmm...now that I look at it, the hr thing may need some help. First hr holds the hours and if the hr is less than 12, it fine the way it is. But if hr was 13, we need it to be set to 1. And if it is 14, we want 2. This is so we can move from a 24 hour clock to a 12 hour clock. The % operator does this. It is a integer divide, but the result is the remainder, not the quotient. So 14 % 2 means we divide 14 by 12 and we want the remainder which is 2.

The problem that I now see is that if hour is zero, we may want to set it to twelve. This kinda depends on your local customs and whatnot, but this is how my watch works. We can do that with:

((hr=hr%12)) || hr=12

The || does the second command only if the first command fails. And a ((...)) that results in a zero is treated as if it failed. I just love stuff like this.

The set command established an array of 12 elements, one for each month. But the index runs from zero to 11. So I had to subtract 1 from the mo variable to get the proper index. So that expression just pulls out the proper element from the array.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Valid separator in time and date format

Hello. I can use any particular (stupid or not) format when using bash date command. Example : ~> date --date "now" '+%Y-%m-%d %H!%M!%S' 2019-06-03 12!55!33or ~> date --date "now" '+%Y£%m£%d %H¤%M¤%S' 2019£06£03 12¤57¤36 or ~> date --date "now" '+%Y-%m-%d %H-%M-%S' 2019-06-03 12-58-51 ... (4 Replies)
Discussion started by: jcdole
4 Replies

2. UNIX for Dummies Questions & Answers

How to chnage format and subtract time and date and get average.?

Hello All , Please support for below request how to change format and subtract time and date and get average. xxx 13-OCT-15 11.32.18.241000 AM 13-OCT-15 11.35.49.089080 AM xxx 13-OCT-15 11.32.24.000000 AM 13-OCT-15 11.45.17.810904 AM xxx 13-OCT-15 11.32.25.232000 AM ... (1 Reply)
Discussion started by: mirwasim
1 Replies

3. HP-UX

DATE and TIME format help in HP-UX

Hello, I have date format as shown in red color below Fri, Sep 12, 2012 08:38:05 PM Can anyone help me with command to change this format to yyyy-mm-dd and HH:MM:SS Note: Time should be in 24 hours format I really appreciate your help!! Thanks, Elavarasan (4 Replies)
Discussion started by: Elavarasan
4 Replies

4. Shell Programming and Scripting

Military type format date/time conversion

Hello All, I have a requirement to convert a 12 hour format to 24 hour time format and the sample input /out put is below Input Time format : Nov 2 2011 12:16AM Out Put Format : Nov 2 2011 0:16 Input : Nov 2 2011 4:16PM Out Put: Nov 2 2011 16:16 I have done this using a... (6 Replies)
Discussion started by: jambesh
6 Replies

5. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

6. Shell Programming and Scripting

awk & date time format

This gives: grep "qprt -o -dp -N 1 -P" printfile.log.1216 |awk '{print $7, $9}' |grep lpc1z The output like below: lpc1z /cerner/d_prod/print/ccltemp4235396d030a.dat.tmp.1216075330] lpc1z /cerner/d_prod/print/ccltemp4235396d036a.dat.tmp.1216075634] lpc1z... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

7. OS X (Apple)

how to change date and time format in menu bar

Can someone please tell me how to change the time and date format in the menu bar. I get to System Preferences -- Language and Text, format, customize, but I can't figure out what to do next. I want to change, say from Tue May 4 to 5 4 2010. How the heck do I do this? (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. Shell Programming and Scripting

date and time in mm/dd/yyyy hh:mm format

Frens, I have a logfile which has got manier things in it and somewer in that file i have a date in mm/dd/yyyy hh:mm format.... I just need to get the date and time nothing else.... i have searched in this forum for it but got only date in mm-dd-yyyy format.. but i need to search for... (10 Replies)
Discussion started by: smarty86
10 Replies

9. Shell Programming and Scripting

Date and Time format

I want to append current date and time to a file name like filename_090920091210. If I use filename_`date +%d%m%Y%T`, the ouput is filename_0909200912:10:33. How to format this as filename_090920091210. Please let me know. Thanks in advance. (3 Replies)
Discussion started by: srimenon09
3 Replies

10. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies
Login or Register to Ask a Question