NAME

EasyTool - The Library of Perl Functions in Common Usage

SYNOPSIS

use EasyTool;

if(defined(&EasyTool::foo)){
  print "lib is included";
}else{
  print "lib is not included";
}

print EasyTool::is_int(2147483647); #true
print EasyTool::is_int(-2147483648); #true
print EasyTool::is_int(2147483648); #false

print EasyTool::is_id(4294967295); #true
print EasyTool::is_id(4294967296); #false

print EasyTool::is_email("xxx.abc@test.com"); #true

print EasyTool::trim(" \t test\n "); #test

print EasyTool::in('a', {'a' => 1, 'b' => 2}); #true
print EasyTool::in(undef, 1, undef); #true

print EasyTool::ifnull(undef, 1); #1

print EasyTool::read_file("file.in");
print EasyTool::write_file("file.out", "string");
print EasyTool::append_file("file.out", "string");
print EasyTool::delete_file("file.out");

$ra_array = EasyTool::csv_2_array("a.csv");
print EasyTool::array_2_csv("a.csv", $ra_array);

print EasyTool::md5_hex("test"); #'098f6bcd4621d373cade4e832627b4f6'

print EasyTool::crc32("test"); #3632233996

$str = &EasyTool::encode_hex("hello");
print EasyTool::decode_hex($str); #hello

$str = &EasyTool::encode_base64("hello");
print EasyTool::decode_base64($str); #hello

$str = &EasyTool::url_encode('<&%$/ \|=+_]{>@^');
print EasyTool::url_decode($str); #<&%$/ \|=+_]{>@^

$str = &EasyTool::html_encode(";<>&lt\"");
print EasyTool::html_decode($str); #;<>&lt"

print EasyTool::qquote('\n'); #"\\n"
print EasyTool::qquote_bin('\n'); #"\\n"

print EasyTool::dump(['1', {'a' => '1', 'b' => '2'}, undef]); #[1, {"a" => 1, "b" => 2}, ()]

print EasyTool::test_var(); #undef
print EasyTool::test_var(1); #1
print EasyTool::test_var(); #1

print EasyTool::time_2_str('1983-03-07 01:02:03','%yyyy-%MM-%%dd');
print EasyTool::time_2_str('1983-03-07 01:02:03');
print EasyTool::time_2_str('1983-03-07');
print EasyTool::time_2_str('2004-08-28T08:06:00');
print EasyTool::time_2_str('946656000');
print EasyTool::time_2_str(' 1983-03-07 ');
print EasyTool::time_2_str('1983-03-07T01:02:03');


print EasyTool::is_time('1983-03-07 01:02:03');
print EasyTool::time_2_timestamp('1983-03-07 01:02:03');
print EasyTool::hash_2_timestamp({year=>1983,month=>3,day=>7,hour=>1,min=>2,sec=>3});
$rh_time=EasyTool::time_2_hash('1983-03-07 01:02:03'); #{year=>1983,month=>3,day=>7,hour=>1,min=>2,sec=>3}

print EasyTool::now();
print EasyTool::time();
print EasyTool::datetime_now();
print EasyTool::date_now();

