Persian
NAME
Date::Calendar::Persian - Conversions from / to the Persian calendar
SYNOPSIS
Converting a Gregorian date (e.g. 16th February 2021) into Persian
use Date::Calendar::Persian;
my Date $dt-greg;
my Date::Calendar::Persian $dt-persian;
$dt-greg .= new(2021, 2, 16);
$dt-persian .= new-from-date($dt-greg);
say $dt-persian;
# --> 1399-11-28
say $dt-persian.strftime("%A %d %B %Y");
# --> Se shanbe 28 Bahman 1399
Converting a Persian date (e.g. 1 Farvardin 1400) into Gregorian
use Date::Calendar::Persian;
my Date::Calendar::Persian $dt-persian;
my Date $dt-greg;
$dt-persian .= new(year => 1400, month => 1, day => 1);
$dt-greg = $dt-persian.to-date;
say $dt-greg;
# --> 2021-03-21
Conversion with a calendar which defines days as sunset to sunset
use Date::Calendar::Strftime;
use Date::Calendar::Hebrew;
use Date::Calendar::Persian;
my Date::Calendar::Persian $d-pe;
my Date::Calendar::Hebrew $d-he;
$d-pe .= new(year => 1403, month => 8, day => 23, daypart => before-sunrise());
$d-he .= new-from-date($d-pe);
say $d-he.strftime("%A %d %B %Y");
# ---> "Yom Reviʻi 12 Heshvan 5785"
$d-pe .= new(year => 1403, month => 8, day => 23, daypart => daylight());
$d-he .= new-from-date($d-pe);
say $d-he.strftime("%A %d %B %Y");
# ---> "Yom Reviʻi 12 Heshvan 5785" again
$d-pe .= new(year => 1403, month => 8, day => 23, daypart => after-sunset());
$d-he .= new-from-date($d-pe);
say $d-he.strftime("%A %d %B %Y");
# ---> "Yom Chamishi 13 Heshvan 5785" instead of "Yom Reviʻi 12 Heshvan 5785"
DESCRIPTION
Date::Calendar::Persian is a class representing dates in the Persian calendar. It allows you to convert a Persian date into Gregorian or into other implemented calendars, and it allows you to convert dates from Gregorian or from other calendars into Persian.
The Date::Calendar::Persian class gives the arithmetic version of the Persian calendar. The astronomic Persian calendar is partially implemented in Date::Calendar::Persian::Astronomical, included in this distribution.
METHODS
Constructors
new
Create an Persian date by giving the year, month and day numbers and optionally the day part.
new-from-date
Build an Persian date by cloning an object from another class. This
other class can be the core class Date or any
Date::Calendar::xxx class with a daycount method and
hopefully a daypart method.
new-from-daycount
Build an Persian date from the Modified Julian Day number and from the
daypart parameter (optional, defaults do daylight).
Accessors
gist
Gives a short string representing the date, in YYYY-MM-DD format.
year, month, day
The numbers defining the date.
daypart
A number indicating which part of the day. This number should be filled and compared with the following subroutines, with self-documenting names:
before-sunrise()
daylight()
after-sunset()
month-name
The month of the date, as a string.
month-abbr
The month of the date, as a 3-char string.
day-name
The name of the day within the week.
day-abbr
The weekday of the date, as a 3-char string.
daycount
The Modified Julian Day Number (a day-only scheme based on 17 November 1858).
day-of-week
The number of the day within the week (1 for Saturday / Shanbe, 7 for Friday / Jumee).
week-number
The number of the week within the year, 1 to 52 or 1 to 53. Similar to the "ISO date" as defined for Gregorian date. Week number 1 is the Sat→Fri span that contains the first Tuesday / Se shanbe of the year, week number 2 is the Sat→Fri span that contains the second Tuesday / Se shanbe of the year and so on.
week-year
Mostly similar to the year attribute. Yet, the last days of the
year and the first days of the following year can be sort-of
transferred to the other year. The week-year attribute reflects
this transfer. While the real year always begins on 1st Farvardin and
ends on the 29th or 30th Esfand, the week-year always begins on
Saturday / Shanbe and it always ends on Friday / Jumee.
day-of-year
How many days since the beginning of the year. 1 to 365 on normal years, 1 to 366 on leap years.
Other Methods
to-date
Clones the date into a core class Date object or some
Date::Calendar::xxx compatible calendar class. The target class
name is given as a positional parameter. This parameter is optional,
the default value is "Date" for the Gregorian calendar.
To convert a date from a calendar to another, you have two conversion styles, a "push" conversion and a "pull" conversion. For example, while converting "11 Bahman 1440" to the French Revolutionary calendar, you can code:
use Date::Calendar::Persian;
use Date::Calendar::FrenchRevolutionary;
my Date::Calendar::Persian $d-orig;
my Date::Calendar::FrenchRevolutionary $d-dest-push;
my Date::Calendar::FrenchRevolutionary $d-dest-pull;
$d-orig .= new(year => 1440
, month => 11
, day => 11);
$d-dest-push = $d-orig.to-date("Date::Calendar::FrenchRevolutionary");
$d-dest-pull .= new-from-date($d-orig);
When converting from the core class Date, use the pull style.
When converting to the core class Date, use the push style. When
converting from any class other than the core class Date to any
other class other than the core class Date, use the style you
prefer. For the Gregorian calendar, instead of the core class Date,
you can use the child class Date::Calendar::Gregorian which allows
both push and pull styles.
strftime
This method is very similar to the homonymous functions you can find
in several languages (C, shell, etc). It also takes some ideas from
printf-similar functions. For example
$df.strftime("%04d blah blah blah %-25B")
will give the day number padded on the left with 2 or 3 zeroes to
produce a 4-digit substring, plus the substring " blah blah blah ",
plus the month name, padded on the right with enough spaces to produce
a 25-char substring. Thus, the whole string will be at least 42 chars
long. By the way, you can drop the "at least" mention, because the
longest month name is 11-char long, so the padding will always occur
and will always include at least 14 spaces.
A strftime specifier consists of:
A percent sign,
An optional minus sign, to indicate on which side the padding occurs. If the minus sign is present, the value is aligned to the left and the padding spaces are added to the right. If it is not there, the value is aligned to the right and the padding chars (spaces or zeroes) are added to the left.
An optional zero digit, to choose the padding char for right-aligned values. If the zero char is present, padding is done with zeroes. Else, it is done wih spaces.
An optional length, which specifies the minimum length of the result substring.
An optional
"E"or"O"modifier. On some older UNIX system, these were used to give the extended or localized version of the date attribute. Here, they rather give alternate variants of the date attribute. Not used with the Persian calendar.A mandatory type code.
The allowed type codes are:
- %a
The abbreviated day of week.
- %A
The full day of week name.
- %b
The abbreviated month name.
- %B
The full month name.
- %d
The day of the month as a decimal number (range 01 to 31).
- %e
Like %d, the day of the month as a decimal number, but a leading
zero is replaced by a space.
- %f
The month as a decimal number (1 to 12). Unlike %m, a leading zero
is replaced by a space.
- %F
Equivalent to %Y-%m-%d (the ISO 8601 date format)
- %G
The "week year" as a decimal number. Mostly similar to %Y, but it
may differ on the very first days of the year or on the very last
days. Analogous to the year number in the so-called "ISO date" format
for Gregorian dates.
- %j
The day of the year as a decimal number (range 001 to 366).
- %m
The month as a two-digit decimal number (range 01 to 12), including a leading zero if necessary.
- %n
A newline character.
- %Ep
Gives a 1-char string representing the day part:
☾orU+263Ebefore sunrise,☼orU+263Cduring daylight,☽orU+263Dafter sunset.
Rationale: in C or in other programming languages, when strftime
deals with a date-time object, the day is split into two parts, before
noon and after noon. The %p specifier reflects this by giving a
"AM" or "PM" string.
The 3-part splitting in the Date::Calendar::xxx may be
considered as an alternate splitting of a day. To reflect this in
strftime, we use an alternate version of %p, therefore %Ep.
- %t
A tab character.
- %u
The day of week as a 1..7 number.
- %V
The week number as defined above, similar to the week number in the so-called "ISO date" format for Gregorian dates.
- %Y
The year as a decimal number.
- %%
A literal `%' character.
BUGS AND ISSUES
The astronomical Persian calendar is only partially implemented. It can represent dates only in the 1000 to 1800 years (1621 to 2421 in the Gregorian calendar).
Security issues
Another issue, as explained in the Date::Calendar::Strftime
documentation. Please ensure that format-string passed to strftime
comes from a trusted source. Failing that, you could get a format that
includes a outrageous length in a strftime specifier, and you would
drain your PC's RAM very fast.
Relations with :ver<0.0.x> classes and with core class Date
Version 0.1.0 (and API 1) was introduced to ease the conversions with
other calendars in which the day is defined as sunset-to-sunset.
If all Date::Calendar::xxx classes use version 0.1.x and API 1,
the conversions will be correct. But if some Date::Calendar::xxx
classes use version 0.0.x and API 0, there might be problems.
A date from a 0.0.x class has no daypart attribute. But when "seen"
from a 0.1.x class, the 0.0.x date seems to have a daypart
attribute equal to daylight. When converted from a 0.1.x class to a
0.0.x class, the date may just shift from after-sunset (or
before-sunrise) to daylight, or it may shift to the daylight
part of the prior (or next) date. This means that a roundtrip with
cascade conversions may give the starting date, or it may give the
date prior or after the starting date.
If you install <Date::Calendar::Persian:ver<0.1.0>>, why would you
refrain from upgrading other Date::Calendar::xxxx classes? So
actually, this issue applies mainly to the core class Date, because
you may prefer avoiding the installation of
Date::Calendar::Gregorian.
Time
This module and the Date::Calendar::xxx associated modules are
still date modules, they are not date-time modules. The user has to
give the daypart attribute as a value among before-sunrise,
daylight or after-sunset. There is no provision to give a HHMMSS
time and convert it to a daypart parameter.
SEE ALSO
Raku Software
Date::Calendar::Strftime or https://github.com/jforget/raku-Date-Calendar-Strftime
Date::Calendar::Gregorian or https://github.com/jforget/raku-Date-Calendar-Gregorian
Date::Calendar::Julian or https://github.com/jforget/raku-Date-Calendar-Julian
Date::Calendar::Hebrew or https://github.com/jforget/raku-Date-Calendar-Hebrew
Date::Calendar::Hijri or https://github.com/jforget/raku-Date-Calendar-Hijri
Date::Calendar::CopticEthiopic or https://github.com/jforget/raku-Date-Calendar-CopticEthiopic
Date::Calendar::MayaAztec or https://github.com/jforget/raku-Date-Calendar-MayaAztec
Date::Calendar::FrenchRevolutionary or https://github.com/jforget/raku-Date-Calendar-FrenchRevolutionary
Date::Calendar::Bahai or https://github.com/jforget/raku-Date-Calendar-Bahai
Perl 5 Software
Other Software
date(1), strftime(3)
calendar/cal-persia.el in emacs or xemacs.
CALENDRICA 4.0 -- Common Lisp, which can be download in the "Resources" section of https://www.cambridge.org/us/academic/subjects/computer-science/computing-general-interest/calendrical-calculations-ultimate-edition-4th-edition?format=PB&isbn=9781107683167
https://api.kde.org/frameworks-api/frameworks-apidocs/frameworks/kdelibs4support/html/kcalendarsystemjalali_8cpp_source.html Since the KDE version number will change, you should rather use a search engine: https://html.duckduckgo.com/html/?q=calendarsystemjalali%20KDElibs
Book
Calendrical Calculations (Third or Fourth Edition) by Nachum Dershowitz and Edward M. Reingold, Cambridge University Press, see http://www.calendarists.com or https://www.cambridge.org/us/academic/subjects/computer-science/computing-general-interest/calendrical-calculations-ultimate-edition-4th-edition?format=PB&isbn=9781107683167.
Internet
https://www.funaba.org/cc (which seems no longer active)
https://en.wikipedia.org/wiki/Iranian_calendars
AUTHOR
Jean Forget <J2N-FORGET at orange dot fr>
COPYRIGHT AND LICENSE
Copyright (c) 2021, 2024 Jean Forget, all rights reserved.
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.