Changeset 8580 for mauRepo


Ignore:
Timestamp:
28/09/12 13:26:17 (7 years ago)
Author:
mnagni
Message:

Incomplete - # 22558: Formatting of start/end times to conform to OpenSearch? time extension
 http://team.ceda.ac.uk/trac/ceda/ticket/22558

a new string_to_datetime method has been added. It accepts the following format:

  • datatime with timezone 'YYYY-MM-DDTHH:mm:SS[+,-]hhmm'

o example: '1996-12-19T16:39:57-08:00'

  • datatime without timezone 'YYYY-MM-DDTHH:mm:SS'

o example: '1996-12-19T16:39:57' and is equivalent to '1996-12-19T16:39:57+00:00'

  • datatime without timezone 'YYYY-MM-DD'

o example: '1996-12-19' and is equivalent to '1996-12-19T00:00:00+00:00'

Following the OS Time extension specification, a new timeformat has been added to the string_to_datetime method

datatime with UTC timezone 'YYYY-MM-DDTHH:mm:SSZ' example: '1996-12-19T16:39:57Z' and is equivalent to '1996-12-19T16:39:57+00:00'

The previous format is equivalent to datatime without timezone

Location:
mauRepo/HPFos/trunk/hpfos
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/HPFos/trunk/hpfos/HPFos/osImpl/commons.py

    r8565 r8580  
    3636from ea_model.iso_19108_2006_temporal_schema.temporal_objects.tm_instant import TM_Instant 
    3737from ea_model.iso_19108_2006_temporal_schema.temporal_objects.tm_period import TM_Period 
    38 import datetime 
     38import re 
     39from datetime import tzinfo, timedelta, datetime 
     40 
     41class OS_tz(tzinfo): 
     42    def __init__(self, minutes): 
     43        self.__offset = timedelta(minutes = minutes)         
     44     
     45    def utcoffset(self, dt): 
     46        return self.__offset 
     47    def dst(self, dt): 
     48        return timedelta(0) 
     49    def tzname(self,dt): 
     50        return "Not Defined" 
    3951 
    4052DATE_FORMAT = '%Y-%m-%d' 
    41 DATETIME_FORMAT = DATE_FORMAT + 'T%H:%M:%SZ' 
     53DATETIME_FORMAT = DATE_FORMAT + 'T%H:%M:%S' 
     54 
     55def string_to_datetime(datestr): 
     56    datetimeformat_withtz = r'\d{4}\-([0]{1}\d{1}|[1]{1}[0-2]{1})\-([0-2]{1}\d{1}|[3]{1}[0-1]{1})T([0-1]{1}\d{1}|[2]{1}[0-3]{1})\:([0-5]{1}\d{1})\:([0-5]{1}\d{1})[\-\+]([0-1]{1}\d{1}|[2]{1}[0-3]{1})\:([0-5]{1}\d{1})' 
     57    datetimeformat_withnotz  = r'\d{4}\-([0]{1}\d{1}|[1]{1}[0-2]{1})\-([0-2]{1}\d{1}|[3]{1}[0-1]{1})T([0-1]{1}\d{1}|[2]{1}[0-3]{1})\:([0-5]{1}\d{1})\:([0-5]{1}\d{1})' 
     58    datetimeformat_withUTC  = r'\d{4}\-([0]{1}\d{1}|[1]{1}[0-2]{1})\-([0-2]{1}\d{1}|[3]{1}[0-1]{1})T([0-1]{1}\d{1}|[2]{1}[0-3]{1})\:([0-5]{1}\d{1})\:([0-5]{1}\d{1})Z' 
     59    dateformat  = r'\d{4}\-([0]{1}\d{1}|[1]{1}[0-2]{1})\-([0-2]{1}\d{1}|[3]{1}[0-1]{1})'     
     60     
     61    datetime_withtz = re.match(datetimeformat_withtz, datestr) 
     62    datetime_withnotz = re.match(datetimeformat_withnotz, datestr) 
     63    datetime_withUTC = re.match(datetimeformat_withUTC, datestr) 
     64 
     65    tzmins = None 
     66    if datetime_withtz is not None: 
     67        mytz = datestr.replace(datetime_withnotz.group(0),'') 
     68        hours, mins = mytz[1:].split(':') 
     69        tzmins = int(hours)*60 + int(mins) 
     70        if mytz.startswith('-'): 
     71            tzmins = -1*tzmins 
     72 
     73    ret_datetime = None 
     74    if datetime_withUTC is not None: 
     75        ret_datetime = \ 
     76            datetime.strptime(datetime_withUTC.group(0)[:-1], DATETIME_FORMAT)             
     77    elif datetime_withnotz is not None: 
     78        ret_datetime = \ 
     79            datetime.strptime(datetime_withnotz.group(0), DATETIME_FORMAT) 
     80             
     81    if tzmins is not None and ret_datetime is not None:               
     82        ret_datetime = ret_datetime.replace(tzinfo = OS_tz(tzmins)) 
     83        return ret_datetime 
     84         
     85    datetime_simple = re.match(dateformat, datestr)         
     86    if datetime_simple is not None: 
     87        ret_datetime = datetime.strptime(datetime_simple.group(0), DATE_FORMAT) 
     88        return ret_datetime 
    4289 
    4390def tm_InstantToDatetime(tm_instant): 
    4491    idate = tm_instant.position.date8601 
    4592    return datetime.date(int(idate.year), int(idate.month), int(idate.day)) 
    46  
    47 def isoDateStringToDatetime(datestring): 
    48     try: 
    49         return isoDateStringToTimeDate(datestring) 
    50     except: 
    51         try: 
    52             return _isoDateTimeStringToTimeDate(datestring) 
    53         except: 
    54             pass 
    55  
    56 def isoDateStringToTimeDate(datestring): 
    57     ''' 
    58         Return a datatime.datatime instance. 
    59         @param datestring: a date string formatted as '%Y-%m-%d' 
    60     ''' 
    61     return datetime.datetime.strptime(datestring, DATE_FORMAT) 
    62  
    63 def _isoDateTimeStringToTimeDate(timestring): 
    64     ''' 
    65         Return a datatime.datatime instance. 
    66         @param timestring: a time string formatted as '%Y-%m-%dT%H:%M:%SZ' 
    67     ''' 
    68     try: 
    69         return datetime.datetime.strptime(timestring, DATETIME_FORMAT) 
    70     except: 
    71         pass 
    7293 
    7394def from_pt_to_string(phenomenonTime): 
     
    92113def _tm_InstantToString(tm_instant): 
    93114    idate = tm_instant.position.date8601 
    94     return datetime.datetime(int(idate.year), int(idate.month), int(idate.day), 0, 0, 0).isoformat() 
     115    return datetime(int(idate.year), int(idate.month), int(idate.day), 0, 0, 0).isoformat() 
    95116 
    96117def return_not_none_text(element): 
  • mauRepo/HPFos/trunk/hpfos/HPFos/osImpl/myimpl.py

    r8579 r8580  
    4040from ceda_markup.gml.gml import createBeginPosition, createEndPosition, \ 
    4141    createTimePeriod, createValidTime, createEnvelope, createLowerCorner, \ 
    42     createUpperCorner, createPosList, createLinearRing, createExterior, createPolygon, \ 
    43     GML_NAMESPACE 
     42    createUpperCorner, createPosList, createLinearRing, createExterior, \ 
     43    createPolygon, GML_NAMESPACE 
    4444from ceda_markup.georss.georss import createWhere 
    4545from ceda_markup.atom.link import REL_SEARCH, REL_ALTERNATE, createLink 
     
    4949from ceda_markup.opensearch.template.atom import OSAtomResponse 
    5050from ceda_markup.opensearch.template.html import OSHTMLResponse 
    51 from ea_model.iso_19108_2006_temporal_schema.temporal_objects.tm_instant import TM_Instant 
     51from ea_model.iso_19108_2006_temporal_schema.temporal_objects.tm_instant \ 
     52    import TM_Instant 
    5253from ceda_markup.georss import create_where_from_postgis 
    5354from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection \ 
     
    6061from ceda_markup.atom.atom import ATOM_NAMESPACE 
    6162from hpfos.HPFos.osImpl.commons import get_document, get_xml_document, \ 
    62     from_pt_to_string, tm_InstantToDatetime, isoDateStringToTimeDate 
     63    from_pt_to_string, tm_InstantToDatetime, string_to_datetime 
    6364from ceda_markup.opensearch.os_request import OS_NAMESPACE 
    6465from xml.etree.ElementTree import _ElementInterface 
     
    446447        param_7 = OSParam("start", "start",  
    447448                          namespace = "http://a9.com/-/opensearch/extensions/time/1.0/")         
    448         param_8 = OSParam("stop", "stop",  
     449        param_8 = OSParam("stop", "end",  
    449450                          namespace = "http://a9.com/-/opensearch/extensions/time/1.0/")         
    450451        params = [param_1, param_2, param_3, param_4, param_5, param_6, param_7, param_8]  
     
    468469        stop = None 
    469470        if context['start'] is not None: 
    470             start = isoDateStringToTimeDate(context['start']) 
     471            start = string_to_datetime(context['start']) 
    471472        if context['stop'] is not None: 
    472             stop = isoDateStringToTimeDate(context['stop'])             
     473            stop = string_to_datetime(context['stop'])             
    473474         
    474475        if not context.has_key(GUID) or context[GUID] is None: 
  • mauRepo/HPFos/trunk/hpfos/tests/testos.py

    r8523 r8580  
    3131@author: mnagni 
    3232''' 
    33 from hpfos.HPFos.view.view import getDescription, doSearchL0 
    3433from hpfos.tests.testsetup import HPFosTest 
    3534from hpfos.HPFos.osImpl.myimpl import MyOSAtomResponse 
    3635from ceda_markup.opensearch.template.osresponse import Subresult, Result 
    3736from ceda_markup.opensearch.query import create_query 
     37from hpfos.HPFos.osImpl.commons import string_to_datetime 
     38from hpfos.HPFos.view.view import do_search_l0, get_description 
    3839 
    3940 
     
    4243    def description_test(self): 
    4344        get_request = self.reqFactory.get('/description/') 
    44         res = getDescription(request = get_request) 
    45         print res         
     45        res = get_description(request = get_request)         
    4646 
    4747    def search_test(self): 
     
    4949        get_request = self.reqFactory.get('/atom/search', {'count': '3'}) 
    5050        get_request.moles3EPB = self.epbRepo 
    51         res = doSearchL0(iformat = iformat, request = get_request) 
    52         print res 
     51        res = do_search_l0(iformat = iformat, request = get_request) 
    5352 
    5453    def create_query_test(self): 
    5554        context = self._generate_context()   
    5655        query = create_query('atom', self.os_engine.os_query.params_model, context) 
    57         print query 
    5856        return query 
     57     
     58    def datetime_test(self): 
     59        my_date = '1996-12-19T16:39:57-08:00' 
     60        start = string_to_datetime(my_date) 
     61        self.assertEqual('1996-12-19 16:39:57-08:00', str(start), "Error") 
     62         
     63        my_date = '1996-12-19T16:39:57Z' 
     64        start = string_to_datetime(my_date) 
     65        self.assertEqual('1996-12-19 16:39:57', str(start), "Error") 
     66         
     67        my_date = '1996-12-19T16:39:57' 
     68        start = string_to_datetime(my_date) 
     69        self.assertEqual('1996-12-19 16:39:57', str(start), "Error") 
     70         
     71        my_date = '1996-12-19' 
     72        start = string_to_datetime(my_date) 
     73        self.assertEqual('1996-12-19 00:00:00', str(start), "Error") 
     74         
     75        my_date = '1996-12+19' 
     76        start = string_to_datetime(my_date) 
     77        self.assertEqual(None, start, "Error") 
    5978     
    6079    def generate_reponse_test(self): 
     
    6685        response = ar.generate_response(result, queries, '', context) 
    6786        self.assertTrue('<link href="/search/atom/?startIndex=3" rel="self" type="application/atom+xml"/>' in response, "Error") 
    68         self.assertTrue('<link href="/search/atom/?startIndex=1" rel="prev" type="application/atom+xml"/>' in response, "Error")         
     87        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="previous" type="application/atom+xml"/>' in response, "Error")         
    6988        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="first" type="application/atom+xml"/>' in response, "Error") 
    7089        self.assertTrue('<link href="/search/atom/?startIndex=10" rel="next" type="application/atom+xml"/>' in response, "Error") 
     
    7493        response = ar.generate_response(result, queries, '', context) 
    7594        self.assertTrue('<link href="/search/atom/?startIndex=5" rel="self" type="application/atom+xml"/>' in response, "Error") 
    76         self.assertTrue('<link href="/search/atom/?startIndex=1" rel="prev" type="application/atom+xml"/>' in response, "Error")         
     95        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="previous" type="application/atom+xml"/>' in response, "Error")         
    7796        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="first" type="application/atom+xml"/>' in response, "Error") 
    7897        self.assertTrue('<link href="/search/atom/?startIndex=15" rel="next" type="application/atom+xml"/>' in response, "Error") 
     
    82101        response = ar.generate_response(result, queries, '', context) 
    83102        self.assertTrue('<link href="/search/atom/?startIndex=2" rel="self" type="application/atom+xml"/>' in response, "Error") 
    84         self.assertTrue('<link href="/search/atom/?startIndex=1" rel="prev" type="application/atom+xml"/>' in response, "Error")         
     103        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="previous" type="application/atom+xml"/>' in response, "Error")         
    85104        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="first" type="application/atom+xml"/>' in response, "Error") 
    86105        self.assertTrue('<link href="/search/atom/?startIndex=4" rel="next" type="application/atom+xml"/>' in response, "Error") 
     
    90109        response = ar.generate_response(result, queries, '', context) 
    91110        self.assertTrue('<link href="/search/atom/?startIndex=18" rel="self" type="application/atom+xml"/>' in response, "Error") 
    92         self.assertTrue('<link href="/search/atom/?startIndex=17" rel="prev" type="application/atom+xml"/>' in response, "Error")         
     111        self.assertTrue('<link href="/search/atom/?startIndex=17" rel="previous" type="application/atom+xml"/>' in response, "Error")         
    93112        self.assertTrue('<link href="/search/atom/?startIndex=1" rel="first" type="application/atom+xml"/>' in response, "Error") 
    94113        self.assertTrue('<link href="/search/atom/?startIndex=19" rel="next" type="application/atom+xml"/>' in response, "Error") 
  • mauRepo/HPFos/trunk/hpfos/tests/testsetup.py

    r8548 r8580  
    3939        ibbox = create_st_setSRID(180,-90,-180,90) 
    4040        res = HPFosTest.epbRepo.getObservationCollections(bbox=ibbox) 
    41         print res 
    4241 
    4342class MyRequest(RequestFactory): 
Note: See TracChangeset for help on using the changeset viewer.