print EasyTool::day_of_month(2000,2); #29
print EasyTool::day_of_week('2006-07-02'); #0
print EasyTool::timestamp_set('1983-03-07 01:02:03',{year=>1984,month=>5,day=>10,hour=>5,min=>7,sec=>9});#maybe 453013629
print EasyTool::datetime_set('1983-03-07 01:02:03',{year=>1984,day=>10,min=>7});#'1984-03-10 01:07:03'
print EasyTool::date_set('1983-03-07 01:02:03'',{month=>5,hour=>5,sec=>9});#'1983-05-07'

print EasyTool::timestamp_add('1983-03-07 01:02:03',{year=>1,month=>2,day=>3,hour=>4,min=>5,sec=>6});#maybe 453013629
$datetime=EasyTool::datetime_add('1983-03-07 01:02:03',{year=>1,day=>3,min=>5});#'1984-03-10 01:07:03'
$date=EasyTool::date_add('1983-03-07 01:02:03',{month=>2,hour=>4,sec=>6});#'1983-05-07'

The synopsis above only lists the major methods and parameters.

DESCRIPTION

The EasyTool module aims to provide a easy to use, easy to port function set

you can copy and paste some function to embed into your code as easy as possiable youc can also make some modification on function as you need

First of All

support time from 1971 to 2037
if you want more function,please use EasyDateTime
the time zone used in these function is server local time zone

Notation and Conventions

function name

the 'time' in function name means time_str, please read the description of $time_str

param and return value

$str: $str is a string
$email: $email is a string as be accept as a email address

$file_path: $file_path is the path of file you want to operate
$bytes: $bytes is the content to write or append
$byte_count: $byte_count is the length of $bytes
$delete_num: $delete_num is the number of files be deleted

$ra_array: an array represent the csv content

$time_str: $time_str is the string as be accept as a time 

  Samples can be accepted
  '2004-08-28 08:06:00' ' 2004-08-28 08:06:00 '
  '2004-08-28T08:06:00' '2004/08/28 08:06:00'
  '2004.08.28 08:06:00' '2004-08-28 08.06.00'
  '04-8-28 8:6:0' '2004-08-28' '08:06:00'
  '946656000'

  Which string can be accepted?
  rule 0: Unix Timestamp, an int represent seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) can be accepted
  rule 1: there can be some blank in the begin or end of string e.g. ' 2004-08-28 08:06:00 '
  rule 2: date can be separate by . / or - e.g. '2004/08/28 08:06:00'
  rule 3: time can be separate by . or : e.g. '2004-08-28 08.06.00'
  rule 4: date and time can be join by white space or 'T' e.g. '2004-08-28T08:06:00'
  rule 5: can be (date and time) or (only date) or (only time) e.g. '2004-08-28' or '08:06:00'
  rule 6: year can be 2 digits or 4 digits,other field can be 2 digits or 1 digit e.g. '04-8-28 8:6:0'
  rule 7: if only the date be set then the time will be set to 00:00:00
  if only the time be set then the date will be set to 2000-01-01


$timestamp : unix timestamp, an integer like 946656000
$datetime  : date time string, a string, like '2004-08-28 08:06:00'
$date      : date string, a string like, like '2004-08-28'

$rh_time   : a hash represent a time
$rh_time is a struct like {year=>2000,month=>1,day=>1,hour=>0,min=>0,sec=>0}
if some item in $rh_time is not set ,use default value instead
default values: year=>2000,month=>1,day=>1,hour=>0,min=>0,sec=>0

$rh_offset : a hash represent the offset in two times
$rh_offset is a struct like {year=>0,month=>0,day=>0,hour=>0,min=>0,sec=>0}
if some item in $rh_offset is not set ,use zero instead, integer can be negative
one month: {month=>1} 
one day  : {day=>1}
one month and one day: {month=>1,day=>1}
when you add a time with $rh_offset such as {year=>0,month=>0,day=>0,hour=>0,min=>0,sec=>0}, it will add second first,then
  miniute, hour, day, month, year

$template option:
#===FORMAT
#%datetime   return string like '2004-08-28 08:06:00'
#%date       return string like '2004-08-28'
#%timestamp  return unix timestamp

#===YEAR
#%yyyy       A full numeric representation of a year, 4 digits(2004)
#%yy         A two digit representation of a year(04)

#===MONTH
#%MM         Numeric representation of a month, with leading zeros (01..12)
#%M          Numeric representation of a month, without leading zeros (1..12)

#===DAY
#%dd         Day of the month, 2 digits with leading zeros (01..31)
#%d          Day of the month without leading zeros (1..31)

#===HOUR
#%h12        12-hour format of an hour without leading zeros (1..12)
#%h          24-hour format of an hour without leading zeros (0..23)
#%hh12       12-hour format of an hour with leading zeros (01..12)
#%hh         24-hour format of an hour with leading zeros (00..23)
#%ap         a Lowercase Ante meridiem and Post meridiem  (am or pm)
#%AP         Uppercase Ante meridiem and Post meridiem (AM or PM)

