Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello Everybody,
I am in dire need of a ksh script that will convert the UNIX epoch time into regular Pacific standard time. Can someone please help me out here :)Thanks in advance,
Mandy

Hi Mandy. The date displayed by the unix date command is influenced by the (optional) TZ environment variable. See "man date" and "man tztab" Try this:
pstdate=`TZ=PST8CDT date`
echo $pstdate
pstdate=`TZ=PST8CDT date "+%Y%m%d %H:%M"`
echo $pstdate

Thanks, James.
I just wanted a clarification. Do I just cut and paste the above lines into a script?? or is this a command line utility??
Can you please explain -- as you see I am a UNIX newbie.
Regards,
Mandy

Sure. Your request was to convert a date, but you did not give any specifics. I don't know what you want to do with the converted date, so I just captured it into an environment variable that I named pstdate. After running one of those commands, you can see the captured value with:
echo $pstdate
And since the TZ reassignment occurs in a nested command, doing it that way will not impact your shell's current TZ setting.
The unix "date" command displays a date in some default format, or you can specify your own formatting such as "date +%Y%m%d". And when creating its output, the date command considers the value of the optional TZ (Time Zone) environment variable. See if you have the TZ variable set by doing:
echo $TZ
I hope I have not confused you more (just keep asking).

James,
I guess I am unusally dense. Here is what I have to do.
< event date="1031676916"I have a set of files all of which have some fields which look like this
where the figure in the quotes is the UNIX epoch time. I just want to replace all those instances of the epoch time with the regular time so that readers of the files can comprehend them.
I hope I have explained my situation better. Thanks for all your help - Mandy

James,
I guess I am unusally dense. Here is what I have to do.
I have a set of files all of which have some fields which look like this
< event date="1031676916"
where the figure in the quotes is the UNIX epoch time. I just want to replace all those instances of the epoch time with the regular time so that readers of the files can comprehend them.I hope I have explained my situation better. Thanks for all your help - Mandy

Sorry, the confusion was mine. Unix bases its date on GMT date plus your local time-zone setting, and I thought you wanted to convert from the basic GMT date, which is just a matter of having TZ set properly. Upon re-reading, I can see you want to convert from epoch time.
You have two issues to deal with. The most difficult is actual converion of epoch time to date. While this is very easy in many languages (including perl and sql), it is a lot of code to do with basic unix scripting.
But someone just asked this same question two posts above this one (Need to convert the date in a SQUID), and someone suggested using ctime. I will try that solution myself next week, but I am taking some vacation days this week.
The second issue (not hard) is unix scripting that will process the file, locate the epoch time in each line within the quotes and substitute the date in its place.
I will check this thread next week, and if you do not have a solution by that time, I will look into it.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |