Skip to content

Commit

Permalink
1.bug fix : current jalali date goes to (2 dey 2018)
Browse files Browse the repository at this point in the history
2.add timezone and unixtime to SystemDate and remove date model
3.get system date changed to get unixtime every where
  • Loading branch information
ali77gh committed Oct 4, 2018
1 parent 40fc62b commit 3a8d9a0
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
Button getTime = findViewById(R.id.btn_get_time);
TextView timeView = findViewById(R.id.text_time_show);

a.ShowDatePicker(getSupportFragmentManager(), Calendar.Hijri);
a.ShowDatePicker(getSupportFragmentManager(), Calendar.Jalali);
a.setOnDateSelected((dateSystem, unixTime) -> {
timeView.setText("unix time is: " + unixTime);
timeView.append("\ndate is: " + dateSystem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,96 @@
import android.util.Log;

import com.ali.uneversaldatetools.model.DateModel;
import com.ali.uneversaldatetools.tools.DateTools;
import com.ali.uneversaldatetools.tools.UnixTimeTools;

import java.util.Date;
import java.util.Objects;
import java.util.TimeZone;

/**
* Created by ali on 9/5/18.
*/

//this is struct
public class DateSystem implements IDate {

private Calendar Calendar;
private DateModel Date;
private IDate Date_SD;

public DateSystem(DateModel date, Calendar calendar) {
public DateSystem(int year, int month, int day, Calendar calendar) {
Calendar = calendar;

if (date.month == 0) throw new IllegalArgumentException("month cant be 0");
Log.d("month: ", String.valueOf(month));
if (month > 12) month -= 12;

switch (Calendar) {
case Jalali: {
Date_SD = new JalaliDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());

case Jalali:
Date_SD = new JalaliDateTime(year, month, day);
break;
}
case Gregorian: {
Date_SD = new GregorianDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());

case Gregorian:
Date_SD = new GregorianDateTime(year, month, day);
break;
}
case Hijri: {
Date_SD = new HijriDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());

case Hijri:
Date_SD = new HijriDateTime(year, month, day);
break;
}
default: {

default:
throw new RuntimeException("Invalid Calendar Type!");
}
}

Date = date;
}
Date = Date_SD.getDate();
}

public DateSystem(int year, int month, int day, Calendar calendar) {
public DateSystem(int year, int month, int day, int hour, int min, int sec, TimeZone timeZone, Calendar calendar) {
Calendar = calendar;

Log.d("month: ", String.valueOf(month));
if (month > 12) month -= 12;
else if (month < 1) month += 12;

switch (Calendar) {
case Jalali: {
Date_SD = new JalaliDateTime(year, month, day);

case Jalali:
Date_SD = new JalaliDateTime(year, month, day, hour, min, sec, timeZone);
break;
}
case Gregorian: {
Date_SD = new GregorianDateTime(year, month, day);

case Gregorian:
Date_SD = new GregorianDateTime(year, month, day, hour, min, sec, timeZone);
break;
}
case Hijri: {
Date_SD = new HijriDateTime(year, month, day);

case Hijri:
Date_SD = new HijriDateTime(year, month, day, hour, min, sec, timeZone);
break;
}
default: {

default:
throw new RuntimeException("Invalid Calendar Type!");
}

}
Date = Date_SD.getDate();
}

public DateSystem(int unixTime, TimeZone timeZone, Calendar calendar) {
Calendar = calendar;

switch (Calendar) {

case Jalali:
Date_SD = new JalaliDateTime(unixTime, timeZone);
break;

case Gregorian:
Date_SD = new GregorianDateTime(unixTime, timeZone);
break;

case Hijri:
Date_SD = new HijriDateTime(unixTime, timeZone);
break;

default:
throw new RuntimeException("Invalid Calendar Type!");

}
Date = Date_SD.getDate();
}
Expand Down Expand Up @@ -189,20 +215,23 @@ public DateModel AddDays(int days) {
return Date_SD.AddDays(days);
}

public static DateSystem Now(Calendar calendar) {
return new DateSystem(DateTools.getCurrentDate(), calendar);
public static DateSystem Now(TimeZone timeZone, Calendar calendar) {
return new DateSystem(UnixTimeTools.getCurrentUnixTime(), timeZone, calendar);
}

public static DateSystem Parse(String date, Calendar calendar) {
switch (calendar) {
case Jalali: {
return new DateSystem(JalaliDateTime.Parse(date).getDate(), calendar);
DateModel d = JalaliDateTime.Parse(date).getDate();
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
}
case Gregorian: {
return new DateSystem(GregorianDateTime.Parse(date).getDate(), calendar);
DateModel d = GregorianDateTime.Parse(date).getDate();
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
}
case Hijri: {
return new DateSystem(HijriDateTime.Parse(date).getDate(), calendar);
DateModel d = HijriDateTime.Parse(date).getDate();
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
}
default: {
throw new RuntimeException("Invalid Calendar Type!");
Expand All @@ -213,13 +242,16 @@ public static DateSystem Parse(String date, Calendar calendar) {
public static DateSystem Parse(String yearMonth, int day, Calendar calendar) {
switch (calendar) {
case Jalali: {
return new DateSystem(JalaliDateTime.Parse(yearMonth, day).getDate(), calendar);
DateModel d = JalaliDateTime.Parse(yearMonth, day).getDate();
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
}
case Gregorian: {
return new DateSystem(GregorianDateTime.Parse(yearMonth, day).getDate(), calendar);
DateModel d = GregorianDateTime.Parse(yearMonth, day).getDate();
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
}
case Hijri: {
return new DateSystem(HijriDateTime.Parse(yearMonth, day).getDate(), calendar);
DateModel d = HijriDateTime.Parse(yearMonth, day).getDate();
return new DateSystem(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone(), calendar);
}
default: {
throw new RuntimeException("Invalid Calendar Type!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.support.annotation.NonNull;

import com.ali.uneversaldatetools.model.DateModel;
import com.ali.uneversaldatetools.tools.DateTools;
import com.ali.uneversaldatetools.tools.UnixTimeTools;

import java.util.TimeZone;

Expand All @@ -21,7 +21,7 @@ public class GregorianDateTime implements IDate, Comparable<GregorianDateTime> {
private int Sec;
private TimeZone TimeZone;

public static final int[] DaysInMonth = {
static final int[] DaysInMonth = {
0,
31,
29,
Expand Down Expand Up @@ -105,8 +105,7 @@ public static GregorianDateTime ParseYearMonth(String s) {
}

public static GregorianDateTime Now() {
DateModel crnt = DateTools.getCurrentDate();
return new GregorianDateTime(crnt.year, crnt.month, crnt.day, crnt.hour, crnt.min, crnt.sec, TimeZoneHelper.getSystemTimeZone());
return new GregorianDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.support.annotation.NonNull;

import com.ali.uneversaldatetools.model.DateModel;
import com.ali.uneversaldatetools.tools.DateTools;
import com.ali.uneversaldatetools.tools.UnixTimeTools;

import java.util.TimeZone;

Expand All @@ -21,7 +21,7 @@ public class HijriDateTime implements IDate, Comparable<HijriDateTime> {
private int Sec;
private TimeZone TimeZone;

public static final int[] DaysInMonth = {
static final int[] DaysInMonth = {
0,
30,
29,
Expand Down Expand Up @@ -105,8 +105,7 @@ public static HijriDateTime ParseYearMonth(String yearMonth) {
}

public static HijriDateTime Now() {
DateModel d = DateTools.getCurrentDate();
return new HijriDateTime(d.year, d.month, d.day, d.hour, d.min, d.sec, TimeZoneHelper.getSystemTimeZone());
return new HijriDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone());
}

public DateModel getDate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.support.annotation.NonNull;

import com.ali.uneversaldatetools.model.DateModel;
import com.ali.uneversaldatetools.tools.DateTools;
import com.ali.uneversaldatetools.tools.UnixTimeTools;

import java.util.TimeZone;

Expand All @@ -21,7 +21,7 @@ public class JalaliDateTime implements IDate, Comparable<JalaliDateTime> {
private int Sec;
private TimeZone TimeZone;

public static final int[] DaysInMonth = {
static final int[] DaysInMonth = {
0,
31,
31,
Expand Down Expand Up @@ -110,9 +110,7 @@ public static JalaliDateTime ParseYearMonth(String s) {
}

public static JalaliDateTime Now() {
DateModel crnt = DateTools.getCurrentDate();
DateConverter.GregorianToJalali(crnt.year, crnt.month, crnt.day);
return new JalaliDateTime(crnt.year, crnt.month, crnt.day, crnt.hour, crnt.min, crnt.sec, TimeZoneHelper.getSystemTimeZone());
return new JalaliDateTime(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone());
}

public DateModel getDate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import com.ali.uneversaldatetools.R;
import com.ali.uneversaldatetools.date.Calendar;
import com.ali.uneversaldatetools.date.DateSystem;
import com.ali.uneversaldatetools.date.TimeZoneHelper;
import com.ali.uneversaldatetools.model.Month;
import com.ali.uneversaldatetools.tools.Convert;
import com.ali.uneversaldatetools.tools.DateTools;
import com.ali.uneversaldatetools.tools.ExpandAndCollapseAnimation;
import com.ali.uneversaldatetools.tools.StringGenerator;
import com.ali.uneversaldatetools.tools.UnixTimeTools;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -88,7 +89,7 @@ public void ShowDatePicker(FragmentManager appCompatActivity, @NonNull DateSyste
}

public void ShowDatePicker(FragmentManager appCompatActivity, Calendar calendar) {
DateSystem defaultDate = new DateSystem(DateTools.getCurrentDate(), calendar); // now
DateSystem defaultDate = new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), calendar); // now
ShowDatePicker(appCompatActivity, defaultDate);
}

Expand Down Expand Up @@ -378,12 +379,12 @@ public int selectedDayProvider() {

@Override
public int currentMonthProvider() {
return new DateSystem(DateTools.getCurrentDate(), mDateSystem.getCalendar()).getMonth();
return new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), mDateSystem.getCalendar()).getMonth();
}

@Override
public int currentDayProvider() {
return new DateSystem(DateTools.getCurrentDate(), mDateSystem.getCalendar()).getDay();
return new DateSystem(UnixTimeTools.getCurrentUnixTime(), TimeZoneHelper.getSystemTimeZone(), mDateSystem.getCalendar()).getDay();
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ali.uneversaldatetools.tools;

/**
* Created by ali on 9/11/18.
*/

public class UnixTimeTools {

public static int getCurrentUnixTime() {
return (int) (System.currentTimeMillis() / 1000L); //toSec
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.ali.uneversaldatetools.date.JalaliDateTime;
import com.ali.uneversaldatetools.date.TimeZoneHelper;
import com.ali.uneversaldatetools.model.DateModel;
import com.ali.uneversaldatetools.tools.DateTools;
import com.ali.uneversaldatetools.tools.UnixTimeTools;

import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -37,7 +37,7 @@ public void ToStringTest() {

@Test
public void UnixTest() {
int unixTime = DateTools.getCurrentUnixTime();
int unixTime = UnixTimeTools.getCurrentUnixTime();

Log(unixTime);
DateModel dateModel = DateConverter.UnixToJalali(unixTime);
Expand Down

0 comments on commit 3a8d9a0

Please sign in to comment.