1758623 Members
2133 Online
108874 Solutions
New Discussion юеВ

Formatting a date

 
SOLVED
Go to solution
Matt Hearn
Regular Advisor

Formatting a date

Hey all! I have a little script I'm writing that checks logs on a given date for certain security related things (suing to root, failed logins, sudo failures, etc.) It basically works okay, but I'd like to pretty up the subject on the email it sends out with a formatted date (instead of just MM/DD as it does now).

Problem is, I can't find any function that'll format a date other than using /usr/bin/date to format the current date/time. I guess I can use perl, but I'm trying to make the script entirely ksh for portability on a variety of OS versions (perl versions in our environment vary and are in different locations on some boxes).

Do I have any options other than writing a staggeringly lengthy script, or using a command-line perl call? I feel like I knew a command to do this, but I can't come up with it off the top of my head and google and man have failed me.

Thanks!
10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: Formatting a date

What do you want the date to look like?

Without knowing how you want it formatted, it's kind of difficult to make any recommendations.
Tim Nelson
Honored Contributor

Re: Formatting a date

The date command is pretty flexable...

date +%M%D%Y, date +%m%d%y, date +%m-%d-%Y

just about anything is possible.

Did you try to simply read the man pages ? It is a great local source for information on unix commands.

Matt Hearn
Regular Advisor

Re: Formatting a date

I guess I wasn't very clear; I know how to format the date. The problem is that the date command itself only formats TODAY's date (and the current time). I need to be able to type a shell command like:

./security_info.ksh 06/20

or

./security_info.ksh 20080620

and have it send out an email with a subject line of:

"Security Info for Friday, June 20th, 2008"

The script is set up to do that if you just type "./security_info.ksh" (it defaults to the current date and formats accordingly), but on occasion I need to generate an email for a date in the past.

Hope this is clearer. Thanks!
James R. Ferguson
Acclaimed Contributor

Re: Formatting a date

Hi Matt:

Dates in the past can easily be generated by Perl:

# perl -le 'print scalar localtime(time()-(60*60*24))'

Sun Jun 22 11:09:46 2008

...yields exactly yesterday in your localtime.

Now, use the POSIX module to format the date just like you would use the Unix 'date' command:

# perl -MPOSIX -le 'print strftime "%m/%d/%Y %H:%M:%S",localtime(time()-(60*60*24))'

06/22/2008 11:09:48

Regards!

...JRF...
Tim Nelson
Honored Contributor

Re: Formatting a date

If you search through this forum you will find that you are right, using math with date or generating dates and times other than current is not easy without some other assistance.

The perl option is the simplest.

There are others that us a short C program that converts the date to epoch, does the math, then converts back to human.

There is another that is all ksh and breaks out day, month, year, puts into arrays and allows for add/sub that way but is would be something you would wish to call as a sub script.

Do some searching and pick the one you like best.

Otherwise you will have to write something yourself and will run into the same issue as others, what do I do when I reach the EOM, or leap year, or begining of month....What if I want the last work day of the month....

Best of luck..

Dennis Handly
Acclaimed Contributor

Re: Formatting a date

>have it send out an email with a subject line of:
"Security Info for Friday, June 20th, 2008"

It may be better to not format it. Then it will sort by date for the YYYYMMDD format.
Matt Hearn
Regular Advisor

Re: Formatting a date

Thanks, James! Looks like perl is my only option. I'll work it out the portability problems. The only other is this: I do

perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(mktime(0,0,0,21,6,2008))'

Which I would assume should return 6/21/2008, but instead returns:

12/31/1969

So somehow it seems like mktime is returning 0. I'm working on it, but if you can spot any obvious idiocy on my part it would be appreciated. :)
James R. Ferguson
Acclaimed Contributor
Solution

Re: Formatting a date

Hi (again) Matt:

In you last post:

# perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(mktime(0,0,0,21,6,2008))'

...this should be:

# perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(mktime(0,0,0,21,5,108))'

...assuming that you were trying to show June 21, 2008. The month is zero-relative. The year is the year since 1900.

Notice that you don't call 'mktime()' as it is implied.

If I'm calculating some time in the past or future, I always use the epoch time +- some number of seconds as I first showed.

Regards!

...JRF...
Matt Hearn
Regular Advisor

Re: Formatting a date

Thanks, that fixed it! I'm not calling dates relative to the current date, unfortunately; that would definitely be simpler. I'm specifying a specific date from the command line, hence my use of mktime.