Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reproducible builds: honour SOURCE_DATE_EPOCH #273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 66 additions & 47 deletions PDFWriter/PDFDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,72 +182,91 @@ void PDFDate::SetToCurrentTime()
{
time_t currentTime;
tm structuredLocalTime;
long timeZoneSecondsDifference;
char *env_SOURCE_DATE_EPOCH = std::getenv("SOURCE_DATE_EPOCH");

time(&currentTime);
SAFE_LOCAL_TIME(structuredLocalTime,currentTime);
if (env_SOURCE_DATE_EPOCH)
{
std::istringstream iss(env_SOURCE_DATE_EPOCH);
iss >> currentTime;
if (iss.fail() || !iss.eof())
{
TRACE_LOG("PDFDate::SetToCurrentTime, Could not parse SOURCE_DATE_EPOCH as integer");
Year = -1;
return;
}

Year = structuredLocalTime.tm_year + 1900;
Month = structuredLocalTime.tm_mon + 1;
Day = structuredLocalTime.tm_mday;
Hour = structuredLocalTime.tm_hour;
Minute = structuredLocalTime.tm_min;
Second = structuredLocalTime.tm_sec;
UTC = eSame;
}
else
{
time(&currentTime);

// if unsuccesful or method unknown don't provide UTC info (currently only knows for WIN32 and OSX
// if unsuccesful or method unknown don't provide UTC info (currently only knows for WIN32 and OSX
long timeZoneSecondsDifference;
#if defined (__MWERKS__) || defined (__GNUC__) || defined(_AIX32) || defined(WIN32)
int status;
int status;
#if !defined(__MWERKS__) // (using c methods)
struct tm *gmTime;
struct tm *gmTime;

time_t localEpoch, gmEpoch;
time_t localEpoch, gmEpoch;

/*First get local epoch time*/
localEpoch = time(NULL);
/*First get local epoch time*/
localEpoch = time(NULL);

/* Using local time epoch get the GM Time */
gmTime = gmtime(&localEpoch);
gmTime->tm_isdst = -1;
/* Convert gm time in to epoch format */
gmEpoch = mktime(gmTime);
/* Using local time epoch get the GM Time */
gmTime = gmtime(&localEpoch);
gmTime->tm_isdst = -1;
/* Convert gm time in to epoch format */
gmEpoch = mktime(gmTime);

timeZoneSecondsDifference =difftime(gmEpoch, localEpoch);
status = 0;
#else // __MWERKS__ (using OSX methods)
CFTimeZoneRef tzRef = ::CFTimeZoneCopySystem();
if (tzRef)
{
CFTimeInterval intervalFromGMT = ::CFTimeZoneGetSecondsFromGMT(tzRef, currentTime);
::CFRelease(tzRef);
timeZoneSecondsDifference = intervalFromGMT;
timeZoneSecondsDifference =difftime(gmEpoch, localEpoch);
status = 0;
}
else
status = -1;
#else // __MWERKS__ (using OSX methods)
CFTimeZoneRef tzRef = ::CFTimeZoneCopySystem();
if (tzRef)
{
CFTimeInterval intervalFromGMT = ::CFTimeZoneGetSecondsFromGMT(tzRef, currentTime);
::CFRelease(tzRef);
timeZoneSecondsDifference = intervalFromGMT;
status = 0;
}
else
status = -1;
#endif

if(0 == status)
{
if(0 == timeZoneSecondsDifference)
if(0 == status)
{
UTC = eSame;
if(0 == timeZoneSecondsDifference)
{
UTC = eSame;
}
else
{
UTC = timeZoneSecondsDifference > 0 ? eEarlier : eLater;
HourFromUTC = (int)(labs(timeZoneSecondsDifference) / 3600);
MinuteFromUTC = (int)((labs(timeZoneSecondsDifference) - (labs(timeZoneSecondsDifference) / 3600)*3600) / 60);
}
}
else
{
UTC = timeZoneSecondsDifference > 0 ? eEarlier : eLater;
HourFromUTC = (int)(labs(timeZoneSecondsDifference) / 3600);
MinuteFromUTC = (int)((labs(timeZoneSecondsDifference) - (labs(timeZoneSecondsDifference) / 3600)*3600) / 60);
UTC = eUndefined;
TRACE_LOG("PDFDate::SetToCurrentTime, Couldn't get UTC.");
}
}
else
{
UTC = eUndefined;
TRACE_LOG("PDFDate::SetToCurrentTime, Couldn't get UTC.");
}

#else
UTC = eUndefined;
UTC = eUndefined;
#endif
}

SAFE_LOCAL_TIME(structuredLocalTime,currentTime);

Year = structuredLocalTime.tm_year + 1900;
Month = structuredLocalTime.tm_mon + 1;
Day = structuredLocalTime.tm_mday;
Hour = structuredLocalTime.tm_hour;
Minute = structuredLocalTime.tm_min;
Second = structuredLocalTime.tm_sec;

}

void PDFDate::ParseString(std::string inValue)
Expand Down Expand Up @@ -315,4 +334,4 @@ void PDFDate::ParseString(std::string inValue)
MinuteFromUTC = Int(inValue.substr(20,2));
}

}
}
Loading