Skip to content

Commit

Permalink
changes on unix time
Browse files Browse the repository at this point in the history
time zone added
bug : current jalali date goes to (2 dey 2018)
  • Loading branch information
ali77gh committed Oct 2, 2018
1 parent 9fde09a commit 40fc62b
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 290 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.Jalali);
a.ShowDatePicker(getSupportFragmentManager(), Calendar.Hijri);
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 @@ -487,9 +487,9 @@ public static DateModel HijriToGregorian(int year, int month, int day) {
return DaysToGregorian(HijriToDays(year, month, day) + 227014);
}

//unix time

public static DateModel UnixToHijri(int sec, int timeZone) {
sec += timeZone;
public static DateModel UnixToHijri(int sec) {
DateModel date = DaysToHijri((sec / 86400) + 719163 - 227014);
date.sec = sec % 86400;
date.hour = date.sec / 3600;
Expand All @@ -499,12 +499,7 @@ public static DateModel UnixToHijri(int sec, int timeZone) {
return date;
}

public static DateModel UnixToHijri(int sec) {
return UnixToHijri(sec, 0);
}

public static DateModel UnixToJalali(int sec, int timeZone) {
sec += timeZone;
public static DateModel UnixToJalali(int sec) {
DateModel date = DaysToJalali((sec / 86400) + 719163 - 226895);
date.sec = sec % 86400;
date.hour = date.sec / 3600;
Expand All @@ -514,12 +509,7 @@ public static DateModel UnixToJalali(int sec, int timeZone) {
return date;
}

public static DateModel UnixToJalali(int sec) {
return UnixToJalali(sec, 0);
}

public static DateModel UnixToGregorian(int sec, int timeZone) {
sec += timeZone;
public static DateModel UnixToGregorian(int sec) {
DateModel date = DaysToGregorian((sec / 86400) + 719163);
date.sec = sec % 86400;
date.hour = date.sec / 3600;
Expand All @@ -529,9 +519,48 @@ public static DateModel UnixToGregorian(int sec, int timeZone) {
return date;
}

public static DateModel UnixToGregorian(int sec) {
return UnixToGregorian(sec, 0);

public static int HijriToUnix(int year, int month, int day, int hour, int min, int sec) {

int from = 719163; // const : DateConverter.GregorianToDays(1970, 1, 1)

from -= 227014; // hijri and gregorian deference in days

int days = HijriToDays(year, month, day);

int lastDays = ((days - from) * 86400);

int today = sec + (min * 60) + (hour * 3600);

return today + lastDays;

}

public static int JalaliToUnix(int year, int month, int day, int hour, int min, int sec) {

int from = 719163; // const : DateConverter.GregorianToDays(1970, 1, 1)

from -= 226895; // jalali and gregorian deference in days

int days = JalaliToDays(year, month, day);

int lastDays = ((days - from) * 86400);

int today = sec + (min * 60) + (hour * 3600);

return today + lastDays;
}

//todo to unix methods
public static int GregorianToUnix(int year, int month, int day, int hour, int min, int sec) {

int from = 719163; // const -> DateConverter.GregorianToDays(1970, 1, 1)

int days = GregorianToDays(year, month, day);

int lastDays = ((days - from) * 86400);

int today = sec + (min * 60) + (hour * 3600);

return today + lastDays;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public DateSystem(DateModel date, Calendar calendar) {
if (date.month == 0) throw new IllegalArgumentException("month cant be 0");
switch (Calendar) {
case Jalali: {
Date_SD = new JalaliDateTime(date);
Date_SD = new JalaliDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());
break;
}
case Gregorian: {
Date_SD = new GregorianDateTime(date);
Date_SD = new GregorianDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());
break;
}
case Hijri: {
Date_SD = new HijriDateTime(date);
Date_SD = new HijriDateTime(date.year, date.month, date.day, date.hour, date.min, date.sec, TimeZoneHelper.getSystemTimeZone());
break;
}
default: {
Expand Down Expand Up @@ -274,8 +274,7 @@ public int hashCode() {
return (Calendar.getValue() * 397) ^ (Date_SD.hashCode());
}

public long getUnixTime() {
long from = DateConverter.GregorianToDays(1970, 1, 1);
return (getGregorianDateTime().getDays() - from) * 86400;
public int toUnixTime() {
return Date_SD.toUnixTime();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.ali.uneversaldatetools.date;

/**
* Created by ali on 10/2/18.
*/

public class DateValidation {


public static void GregorianValidate(int year, int month, int day, int hour, int min, int sec) {
BaseValidate(year,month,hour, min, sec);
GregorianDayValidate(year, month, day);
}

public static void JalaliValidate(int year, int month, int day, int hour, int min, int sec) {
BaseValidate(year,month,hour, min, sec);
JalaliDayValidate(year, month, day);
}

public static void HijriValidate(int year, int month, int day, int hour, int min, int sec) {
BaseValidate(year,month,hour, min, sec);
HijriDayValidate(year, month, day);
}


private static void GregorianDayValidate(int year, int month, int day) {

//day
if (DateConverter.IsGregorianLeap(year) & month == 2) {
if (day < 1 | day > GregorianDateTime.DaysInMonth[month] + 1)
NotValid("day",day);
} else {
if (day < 1 | day > GregorianDateTime.DaysInMonth[month])
NotValid("day",day);
}

}

private static void JalaliDayValidate(int year, int month, int day) {

//day
if (DateConverter.IsGregorianLeap(year) & month == 12) {
if (day < 1 | day > JalaliDateTime.DaysInMonth[month] + 1)
NotValid("day",day);
} else {
if (day < 1 | day > JalaliDateTime.DaysInMonth[month])
NotValid("day",day);
}

}

private static void HijriDayValidate(int year, int month, int day) {
//day
if (DateConverter.IsGregorianLeap(year) & month == 12) {
if (day < 1 | day > HijriDateTime.DaysInMonth[month] + 1)
NotValid("day",day);
} else {
if (day < 1 | day > HijriDateTime.DaysInMonth[month])
NotValid("day",day);
}
}


private static void BaseValidate(int year, int month, int hour, int min, int sec) {

//year
if (year < 0) NotValid("year", year);

//month
if (month < 1 | month > 12) NotValid("month", month);

//time
if (hour > 23 | hour < 0) NotValid("hour", hour);
if (min > 59 | min < 0) NotValid("minute", min);
if (sec > 59 | sec < 0) NotValid("second", sec);
}


private static void NotValid(String what, int value) {
throw new IllegalArgumentException("invalid " + what + " : " + value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

public class DefaultCalendar {

private Calendar defaultCalendar;
private static Calendar mDefaultCalendar;

public Calendar getDefaultCalendar() {
return defaultCalendar;
public static Calendar getDefaultCalendar() {
return mDefaultCalendar;
}

public void setDefaultCalendar(Calendar defaultCalendar) {
this.defaultCalendar = defaultCalendar;
public static void setDefaultCalendar(Calendar DefaultCalendar) {
mDefaultCalendar = DefaultCalendar;
}
}
Loading

0 comments on commit 40fc62b

Please sign in to comment.