#===MINUTE
#%mm         Minutes with leading zeros (00..59)
#%m          Minutes without leading zeros (0..59)

#===SECOND
#%ss         Seconds, with leading zeros (00..59)
#%s          Seconds, without leading zeros (0..59)

$bool: 1 for true and '' for false

extra knowledge

AM and PM - What is Noon and Midnight?
AM and PM start immediately after Midnight and Noon (Midday) respectively.
This means that 00:00 AM or 00:00 PM (or 12:00 AM and 12:00 PM) have no meaning.
Every day starts precisely at midnight and AM starts immediately after that point in time e.g. 00:00:01 AM (see also leap seconds)
To avoid confusion timetables, when scheduling around midnight, prefer to use either 23:59 or 00:01 to avoid confusion as to which day is being referred to.
It is after Noon that PM starts e.g. 00:00:01 PM (12:00:01)

basic function

foo - check whether this module is be used

if(defined(&EasyTool::foo)){
  print "lib is included";
}else{
  print "lib is not included";
}

is_int - whether $str is integer and $max>$str>=$min

&EasyTool::is_int($str);
&EasyTool::is_int($str, $min);
&EasyTool::is_int($str, $min, $max);

default $max is 2147483648, default min is -2147483648

is_id - whether this is a 32bit unsigned int id, 1<=$id<4294967296

&EasyTool::is_id($id);

is_email - whether this is a valid email address

&EasyTool::is_email($email);

trim - delete blank before and after $str, return undef if $str is undef

$str = &EasyTool::trim($str);

in - whether $word is in the scalars or is the key of the hash after it

&EasyTool::in($word,$word1,$word2,..);
&EasyTool::in($word,$rh);

$word can be undef

ifnull - If $scalar1 is not undef, return $scalar1, else return $scalar2

&EasyTool::ifnull($scalar1,$scalar2)

read_file - Read file in binmode

$bytes=&EasyTool::read_file($file_path)

write_file - Write file in binmode

$byte_count=&EasyTool::write_file($file_path,$bytes)

append_file - Append file in binmode

$byte_count=&EasyTool::append_file($file_path,$bytes)

delete_file - Delete file

$delete_num=&EasyTool::delete_file($file_path)

csv_2_array - Read the content of .csv file and put the data into array_ref of array_ref

$ra_array=&EasyTool::csv_2_array($file_path)

array_2_csv - Write .csv file with the data in $ra_array

&EasyTool::array_2_csv($file_path, $ra_array)

md5_hex - Encrypt by MD5

$md5 = &EasyTool::md5_hex($str)

crc32 - Encrypt by CRC32

$crc = &EasyTool::crc32($str)

encode_hex decode_hex - encode and decode string in HEX

$str = &EasyTool::encode_hex($str)
$str = &EasyTool::decode_hex($str)

encode_base64 decode_base64 - encode and decode string in Base64

Every 3*8bit will be convert into 4*6bit, then add 2bit 0 before every 6bit
11010101 11000101 00110011 -> 110101 011100 010100 110011
                           -> 00110101 00011100 00010100 00110011
then every byte convert with the table below:
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| 0 | A | 8 | I |16 | Q |24 | Y |32 | g |40 | o |48 | w |56 | 4 |pad| = |
| 1 | B | 9 | J |17 | R |25 | Z |33 | h |41 | p |49 | x |57 | 5 |   |   |
| 2 | C |10 | K |18 | S |26 | a |34 | i |42 | q |50 | y |58 | 6 |   |   |
| 3 | D |11 | L |19 | T |27 | b |35 | j |43 | r |51 | z |59 | 7 |   |   |
| 4 | E |12 | M |20 | U |28 | c |36 | k |44 | s |52 | 0 |60 | 8 |   |   |
| 5 | F |13 | N |21 | V |29 | d |37 | l |45 | t |53 | 1 |61 | 9 |   |   |
| 6 | G |14 | O |22 | W |30 | e |38 | m |46 | u |54 | 2 |62 | + |   |   |
| 7 | H |15 | P |23 | X |31 | f |39 | n |47 | v |55 | 3 |63 | / |   |   |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

