godber / pds3label

A Python module for parsing PDS 3 labels.
MIT License
0 stars 0 forks source link

Add support for date, time and datetime labels #10

Open godber opened 9 years ago

godber commented 9 years ago

Add support for date, time and datetime labels

https://github.com/godber/pds3label/blob/master/test/data/dates.lbl

PDS_VERSION_ID = PDS3
DATE1          = 1990-07-04
DATE2          = 1990-158
DATE3          = 2001-001
TIME1          = 12:00
TIME1_S        = 12:00:45
TIME1_S_FLOAT  = 12:00:45.4571
TIME2          = 15:24:12Z
TIME3          = 01:12:22+07
TIME4          = 01:12:22+7
TIME5          = 01:10:39.4575+07
DATE_TIME1     = 1990-07-04T12:00
DATE_TIME2     = 1990-158T15:24:12Z
DATE_TIME3     = 2001-001T01:10:39+7
DATE_TIME4     = 2001-001T01:10:39.457591+7
END

Datetime strings in the PDS label should be returned as Python Datetime objects.

godber commented 9 years ago

To do this task right, I think the grammar needs to be modified so that DATE, TIME, and DATE_TIME are grammar rules rather than tokens, unless I can find out how to access the nested tokens from the visitor level.

Basically I am thinking the following rule:

date_time_value
  : DATE
  | TIME
  | DATE_TIME
  ;

Should be something more along the line of:

date_time_value
  : DIGIT DIGIT DIGIT DIGIT '-' DIGIT+ '-' DIGIT+    # YEAR_MONTH_DAY
  | DIGIT DIGIT DIGIT DIGIT '-' DIGIT+               # YEAR_DOY
  | HH_MM_SS                                         # HH_MM_SS
  | HH_MM_SS 'Z'                                     # HH_MM_SS_Z
  | HH_MM_SS SIGN DIGIT+                             # HH_MM_SS_N
  | DATE_TIME ...
  ;

Maybe not, since DATE_TIME is DATE 'T' TIME.

Basically, what it comes down to is that in order to properly implement visitDate_time_value I think I am going to have to do a lot of string matching that should automatically happen in the grammar. Maybe I just don't get how to use it fully yet.