help-glpk
[Top][All Lists]

## Re: [Help-glpk] Time conversion functions

 From: Andrew Makhorin Subject: Re: [Help-glpk] Time conversion functions Date: Wed, 19 Nov 2008 00:13:29 +0300

```> currently I am using GLPK mainly for scheduling problems. Here the data
> often contains references to time. The GMPL language does not provide any
> functions to convert timestamps to numbers and numbers back to timestamps.

Just an example that demonstrates another way to implement date/time in
MathProg without any changes in the translator.

The model below implements the Gregorian calendar. Parameter jday
converts the plain date to the Julian day, and parameters year, month,
and day converts the julian day to the corresponding date components.
Though the model is a bit intricated, it works correctly for all dates
in the range from 0001/01/01 to 4000/12/31.

/* date.mod */

param jday{y in 1..4000, m in 1..12, d in 1..31}, integer, :=
(146097 * ((y - (if m <= 2 then 1)) div 100)) div 4 +
(1461 * ((y - (if m <= 2 then 1)) mod 100)) div 4 +
(153 * (m + (if m <= 2 then 9 else -3)) + 2) div 5 + d + 1721119;

param jdate{f in 0..10, j in 1721426..3182395}, integer, :=
if f = 0 then
j - 1721119
else if f = 1 then
(4 * jdate[0,j] - 1) div 146097
else if f = 2 then
(4 * jdate[0,j] - 1) mod 146097
else if f = 3 then
jdate[2,j] div 4
else if f = 4 then
(4 * jdate[3,j] + 3) div 1461
else if f = 5 then
(4 * jdate[3,j] + 3) mod 1461
else if f = 6 then
(jdate[5,j] + 4) div 4
else if f = 7 then
(5 * jdate[6,j] - 3) div 153
else if f = 8 then
(5 * jdate[6,j] - 3) mod 153
else if f = 9 then
(jdate[8,j] + 5) div 5
else /* f = 10 */
100 * jdate[1,j] + jdate[4,j];

param year{j in 1721426..3182395}, integer, :=
jdate[10,j] + (if jdate[7,j] <= 9 then 0 else 1);

param month{j in 1721426..3182395}, integer, :=
jdate[7,j] + (if jdate[7,j] <= 9 then +3 else -9);

param day{j in 1721426..3182395}, integer, :=
jdate[9,j];

/**********************************************************************/

param YEAR := 2008;

param beg := jday[YEAR,1,1];

param end := jday[YEAR,12,31];

printf{j in beg..end} "j = %d; day = %02d month = %02d year = %04d\n",
j, day[j], month[j], year[j];

end;

```