Gossamer Forum
Home : General : Perl Programming :

Conversion to unix time

(Page 1 of 2)
> >
Quote Reply
Conversion to unix time
Hi all, I need a script that can directly change real time to unix time. Is there any script that available for this ?

Thanks !
Quote Reply
Re: [nsync] Conversion to unix time In reply to
http://perldoc.perl.org/Time/Local.html

Code:
use Time::Local;
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
Quote Reply
Re: [Volker] Conversion to unix time In reply to
Thanks dude!
Thats only 1 command. May i know whether there is any full script that already done purposely for conversion? just like a program that will ask u to key in the time/date/year then it will process and send you the result. THankss ~
Quote Reply
Re: [nsync] Conversion to unix time In reply to
Have some question regarding to the 'gmtime'
here my script :


$unix_stmp = 1131056063;

$real_time = gmtime($unix_stmp);
@real_time = gmtime($unix_stmp);
print"$real_time\n";


So,i will get this :


Thu Nov 3 22:14:23 2005


Here is my question, if i would like to capture the string
2005(year) and 23(second),what is the command that i should write ? i manage to capture
1) date($real_time[3])
2) month($real_time[4])
3) minute($real_time[1])
4) hour($real_time[2])



Quote Reply
Re: [nsync] Conversion to unix time In reply to
or got another option ......my time here is GMT+8:00.....while i using 'gmtime' will always give me the GMT time.
So got any solution to this problem ? i would like to show the time in my timezone which is GMT+8:00.

THanks for help ~
Quote Reply
Re: [nsync] Conversion to unix time In reply to
Code:
my $unix_stmp = time;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($unix_stmp);
$year += 1900;
$mon += 1;

print "$year $sec\n";

http://perldoc.perl.org/functions/gmtime.html is your friend, again.

For your second question try localtime http://perldoc.perl.org/...tions/localtime.html.
Quote Reply
Re: [nsync] Conversion to unix time In reply to
This sounds like a school assignment?
Quote Reply
Re: [Hargreaves] Conversion to unix time In reply to
Isn't it? Tongue

Learning by doing...
Quote Reply
Re: [Volker] Conversion to unix time In reply to
Thanks ya volker !
LOL ....its look like school assignment..rite? hehe ..

let me try first .....thanks again LaughLaugh
Quote Reply
Re: [Volker] Conversion to unix time In reply to
To all expert :

Need some help again Sly
Let said .....i would like to get input from user, and i would like limit their input, my code as below :

print "Please enter 1-60\n";
$option = <STDIN>;

For this case,let say user input a or b or 90 or 100, this code will reject it, and ask the user re-enter again. So, how can i make this ?? any help from you guy ? Laugh Thanks !
Quote Reply
Re: [nsync] Conversion to unix time In reply to
In Reply To:
To all expert :

Need some help again Sly
Let said .....i would like to get input from user, and i would like limit their input, my code as below :

print "Please enter 1-60\n";
$option = <STDIN>;

For this case,let say user input a or b or 90 or 100, this code will reject it, and ask the user re-enter again. So, how can i make this ?? any help from you guy ? Laugh Thanks !



anyone could help on this ? perhaps i need user to enter from 0-60.
THanks
Quote Reply
Re: [nsync] Conversion to unix time In reply to
Code:
do {
print "Please enter a value between 0 and 60";
chomp($option = <STDIN>);
} while ($option > 0 or $option > 60);
Quote Reply
Re: [Hargreaves] Conversion to unix time In reply to
hey fren, thanks for replying, but it seem like cannot work as well ..it cannot detect alhpabet as a wrong input. For my case, i only want accept numbe from 0-60 ~
any idea ?
Thanks ya ~

Last edited by:

nsync: Nov 15, 2005, 1:02 PM
Quote Reply
Re: [nsync] Conversion to unix time In reply to
Oops sorry, try:

Code:
do {
print "Please enter a value between 0 and 60";
chomp($option = <STDIN>);
} while ($option !~ /^\d\d?$/ or ($option < 0 or $option > 60));
Quote Reply
Re: [Hargreaves] Conversion to unix time In reply to
yeah ! solve..thanks dude ! :D
Quote Reply
Re: [nsync] Conversion to unix time In reply to
to all pro,

have another question regarding to time (GMT).

print "Please enter unix time stamp\n";
chomp($unix_stmp = <STDIN>);
use Time::Local;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($unix_stmp);
$year += 1900;
$mon += 1;
print "Real time = $hour:$min:$sec $mday/$mon/$year\n";


Above is my coding. It will get unix time from user and convert it to real time. I have some question on it. I try to add 8 hours with my code but it cause lot of trouble, the date/month/year might change as well. below is my question

1. the time i get is in GMT, i would like to get the answer in my area which is GMT + 8. So any idea on this ? (others than add 8 hours to $hour)


thanks lot if can help !

Last edited by:

nsync: Nov 17, 2005, 5:43 AM
Quote Reply
Re: [nsync] Conversion to unix time In reply to
Code:
print "Please enter unix time stamp\n";
chomp($unix_stmp = <STDIN>);
$unix_stmp += 8 * 60 * 60; # 8 hours
use Time::Local;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($unix_stmp);
$year += 1900;
$mon += 1;
print "Real time = $hour:$min:$sec $mday/$mon/$year\n";

Last edited by:

Volker: Nov 17, 2005, 7:21 AM
Quote Reply
Re: [Volker] Conversion to unix time In reply to
hey volker !
really thanks ! you help me alot ! next time treat u a meal .. LOL
Quote Reply
Re: [nsync] Conversion to unix time In reply to
To all,

Have another question regarding to gmtime and timelocal. Is there any limitation on the year we input ?

for below code :

use Time::Local;
$month -=
1;
$unix_time = timelocal($sec,$minute,$hour,$date,$month,$year);


i found that the $year cannot be more than 2038, else it will cause an error :

Day too big - 24868 > 24855
sec too small - 24868 < 28800

What is that error mean ?? is that mean the year we input cannot be too far ? Is this the limitation of "localtime" ? anybody can clear my doubt ??

same as below :

print
"\nPlease enter unix time stamp\n";
chomp($unix_stmp = <STDIN>);
$unix_stmpGmt8 = $unix_stmp +
8 * 60 * 60;
use Time::Local;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($unix_stmpGmt8);


if the unix time is too big ( i use >2500 ), it cant get answer. Is this another limitation for command "gmtime" ?

Thanks if can clear my doubt
Quote Reply
Re: [nsync] Conversion to unix time In reply to
It is a limitation. Epoch seconds values are limited by the size of an integer. If you have a 32-bit signed integer holding your Epoch seconds, you can only represent dates from Fri Dec 13 20:45:52 1901 to Tue Jan 19 03:14:07 2038 (GMT). So by 2038, make sure you upgrade your server from 32 to 64 bit =)

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Conversion to unix time In reply to
I have a 64bit processor, so what date can I go up to? Tongue
Quote Reply
Re: [Hargreaves] Conversion to unix time In reply to
=) Haven't done the math, but damned dirty apes may be running the show =)

Anticipated Time::Local Error Overflow Error Messages

"Get your stinking paws off me, you damned dirty ape!"
"You maniacs! You blew it up! Damn you! God damn you all to hell!"

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Conversion to unix time In reply to
Thanks dude !! you clear my doubt :D
thanks lot man ~
Quote Reply
Re: [nsync] Conversion to unix time In reply to
Hi all

Have another issue that would like to bring out here...If the $sec/$hour/$minute/$date/$month is less than 10, it will show as 1 number only ...but this make the display look weird,for example

1:2:3 2/3/1999
H:M:S D/M/Y

So, if i would like to make the minute/second/hour become 2 digit......

01:02:03

how can i make this ? any code can make for this conversion..?

sorry for noob question..LOL
Quote Reply
Re: [nsync] Conversion to unix time In reply to
Use the Perl printf function. For example:

printf ("%02d", $sec);

will output 2 as 02.

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
> >