Computing.Net > Forums > Unix > Script to convert UNIX time to PST

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Script to convert UNIX time to PST

Reply to Message Icon

Name: Amanda Brown
Date: September 23, 2002 at 19:24:58 Pacific
OS: Solaris5.7
CPU/Ram: 1GB
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: September 24, 2002 at 06:39:19 Pacific
Reply:

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


0

Response Number 2
Name: Amanda Brown
Date: September 24, 2002 at 08:15:01 Pacific
Reply:

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


0

Response Number 3
Name: James Boothe
Date: September 24, 2002 at 12:10:51 Pacific
Reply:

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).


0

Response Number 4
Name: Amanda Brown
Date: September 24, 2002 at 17:16:40 Pacific
Reply:

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


0

Response Number 5
Name: Amanda Brown
Date: September 24, 2002 at 17:18:43 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: James Boothe
Date: September 27, 2002 at 09:06:27 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Script to convert UNIX time to PST

script to convert date/time to seco www.computing.net/answers/unix/script-to-convert-datetime-to-seco/3795.html

unix script to submit html form www.computing.net/answers/unix/unix-script-to-submit-html-form/1324.html

How to call unix script from pl/sql www.computing.net/answers/unix/how-to-call-unix-script-from-plsql/7767.html