$str = &EasyTool::encode_base64($str)
$str = &EasyTool::decode_base64($str)

url_encode url_decode - encode and decode string in URLs

+------------------------+---------------+
| 0-9 A-Z a-z _.!~*'()-  | Not changed   |
| space                  | +             |
| others                 | %xx(xx is HEX)|
+------------------------+---------------+

$str = &EasyTool::url_encode($str);
$str = &EasyTool::url_decode($str);

html_encode html_decode - encode and decode string in HTML

+---+-------+
| > |  &gt; |
| < |  &lt; |
| " | &quot;|
| & | &amp; |
|   | &nbsp;|
+---+-------+

$str = &EasyTool::html_encode($str);
$str = &EasyTool::html_decode($str);

text_2_html - convert text into HTML

    $text =~ s/\&/\&amp;/g;
    $text =~ s/"/\&quot;/g;
    $text =~ s/ / \&nbsp;/g;
    $text =~ s/</\&lt;/g;
    $text =~ s/>/\&gt;/g;
    $text =~ s/[\a\f\e\0\r]//isg;
    $text =~ s/document.cookie/documents\&\#46\;cookie/isg;
    $text =~ s/'/\&\#039\;/g;
    $text =~ s/\$/\&\#36;/isg;
    $text =~ s|\n\n|<p></p>|g;
    $text =~ s|\n|<br />|g;
    $text =~ s/\t/ \&nbsp; \&nbsp;/g;
	+---------------+--------------------+
	|       &       |        &amp;       |
	|       "       |       &quot;       |
	|     space     |       &nbsp;       |
	|       <       |        &lt;        |
	|       >       |        &gt;        |
	|  \a\f\e\0\r   |                    |
	|document.cookie|documents&#46;cookie|
	|       '       |       &#039;       |
	|       $       |        &#36;       |
	|     \n\n      |      <p></p>       |
	|      \n       |       <br />       |
	|      \t       |   \&nbsp; \&nbsp;  |
	+---------------+--------------------+

	$html = &EasyTool::text_2_html($str);

qquote qquote_bin - put string into double-quotes

$str = &EasyTool::qquote($str);
$str = &EasyTool::qquote_bin($str);

dump - Dump the scalar, array, hash, onto the screen

&EasyTool::dump($data);

test_var - Store the value of $var

$var = &EasyTool::test_var();
&EasyTool::test_var($var);

time_2_str - format output time string

$format_str=EasyTool::time_2_str($time_str[,$template])
time_2_str($time_str) return str such as '2000-01-01 00:00:00'
time_2_str($time_str,'%yyyy-%MM-%dd') return str such as '2000-01-01'

is_time - whether this is a valid time string

$bool=EasyTool::is_time($time_str)

time_2_timestamp - input is $time_str output is unix timestamp

$timestamp=EasyTool::time_2_timestamp($time_str)

hash_2_timestamp - input is hash output is unix timestamp

$timestamp=EasyTool::hash_2_timestamp($rh_time)

time_2_hash

$rh_time=EasyTool::time_2_hash($time_str)

get time of now

$timestamp=EasyTool::now();
$timestamp=EasyTool::time();
$datetime=EasyTool::datetime_now();
$date=EasyTool::date_now();

day_of_month - get day count of specified month

$day_count=day_of_month($year,$month)

day_of_week - get day number in week

$day_count=day_of_week($time_str)

set time funcion

$timestamp=EasyTool::timestamp_set($time_str,$rh_time);
$date=EasyTool::date_set($time_str,$rh_time);
$datetime=EasyTool::datetime_set($time_str,$rh_time)

time operate funcion

$timestamp=EasyTool::timestamp_add($time_str,$rh_offset);
$datetime=EasyTool::datetime_add($time_str,$rh_offset);
$date=EasyTool::date_add($time_str,$rh_offset)

COPYRIGHT

The EasyTool module is Copyright (c) 2003-2005 QIAN YU. All rights reserved.

You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.