Solved

# Convert to UTC

Posted on 2003-02-27
Medium Priority
1,956 Views
I am getting date from a 'form'.
In MM-DD-YYYY format. I need to store this date in UTC time.
Can anybody help, how to convert date from 'form' to 'UTC'?
Using perl only.

Any suggerstions
Thanks
0
Question by:kooldiamond
• 4
• 2

LVL 2

Accepted Solution

perldork earned 140 total points
ID: 8036897
Hi, you can use perl modules like Date::Calc and Date::Manip to do this .. just for fun I tried to see if I could do this myself and i came up with this:

my \$date = \$ARGV[0] || '02-04-2003';

my @months = qw(31 28 31 30 31 30 31 31 30 31 30 31);

my (\$mon, \$day, \$year) = split('-', \$date);
my \$seconds = ((\$year - 1970) * 365 * 24 * 60 * 60);
my \$leap_days = 0;

for (my \$i = \$year; \$i > 1970; \$i--) {
if (\$i % 4 == 0 || \$i % 100 == 0) {
\$leap_days++;
}
}

\$seconds += (\$leap_days * 24 * 60 * 60);

for (my \$i = 0; \$i < (\$mon - 1); \$i++) {
\$seconds += (\$months[\$i] * 24 * 60 * 60);
}

\$seconds += (\$day * 24 * 60 * 60);

print scalar localtime(\$seconds);

I have tested it with a bunch of dates and it seems to produce the correct date.

Note that this will only work for dates newer than Jan 1, 1970 .. as you specified UTC .. and 1/1/1970 is the beginning of UNIX time (the "epoch.")

0

LVL 2

Expert Comment

ID: 8036904
Note also that this only works to day granularity .. not hours/minutes/seconds.
0

Author Comment

ID: 8057722
perldok

Thanks a lot!!!
Its working fine in my code now :)
One real quick question, If we want to stop the date passed in past? How we can inculcate that feature?

Thanks
0

LVL 2

Expert Comment

ID: 8058117
What do you mean by that?  Do you mean you want to stop dates that are before 01-01-1970?
0

Author Comment

ID: 8067507
I mean if the date is not 'Today'
If the date entered is in 'Past' can we stop that?
0

LVL 2

Expert Comment

ID: 8067676
Sure ... here is the code you need to get the values for

the month
the day
the year

that make up 'today' ...

perldoc -f localtime

my (\$mday, \$mon, \$year) = (localtime())[3,4,5];

\$mon++;
\$year += 1900;

print "Today is \$mon/\$mday/\$year\n";
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
###### Suggested Courses
Course of the Month9 days, 5 hours left to enroll