Skip to content

Commit

Permalink
Fix Bug in Change year number in first month of year
Browse files Browse the repository at this point in the history
  • Loading branch information
Azare77 committed Dec 20, 2023
1 parent b2a5507 commit 5c3a434
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 77 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@

## [1.2.1]
* Fix Bug in first day on Gregorian
* change example
* change example

## [1.2.2]
* Fix Bug in Change year number in first month of year
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Modified and Completed Version of [jalali_calendar](https://pub.dev/packages/jal

## Import this line in Flutter pubspec.yaml
```dart
jalali_table_calendar: ^1.2.1
jalali_table_calendar: ^1.2.2
```
## To Use Calendar Or DatePicker , Import this line to your dart file
```dart
Expand Down
18 changes: 6 additions & 12 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:jalali_table_calendar/jalali_table_calendar.dart';
import 'package:shamsi_date/shamsi_date.dart';

void main() {
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new MyApp(),
));
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp(),
),
);
}

class MyApp extends StatefulWidget {
Expand Down Expand Up @@ -44,8 +45,6 @@ class _State extends State<MyApp> {
today.add(Duration(days: 2)): [6, 5, 465, 66546],
};
super.initState();
print(
"Parse TO Format ${Gregorian(2019, 02, 20, 00, 19, 54, 000).toJalali()}");
}

String numberFormatter(String number, bool persianNumber) {
Expand Down Expand Up @@ -117,11 +116,6 @@ class _State extends State<MyApp> {
onPressed: _showDatePicker,
child: new Text('نمایش دیت پیکر'),
),
Text(
"\nزمان و تاریخ فعلی سیستم : ${Jalali.now()}",
textAlign: TextAlign.center,
textDirection: TextDirection.rtl,
),
Divider(),
Text(
"تقویم ",
Expand Down
19 changes: 8 additions & 11 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_localizations:
dependency: "direct main"
description: flutter
source: sdk
version: "0.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -71,17 +76,17 @@ packages:
dependency: transitive
description:
name: intl
sha256: a3715e3bc90294e971cb7dc063fbf3cd9ee0ebf8604ffeafabd9e6f16abbdbe6
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
url: "https://pub.dev"
source: hosted
version: "0.18.0"
version: "0.18.1"
jalali_table_calendar:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "1.2.1"
version: "1.2.2"
jiffy:
dependency: transitive
description:
Expand Down Expand Up @@ -122,14 +127,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.3"
shamsi_date:
dependency: "direct main"
description:
name: shamsi_date
sha256: "19020ef0a001db3eabcec8657f8aa10b93b3746641d9eca64db9a41e6506645c"
url: "https://pub.dev"
source: hosted
version: "1.0.2"
sky_engine:
dependency: transitive
description: flutter
Expand Down
3 changes: 2 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ environment:
dependencies:
flutter:
sdk: flutter
shamsi_date: ^1.0.1
flutter_localizations:
sdk: flutter
jalali_table_calendar:
path: ../

Expand Down
1 change: 0 additions & 1 deletion lib/src/TableCalendar/CalendarDayPicker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ class CalendarDayPicker extends StatelessWidget {
final int year = displayedMonth.year;
final int month = displayedMonth.month;
final int mDay = displayedMonth.day;

final PersianDate getPearData =
PersianDate.pDate(gregorian: displayedMonth.toString());
final PersianDate selectedPersianDate =
Expand Down
6 changes: 0 additions & 6 deletions lib/src/TableCalendar/CalendarMonthPicker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,6 @@ class _CalendarMonthPickerState extends State<CalendarMonthPicker>
child: IconButton(
icon: Icon(Icons.chevron_left,
color: Theme.of(context).textTheme.titleSmall?.color),
tooltip: _isDisplayingFirstMonth
? null
: '${localizations.previousMonthTooltip} ${localizations.formatMonthYear(_previousMonthDate)}',
onPressed:
_isDisplayingFirstMonth ? null : _handlePreviousMonth,
),
Expand All @@ -324,9 +321,6 @@ class _CalendarMonthPickerState extends State<CalendarMonthPicker>
child: IconButton(
icon: Icon(Icons.chevron_right,
color: Theme.of(context).textTheme.titleSmall?.color),
tooltip: _isDisplayingLastMonth
? null
: '${localizations.nextMonthTooltip} ${localizations.formatMonthYear(_nextMonthDate)}',
onPressed: _isDisplayingLastMonth ? null : _handleNextMonth,
),
),
Expand Down
6 changes: 0 additions & 6 deletions lib/src/jalaliCalendarPicker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,6 @@ class _MonthPickerState extends State<MonthPicker>
opacity: _chevronOpacityAnimation,
child: IconButton(
icon: const Icon(Icons.chevron_left),
tooltip: _isDisplayingFirstMonth
? null
: '${localizations.previousMonthTooltip} ${localizations.formatMonthYear(_previousMonthDate)}',
onPressed:
_isDisplayingFirstMonth ? null : _handlePreviousMonth,
),
Expand All @@ -783,9 +780,6 @@ class _MonthPickerState extends State<MonthPicker>
opacity: _chevronOpacityAnimation,
child: IconButton(
icon: const Icon(Icons.chevron_right),
tooltip: _isDisplayingLastMonth
? null
: '${localizations.nextMonthTooltip} ${localizations.formatMonthYear(_nextMonthDate)}',
onPressed: _isDisplayingLastMonth ? null : _handleNextMonth,
),
),
Expand Down
151 changes: 116 additions & 35 deletions lib/src/persian_date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,44 +177,127 @@ class PersianDate {
];

gregorianToJalali(int y, int m, int d, [String? separator]) {
var sumMonthDay = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
var jY = 0;
if (y > 1600) {
jY = 979;
y -= 1600;
int differenceBetweenDays = 0;

int date = 0;
int year = 0;

int gregorianYear = y;
int gregorianMonth = m;
int gregorianDay = d;
List notLeapYear = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
List isLeapYear = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335];
num month;
if ((gregorianYear % 4) != 0) {
date = notLeapYear[gregorianMonth - 1] + gregorianDay;
if (date > 79) {
date = date - 79;
if (date <= 186) {
switch (date % 31) {
case 0:
month = date / 31;
date = 31;
break;
default:
month = (date / 31) + 1;
date = (date % 31);
break;
}
year = gregorianYear - 621;
} else {
date = date - 186;

switch (date % 30) {
case 0:
month = (date / 30) + 6;
date = 30;
break;
default:
month = (date / 30) + 7;
date = (date % 30);
break;
}
year = gregorianYear - 621;
}
} else {
if ((gregorianYear > 1996) && (gregorianYear % 4) == 1) {
differenceBetweenDays = 11;
} else {
differenceBetweenDays = 10;
}
date = date + differenceBetweenDays;

switch (date % 30) {
case 0:
month = (date / 30) + 9;
date = 30;
break;
default:
month = (date / 30) + 10;
date = (date % 30);
break;
}
year = gregorianYear - 622;
}
} else {
jY = 0;
y -= 621;
}
var gy = (m > 2) ? y + 1 : y;
var day = (365 * y) +
((gy + 3) ~/ 4) -
((gy + 99) ~/ 100) +
((gy + 399) ~/ 400) -
80 +
d +
sumMonthDay[m - 1];
jY += 33 * (day.round() / 12053).floor();
day %= 12053;
jY += 4 * (day.round() / 1461).floor();
day %= 1461;
jY += ((day.round() - 1) / 365).floor();
if (day > 365) day = ((day - 1).round() % 365);
int jm;
var jd;
int days = day.toInt();
if (days < 186) {
jm = 1 + (days ~/ 31);
jd = 1 + (days % 31);
} else {
jm = 7 + ((days - 186) ~/ 30);
jd = 1 + (days - 186) % 30;
date = isLeapYear[gregorianMonth - 1] + gregorianDay;

if (gregorianYear >= 1996) {
differenceBetweenDays = 79;
} else {
differenceBetweenDays = 80;
}
if (date > differenceBetweenDays) {
date = date - differenceBetweenDays;

if (date <= 186) {
switch (date % 31) {
case 0:
month = (date / 31);
date = 31;
break;
default:
month = (date / 31) + 1;
date = (date % 31);
break;
}
year = gregorianYear - 621;
} else {
date = date - 186;

switch (date % 30) {
case 0:
month = (date / 30) + 6;
date = 30;
break;
default:
month = (date / 30) + 7;
date = (date % 30);
break;
}
year = gregorianYear - 621;
}
} else {
date = date + 10;

switch (date % 30) {
case 0:
month = (date / 30) + 9;
date = 30;
break;
default:
month = (date / 30) + 10;
date = (date % 30);
break;
}
year = gregorianYear - 622;
}
}
var persianDate;
if (separator == null)
persianDate = [jY, jm, jd];
persianDate = [year, month.floor(), date];
else
persianDate = "$jY$separator$jm$separator$jd";
persianDate = "$year$separator${month.floor()}$separator$date";
return persianDate;
}

Expand Down Expand Up @@ -423,8 +506,6 @@ class PersianDate {

String newFormat = format;

print(parse.weekday);

if (newFormat.indexOf(yyyy) != -1)
newFormat = newFormat.replaceFirst(yyyy, _digits(jParse[0], 4));
if (newFormat.indexOf(yy) != -1)
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ packages:
dependency: transitive
description:
name: intl
sha256: a3715e3bc90294e971cb7dc063fbf3cd9ee0ebf8604ffeafabd9e6f16abbdbe6
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
url: "https://pub.dev"
source: hosted
version: "0.18.0"
version: "0.18.1"
jiffy:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: jalali_table_calendar
description: Table Calendar , Calendar DatePacker , Date picker and Date Convertor For Persian Date(Jalali/Shamsi date) with holidays.
version: 1.2.1
version: 1.2.2
homepage: https://github.com/Azare77/jalali_table_calendar

environment:
Expand Down

0 comments on commit 5c3a434

Please sign in to comment.