Skip to content

Commit

Permalink
Fix memory issue because of impacting with TZ
Browse files Browse the repository at this point in the history
  • Loading branch information
loint committed Apr 16, 2019
1 parent ccf09e1 commit 6f4ae12
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
3 changes: 1 addition & 2 deletions library/Java/Text/SimpleDateFormat/SimpleDateFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ void SimpleDateFormat::setTimeZone(const TimeZone &timeZone) {
String SimpleDateFormat::format(const Date &date) {
String tz = String("TZ=") + this->timeZone.getID();
putenv(tz.toCharPointer());

std::time_t current_time;
std::time(&current_time);
struct std::tm *timeinfo = std::localtime(&current_time);
long offset = timeinfo->tm_gmtoff;

unsetenv("TZ");
size_t bufferLength = 80;
string buffer = (string) calloc(bufferLength, sizeof(char));
time_t timestamp = (time_t) ((date.getTime() / 1000) + offset);
Expand Down
6 changes: 2 additions & 4 deletions library/Java/Util/Date/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,16 +428,14 @@ long Date::parse(String inputString) {
long Date::getOffsetFromUTC() {
long currentTime;
struct tm * timeInfo;

time( &currentTime );
time(&currentTime);
timeInfo = gmtime (&currentTime);
time_t utc = mktime(timeInfo);
timeInfo = localtime(&currentTime);
time_t local = mktime(timeInfo);

// Get offset in hours from UTC
double offsetFromUTC = difftime(utc, local); // HOUR_IN_SECOND;

return (long) offsetFromUTC;
}

Expand Down Expand Up @@ -527,6 +525,7 @@ void Date::initializeDate(int year, int month, int date,

this->timer = mktime(&localTimer);
this->localTimer = localtime(&this->timer);
this->updateDateStatus();
}

void Date::initializeDate(long timestamp) {
Expand All @@ -549,7 +548,6 @@ String Date::timeToString(String pattern, tm *timeManagement) const {
long Date::getUTCTime(long timer) {
tm tempTimer = {0};
tm *utcTimer = gmtime_r(&timer, &tempTimer);

return mktime(utcTimer);
}

Expand Down

0 comments on commit 6f4ae12

Please sign in to comment.