An introduction to the 10 CPAN modules in Perl that operate on dates and times _perl

Source: Internet
Author: User
Tags benchmark datetime time zones iso 8601 posix readable

The ability to manipulate dates and times is fundamental to most programming languages, and Perl is no exception. But when it comes to more complex processing of time, Perl's own features often seem powerless. In this case, it's a good idea to take a look at the Perl comprehensive Collection Network (CPAN), which has a lot of Perl modules ready to use, allowing developers to effectively perform date and time operations. Spend some time browsing the site to see if it is worthwhile to find something to use in the next project.

This article lists the 10 most interesting modules on the Perl cpan that manipulate dates and times, laying the groundwork for your next conversion time zone and calculating the number of seconds between two timestamps, and now we're going to look at them.

Note: You can install the CPAN module directly from the Internet according to the method provided below.

Date::manip

This module is called the "Swiss Army Knife" of the date control module. It provides routines that can be used to analyze and compare dates, extract date information, determine date information, determine date/time offsets, use duplicate dates and times, and use dates in different languages. But it is slower to run than other, more professional modules.
Use this module when you need a generic date-handling module and are not particularly concerned with performance.

Datetime

This module, which is part of the DateTime project of Perl, provides a base class for "representing a combination of date/time". It is the easiest way to create a custom Date object and includes modules for parsing, formatting, calculating durations, and manipulating time zones.
In general, when you need to localize or define a custom storage container for time information, you will use this module as a new date-related object.

Time::format

This module is unmatched when you need to reformat date and time values-it supports a large number of formatting code that can dramatically alter the appearance of dates and times. It accepts both DateTime objects and Date::manip strings, and also provides an interface to the POSIX strftime () function for the added control.
Use this module when you need to format date and time values (usually before displaying them to application users).

Time::interval

This module is useful for processing time intervals. It provides a routine that calculates the total number of days, hours, minutes, and seconds between two timestamps. It also makes it easy to convert the time length to different units of time, such as seconds, hours, or minutes.
Use this module when you need to calculate the elapsed time between two date values.

Date::convert

This module provides routines for converting between different date formats (Gregorian, Julian, Jewish, Islamic, and absolute). It also provides hooks to easily extend to the new calendar format.
Use this module when you need to convert from one calendar format to another.

Benchmark

This module allows you to run the same piece of code more than once, to calculate the time each run is performed and return the average. It can also be used to see how many times a block of code can run at a fixed time window.
Use this module when performing benchmark tests, measuring the speed at which a block of code is running, and collecting accurate readings to guide you through optimization.

Time::normalize

This module provides routines for formatting arbitrary date and time values into a uniform, standard representation that can then be used to compute or save. It checks for errors in the input and returns a separate time and/or date component in a clear format.
Use this module when you suspect that the date you entered is wrong and you need to "clean up" before using it in a database or application.

Regexp::common::time

This module creates regular expressions that can be used to parse dates and times. It supports capturing date patterns from string values using both precise rules and complex fuzzy logic.
Use this module to identify and extract date information from human-readable strings and convert it into machine-readable format (e.g. ISO 8601).

MySQL::D Ateformat

This module provides routines to convert dates and numbers back and forth between MySQL's YYYY-MM-DD format and people's easily understandable strings. This module is useful when retrieving/adding records containing date information from/to the MySQL database.
You can use this module to reformat the MySQL date/time string in order to display it in a more readable way, or to modify a user-supplied date value before inserting it into the MySQL database.

Net::time

This module provides a client interface for retrieving date and time information from a remote client. This is especially useful if your application is always sure to know the current date and time to keep the system from being spied on by hackers.
Use this module when retrieving date/time strings from a remote host over a TCP network.

Use examples:

Get information about a date and time

Method One

Copy Code code as follows:

Use POSIX QW (strftime);
LocalTime (Time ()-24*60*60)
Strftime "%y%m%d%k%m%s", localtime $^t;

Method Two
Copy Code code as follows:

Use 5.010;
Use POSIX QW (strftime);
# These are core modules in Perl 5.10 and newer
Use time::P iece;
Use Time::seconds;

My $yesterday = localtime ()-one_day;
Say $yesterday->strftime ('%b%d%Y ');

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.