help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] Time conversion functions


From: Xypron
Subject: Re: [Help-glpk] Time conversion functions
Date: Sat, 22 Nov 2008 23:24:52 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.18) Gecko/20081030 SeaMonkey/1.1.13

Hello Andrew,

I am not sure if you received my previous mail due to the length of the attachment.

Andrew Makhorin wrote:
Hi Xypron,

I plan to implement some basic date/time functions in mathprog based
on your implementation, and I would like to add a notice that we both
are authors of the code. What do you think about that?

At first I would like to tentatively implement two functions str2time
and time2str (the latter is equivalent to your localtime).

Best regards,

Andrew Makhorin

  
Sounds great to me.

I have uploaded my implementation of the conversion function to
ftp://glpk.dyndns.org/glpk/glpk-4.33-strdate.tar.gz

include/glptime.h contains declarations for time functions
src/glptime.h contains the implementation of the time functions
include/glpmpl.h contains the new function codes
src/glpmpl01.c and glpmpl03.c have been enhanced.

My implementation currently uses two consecutive transformations:
str->double:
1st step: convert char * to struct tm. This is done in function str2timeparse.
2nd step: convert struct tm to double. This is done in str2time.
double->str
1st step: convert double to struct tm. This is done in gmtime2str and localtime2str.
2nd step: convert struct tm to char *. This is done in time2str.

In my view the main deficiency of my implementation is the use of function timegm(). This function is not defined by C99. Hence on some systems it may not be available. We should have our own code for transforming time_h to struct tm (or some other structure we might invent).

What was important to me was error handling:

If the format string is illegal the output will be something like:
unknown format string
%V-W%W-%w %Y-%i %FT%TZ
              ^
test.mod:8: gmtime(347155200.000000, '%V-W%W-%w %Y-%i %FT%TZ') failed
The caret tells the user where the error is.

If the time string is illegal the output will be something like:
expected digit
1981-01-0a1Z
         ^
test.mod:8: str2time('1981-01-0a1Z', '%Y-%m-%d%z') failed


When it comes to time zones C99 only supports two time zones: UTC and the local time of the user.  If we wanted to implement time zones ourselves, this would requiring updating time zone information whenever some government on the world changes the rules for daylight saving time. I guess this is a burden we should not load onto our shoulders. Supporting both timestamps for UTC and local time, relying on the operating system to know what the local time offset to UTC is, will help many applications and does not cause too much implementation effort. When you look into my code you will find that only very few lines where needed to add both local time and UTC support.

When I made my implementation I was considering the various date format that exist e.g.
dd.mm.yyyy in Russia and Germany
mm/dd/yy in the US
dd/mm/yy in France
yyyy-mm-dd according to ISO 8601

This is why I use format strings for all transformations.

When it comes to transformation to calendar weeks I have seen at least two systems that exist:
ISO 8601 specifies that a calendar week starts on monday and belongs to the year containing the thursday of the week. This is what I implemented.
The US system has weeks from Sunday through Saturday, and partial weeks at the beginning and the end of the year.

You can use the following model for testing with my coding with transformation to calendar weeks:
set S := {1981..2010};
param ti{s in S} := str2time( s & '-01-01Z', '%Y-%m-%d%z');
param ta{s in S} := str2time( s & '-12-31Z', '%Y-%m-%d%z');
param si{s in S} symbolic := gmtime( ti[s], '%V-W%W-%w %Y-%j %FT%TZ' );
param sa{s in S} symbolic := gmtime( ta[s], '%V-W%W-%w %Y-%j %Y-%m-%dT%H:%M:%SZ' );
solve;
for {s in S}
  printf "%s, %s\n", si[s], sa[s];

Furthermore I have added an example model: examples/date.mod

A good description of ISO 8601 can be found on wikipedia:
http://en.wikipedia.org/wiki/ISO_8601

Supporting duration would be helpful for planning and scheduling applications. As I already created the necessary transformation you might consider to add this to the distribution.

As mentioned before I keep my code in a subversion repository. I have updated all files there.
You can check it out with
svn co http://glpk.dyndns.org/svn/glpk/glpk/branches/glpk-4.33-strdate
or if you are working with Windows you could use TortoiseSVN as a subversion client (version less then 1.2 will be needed for Windows 98).

Best regards

Xypron



reply via email to

[Prev in Thread] Current Thread [Next in Thread]