Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 98833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:16:19+00:00 2026-05-11T00:16:19+00:00

I’ve used the localtime function in Perl to get the current date and time

  • 0

I’ve used the localtime function in Perl to get the current date and time but need to parse in existing dates. I have a GMT date in the following format: ‘20090103 12:00’ I’d like to parse it into a date object I can work with and then convert the GMT time/date into my current time zone which is currently Eastern Standard Time. So I’d like to convert ‘20090103 12:00’ to ‘20090103 7:00’ any info on how to do this would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:16:20+00:00Added an answer on May 11, 2026 at 12:16 am

    Because the Perl built in date handling interfaces are kind of clunky and you wind up passing around a half dozen variables, the better way is to use either DateTime or Time::Piece. DateTime is the all-singing, all-dancing Perl date object, and you’ll probably eventually want to use it, but Time::Piece is simpler and perfectly adequate to this task, has the advantage of shipping with 5.10 and the technique is basically the same for both.

    Here’s the simple, flexible way using Time::Piece and strptime.

    #!/usr/bin/perl  use 5.10.0;  use strict; use warnings;  use Time::Piece;  # Read the date from the command line. my $date = shift;  # Parse the date using strptime(), which uses strftime() formats. my $time = Time::Piece->strptime($date, '%Y%m%d %H:%M');  # Here it is, parsed but still in GMT. say $time->datetime;  # Create a localtime object for the same timestamp. $time = localtime($time->epoch);  # And here it is localized. say $time->datetime; 

    And here’s the by-hand way, for contrast.

    Since the format is fixed, a regular expression will do just fine, but if the format changes you’ll have to tweak the regex.

    my($year, $mon, $day, $hour, $min) =      $date =~ /^(\d{4}) (\d{2}) (\d{2})\ (\d{2}):(\d{2})$/x; 

    Then convert it to Unix epoch time (seconds since Jan 1st, 1970)

    use Time::Local; # Note that all the internal Perl date handling functions take month # from 0 and the year starting at 1900.  Blame C (or blame Larry for # parroting C). my $time = timegm(0, $min, $hour, $day, $mon - 1, $year - 1900); 

    And then back to your local time.

    (undef, $min, $hour, $day, $mon, $year) = localtime($time);  my $local_date = sprintf '%d%02d%02d %02d:%02d\n',     $year + 1900, $mon + 1, $day, $hour, $min; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.