Statistics
| Branch: | Tag: | Revision:

root / iooclient / timehandler.py @ 235a5968

History | View | Annotate | Download (1.8 kB)

1
# Copyright 2012 Leonidas Poulopoulos
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#    http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

    
15
import datetime
16
import time
17

    
18
def asdayPeriod(timestamp):
19
    '''
20
    Transforms a python datetime timestamp to Alcatel friendly timestamp
21
    
22
    @type timestamp: Python datetime
23
    @param timestamp: The datetime timestamp
24
    
25
    @rtype: String
26
    @return: An Alcatel-friendly string: YYYYMMDDQH where QH in [00,96] representing 15min periods in a day.
27
    '''
28
    daystring = timestamp.strftime('%Y%m%d')
29
    dayquarter = "%s" %((timestamp.hour*60 + timestamp.minute)//15)
30
    return "%s%s" %(daystring,dayquarter.zfill(2))
31

    
32
def fromdayPeriod(timestring):
33
    '''
34
    Transforms an Alcatel-friendly timestamp to a python datetime timestamp
35
    
36
    @type timestring: String
37
    @param timestring: An Alcatel-friendly string: YYYYMMDDQH where QH in [00,96] representing 15min periods in a day.
38
    
39
    @rtype: Datetime
40
    @return: A python datetime timestamp
41
    '''
42
    textdate = timestring[0:8]
43
    dayquarter = timestring[8:10]
44
    # we "steal" a quarter of an hour here...
45
    daymins = (int(dayquarter)-1)*15
46
    hours = daymins//60
47
    mins = daymins - hours*60
48
    datetimestamp = datetime.datetime(*time.strptime(textdate, '%Y%m%d')[:3]+(hours, mins))
49
    # ...and we return it here:
50
    datetimestamp = datetimestamp + datetime.timedelta(minutes=15)
51
    return datetimestamp
52