Solved

# Convert to UTC

Posted on 2003-02-27
Medium Priority
1,951 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
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 Month7 days, 21 hours left to enroll