1 | # ---------------------------------------------------------------------------- |
||
2 | # This software is in the public domain, furnished "as is", without technical |
||
3 | # support, and with no warranty, express or implied, as to its usefulness for |
||
4 | # any purpose. |
||
5 | # |
||
6 | # offsetTime.py |
||
7 | # Handles Displaced Real Time for various applications |
||
8 | # |
||
9 | # Author: hansen/romberg |
||
10 | # ---------------------------------------------------------------------------- |
||
11 | |||
12 | import string |
||
13 | import time |
||
14 | |||
15 | # Given the timeStr, return the offset (in seconds) |
||
16 | # from the current time. |
||
17 | # Also return the launchStr i.e. Programs launched from this |
||
18 | # offset application will use the launchStr as the -z argument. |
||
19 | # The offset will be positive for time in the future, |
||
20 | # negative for time in the past. |
||
21 | # |
||
22 | # May still want it to be normalized to the most recent midnight. |
||
23 | # |
||
24 | # NOTES about synchronizing: |
||
25 | # --With synchronizing on, the "current time" for all processes started |
||
26 | # within a given hour will be the same. |
||
27 | # This guarantees that GFE's have the same current time and ISC grid |
||
28 | # time stamps are syncrhonized and can be exchanged. |
||
29 | # Formatters launched from the GFE in this mode will be synchronized as |
||
30 | # well by setting the launchStr to use the time difference format |
||
31 | # (YYYYMMDD_HHMM,YYYYMMDD_HHMM). |
||
32 | # --This does not solve the problem in the general case. |
||
33 | # For example, if someone starts the GFE at 12:59 and someone |
||
34 | # else starts it at 1:01, they will have different offsets and |
||
35 | # current times. |
||
36 | # --With synchronizing off, when the process starts, the current time |
||
37 | # matches the drtTime in the command line. However, with synchronizing |
||
38 | # on, the current time will be offset by the fraction of the hour at |
||
39 | # which the process was started. Examples: |
||
40 | # Actual Starting time: 20040617_1230 |
||
41 | # drtTime 20040616_0000 |
||
42 | # Synchronizing off: |
||
43 | # GFE Spatial Editor at StartUp: 20040616_0000 |
||
44 | # Synchronizing on: |
||
45 | # GFE Spatial Editor at StartUp: 20040616_0030 |
||
46 | # |
||
47 | |||
48 | |||
49 | View Code Duplication | def determineDrtOffset(timeStr): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
50 | launchStr = timeStr |
||
51 | # Check for time difference |
||
52 | if timeStr.find(",") >= 0: |
||
53 | times = timeStr.split(",") |
||
54 | t1 = makeTime(times[0]) |
||
55 | t2 = makeTime(times[1]) |
||
56 | return t1-t2, launchStr |
||
57 | # Check for synchronized mode |
||
58 | synch = 0 |
||
59 | if timeStr[0] == "S": |
||
60 | timeStr = timeStr[1:] |
||
61 | synch = 1 |
||
62 | drt_t = makeTime(timeStr) |
||
63 | gm = time.gmtime() |
||
64 | cur_t = time.mktime(gm) |
||
65 | |||
66 | # Synchronize to most recent hour |
||
67 | # i.e. "truncate" cur_t to most recent hour. |
||
68 | if synch: |
||
69 | cur_t = time.mktime((gm[0], gm[1], gm[2], gm[3], 0, 0, 0, 0, 0)) |
||
70 | curStr = '%4s%2s%2s_%2s00\n' % (repr(gm[0]), repr(gm[1]), |
||
71 | repr(gm[2]), repr(gm[3])) |
||
72 | curStr = curStr.replace(' ', '0') |
||
73 | launchStr = timeStr + "," + curStr |
||
74 | |||
75 | offset = drt_t - cur_t |
||
76 | return int(offset), launchStr |
||
77 | |||
78 | |||
79 | def makeTime(timeStr): |
||
80 | year = string.atoi(timeStr[0:4]) |
||
81 | month = string.atoi(timeStr[4:6]) |
||
82 | day = string.atoi(timeStr[6:8]) |
||
83 | hour = string.atoi(timeStr[9:11]) |
||
84 | minute = string.atoi(timeStr[11:13]) |
||
85 | # Do not use daylight savings because gmtime is not in daylight |
||
86 | # savings time. |
||
87 | return time.mktime((year, month, day, hour, minute, 0, 0, 0, 0)) |
||
88 |