Statistics
| Branch: | Tag: | Revision:

root / iooclient / timehandler.py @ 235a5968

History | View | Annotate | Download (1.8 kB)

1 235a5968 Leonidas Poulopoulos
# Copyright 2012 Leonidas Poulopoulos
2 235a5968 Leonidas Poulopoulos
#
3 235a5968 Leonidas Poulopoulos
# Licensed under the Apache License, Version 2.0 (the "License");
4 235a5968 Leonidas Poulopoulos
# you may not use this file except in compliance with the License.
5 235a5968 Leonidas Poulopoulos
# You may obtain a copy of the License at
6 235a5968 Leonidas Poulopoulos
#
7 235a5968 Leonidas Poulopoulos
#    http://www.apache.org/licenses/LICENSE-2.0
8 235a5968 Leonidas Poulopoulos
#
9 235a5968 Leonidas Poulopoulos
# Unless required by applicable law or agreed to in writing, software
10 235a5968 Leonidas Poulopoulos
# distributed under the License is distributed on an "AS IS" BASIS,
11 235a5968 Leonidas Poulopoulos
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 235a5968 Leonidas Poulopoulos
# See the License for the specific language governing permissions and
13 235a5968 Leonidas Poulopoulos
# limitations under the License.
14 235a5968 Leonidas Poulopoulos
15 235a5968 Leonidas Poulopoulos
import datetime
16 235a5968 Leonidas Poulopoulos
import time
17 235a5968 Leonidas Poulopoulos
18 235a5968 Leonidas Poulopoulos
def asdayPeriod(timestamp):
19 235a5968 Leonidas Poulopoulos
    '''
20 235a5968 Leonidas Poulopoulos
    Transforms a python datetime timestamp to Alcatel friendly timestamp
21 235a5968 Leonidas Poulopoulos
    
22 235a5968 Leonidas Poulopoulos
    @type timestamp: Python datetime
23 235a5968 Leonidas Poulopoulos
    @param timestamp: The datetime timestamp
24 235a5968 Leonidas Poulopoulos
    
25 235a5968 Leonidas Poulopoulos
    @rtype: String
26 235a5968 Leonidas Poulopoulos
    @return: An Alcatel-friendly string: YYYYMMDDQH where QH in [00,96] representing 15min periods in a day.
27 235a5968 Leonidas Poulopoulos
    '''
28 235a5968 Leonidas Poulopoulos
    daystring = timestamp.strftime('%Y%m%d')
29 235a5968 Leonidas Poulopoulos
    dayquarter = "%s" %((timestamp.hour*60 + timestamp.minute)//15)
30 235a5968 Leonidas Poulopoulos
    return "%s%s" %(daystring,dayquarter.zfill(2))
31 235a5968 Leonidas Poulopoulos
32 235a5968 Leonidas Poulopoulos
def fromdayPeriod(timestring):
33 235a5968 Leonidas Poulopoulos
    '''
34 235a5968 Leonidas Poulopoulos
    Transforms an Alcatel-friendly timestamp to a python datetime timestamp
35 235a5968 Leonidas Poulopoulos
    
36 235a5968 Leonidas Poulopoulos
    @type timestring: String
37 235a5968 Leonidas Poulopoulos
    @param timestring: An Alcatel-friendly string: YYYYMMDDQH where QH in [00,96] representing 15min periods in a day.
38 235a5968 Leonidas Poulopoulos
    
39 235a5968 Leonidas Poulopoulos
    @rtype: Datetime
40 235a5968 Leonidas Poulopoulos
    @return: A python datetime timestamp
41 235a5968 Leonidas Poulopoulos
    '''
42 235a5968 Leonidas Poulopoulos
    textdate = timestring[0:8]
43 235a5968 Leonidas Poulopoulos
    dayquarter = timestring[8:10]
44 235a5968 Leonidas Poulopoulos
    # we "steal" a quarter of an hour here...
45 235a5968 Leonidas Poulopoulos
    daymins = (int(dayquarter)-1)*15
46 235a5968 Leonidas Poulopoulos
    hours = daymins//60
47 235a5968 Leonidas Poulopoulos
    mins = daymins - hours*60
48 235a5968 Leonidas Poulopoulos
    datetimestamp = datetime.datetime(*time.strptime(textdate, '%Y%m%d')[:3]+(hours, mins))
49 235a5968 Leonidas Poulopoulos
    # ...and we return it here:
50 235a5968 Leonidas Poulopoulos
    datetimestamp = datetimestamp + datetime.timedelta(minutes=15)
51 235a5968 Leonidas Poulopoulos
    return datetimestamp