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

  • Home
  • SEARCH
  • 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 604019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:59:36+00:00 2026-05-13T16:59:36+00:00

We’re currently using a 3rd party API that provides datetime in the following format:

  • 0

We’re currently using a 3rd party API that provides datetime in the following format:

Sat Mar 06 09:00:00 ICST 2010
Fri Feb 19 19:30:00 JDT 2010
Fri Feb 19 19:30:00 PST 2010

However, we want to store these datetime objects in MySQL in a standard datetime field which requires the following format:

YYYY-MM-DD HH:MM:SS

Right now we’re using the following code which is reporting errors for certain timezones such as KDT, JDT, and ICST:

use Date::Manip;
use DateTime;
use DateTime::Format::DateManip;

my $date = ParseDate($time);
$date = DateTime::Format::DateManip->parse_datetime($date);
eval{ $time = $date->strftime("%Y-%m-%d %H:%M:%S"); };

Can you recommend a better implementation of the Perl code above to convert the datetime objects from the API to the proper format and time on our server to be inserted into a MySQL datetime field?

Thanks in advance for your help & advice!

  • 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. Editorial Team
    Editorial Team
    2026-05-13T16:59:37+00:00Added an answer on May 13, 2026 at 4:59 pm

    Store the times internally in GMT. Do all manipulations in GMT. Then at the last moment, just as you’re about to display results to the user, then convert to the user’s local time.

    I recommend using Date::Parse, but you’ll have to augment its timezone offsets because it doesn’t currently have Indochina Summer Time and Japan Daylight Time, for example.

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    use Date::Format;
    use Date::Parse;
    
    # add timezone offsets
    $Time::Zone::Zone{icst} = +7*3600;
    $Time::Zone::Zone{jdt}  = +9*3600;
    
    while (<DATA>) {
      chomp;
      warn("$0: failed conversion for $_\n"), next
        unless defined(my $time_t = str2time $_);
    
      my @t = gmtime($time_t);
      print $_, " => ", strftime("%Y-%m-%d %H:%M:%S", @t), "\n";
    }
    
    __DATA__
    Sat Mar 06 09:00:00 ICST 2010
    Fri Feb 19 19:30:00 JDT 2010
    Fri Feb 19 19:30:00 PST 2010
    

    Output:

    Sat Mar 06 09:00:00 ICST 2010 => 2010-03-06 02:00:00
    Fri Feb 19 19:30:00 JDT 2010 => 2010-02-19 10:30:00
    Fri Feb 19 19:30:00 PST 2010 => 2010-02-20 03:30:00

    To support the query you’d like, store the time in GMT plus an offset (i.e., from GMT to the local time from the API). Note that the code below assumes that if str2time can parse a given time, strptime can also. Change the loop to

    my @dates;
    while (<DATA>) {
      chomp;
      warn("$0: failed conversion for $_\n"), next
        unless defined(my $time_t = str2time $_);
    
      my $zone = (strptime $_)[-1];
      my @t = gmtime($time_t);
      push @dates => [ strftime("%Y-%m-%d %H:%M:%S", @t)
                     , sprintf("%+03d:%02d",
                               int($zone / 3600),
                               int($zone % 3600) / 60)
                     , $_
                     ];
    }
    

    With the times collected, render it as SQL:

    print "DROP TABLE IF EXISTS dates;\n",
          "CREATE TABLE dates (date DATETIME, offset CHAR(6));\n",
          "INSERT INTO dates (date,offset) VALUES\n",
            join(",\n\n" =>
              map("  -- $_->[2]\n" .
                  "  ('$_->[0]','$_->[1]')", @dates)),
            ";\n",
          "SELECT CONVERT_TZ(date,'+00:00',offset) FROM dates;\n"
    

    The output is

    DROP TABLE IF EXISTS dates;
    CREATE TABLE dates (date DATETIME, offset CHAR(6));
    INSERT INTO dates (date,offset) VALUES
      -- Sat Mar 06 09:00:00 ICST 2010
      ('2010-03-06 02:00:00','+07:00'),
    
      -- Fri Feb 19 19:30:00 JDT 2010
      ('2010-02-19 10:30:00','+09:00'),
    
      -- Fri Feb 19 19:30:00 PST 2010
      ('2010-02-20 03:30:00','-08:00');
    SELECT CONVERT_TZ(date,'+00:00',offset) FROM dates;
    

    and we can pipe it to mysql:

    $ ./prog.pl | mysql -u username -D dbname
    CONVERT_TZ(date,'+00:00',offset)
    2010-03-06 09:00:00
    2010-02-19 19:30:00
    2010-02-19 19:30:00
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ok I add this as another answer because the comment… May 15, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer Yes, see the documentation of QNetworkRequest. You'll want to do… May 15, 2026 at 5:25 pm
  • Editorial Team
    Editorial Team added an answer Using @Rollback(value = false) annotation on your test case that… May 15, 2026 at 5:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.