-
Notifications
You must be signed in to change notification settings - Fork 0
/
vreme.py
1418 lines (1202 loc) · 53.2 KB
/
vreme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#2006-2008 chefob sdobrev ico2
# -*- coding: utf8 -*-
'''
Universal Calendar Time and Pediods and arithmetics;
physical, logical (next-working-day), inherit/compose, count, compare, overlap, cut
ВРЕМЕТО Е ПАРИ!!!
нещо изразяващо време, трябва да може:
- да се преобразува в произволно друго нещо изразяващо време (за някои от нещата
се изискват допълнителни контекстни данни/дати затова)( в merki.py има наченки)
- да се преобразува от/към низ (dateutil.parse стига)
- да се сравняват по големина (евентуално по начало и/или край) със зададена точност
- да се повтаря/случва....???
- да се изразява в различен вид потребителски представяния, включително и
непълни представяния като интервал зададен като 'месец 10 2008'....
- аритм(ет)и(ята)ката с дати, интервали и др. свинщини
най-често точността за целите на хор ще бъде ден (или час), май.
могат да се преобразуват само периоди в периоди, абс.дата в относителна и обратно
задачката по нарязване на времеви интервал на минимален брой константни периоди се решава другаде.
- ще ни трябват още и обединяване на времеви интервали (като възникнат интервали от различни източници...)
а имаше и още една боза при изчисляване на лихвите се ползваше понятието база 360/360, 365/360...
кратко описание: gate/trac/wiki/Vreme
--------
time is money!
something expressing time, should be able to:
- convert to anything else expressing time (some of them require
extra context data/dates) (merki.py some beginnings)
- convert to/from text (dateutil.parse is ok)
- compare by size (and eventually by start/end), given precision
- repeat/ happen ...
- express in various user presentations, incl. incomplete, like the period 'month 10 2008'
- arithmetics with dates, periods etc things
other:
- conversion is only period to period, abs to/from relative date
- the task of splitting a period into minimum const-sized subperiods is elsewhere
- may need also uniting of periods from various sources
- banking/interests used something called base 360/360, 365/360...
'''
from dateutil.relativedelta import *
from dateutil.rrule import *
import dateutil.parser as parser
import datetime as dt
from svd_util.mix import neighbours
from svd_util.py3 import dictOrder
_now = dt.datetime.now
class BasicCalendar( object):
''' представя физическия календар, т.е. 'непрекъснато' време без неработни дни (периоди)
някъв интерфейс
'''
pass
class BaseInitShow( object):
_attrs = '' # set this below
_str_sep = '|'
def __init__( me, *args): #args must be in _attrs order
i = 0
for each_attr in me._attrs.split():
try:
setattr( me, each_attr, args[ i])
except IndexError:
assert 0, 'wrong number of parameters to __init__ expected: '+ repr( me._attrs)
i += 1
def __str__( me):
return me.__class__.__name__ + '.' + me._str_sep.join( str( getattr( me, attr)) for attr in me._attrs.split())
__repr__ = __str__
class TimeConversion( BaseInitShow):
CanConvert, NoConvert = 'convertible', 'non-convertible'
_attrs = 'code'
_order = []
@staticmethod #zaemki ot merki posle shte gi obedinim/popravim XXX
def setup_chain( table):
for p,mul in table:
mm=0
for q,m in table:
if m is None:
continue
if mm:
q._convert[ p ] = mm
p._convert[ q ] = -mm
if (p is q): # or ( p.typ != q.typ):
q._convert[ p ] = 1
#p._convert[ q ] = 1
mm=m
else: mm*=m
#print '@'.join( str(i) for i in [ p, q, mm, -mm])
@classmethod
def all( klas):
assert klas._order
return klas._order
@classmethod
def setup( klas, units):
for k,v in units.iteritems():
tc_inst = klas( code= k, *v )
setattr( klas, k, tc_inst)
klas._order.append( tc_inst)
def __init__( me, ime, selector =None, code =None):
''' measure_to, measure_from - имената на мерките са в единствено число
isDateRequired - дали иска контекстна дата
'''
BaseInitShow.__init__( me, *tuple( None for i in range( len(me._attrs))) )
me.ime = (ime is None) and me.measure_from or ime #choveshko ime
me.typ = ( selector is None) and me.CanConvert or me.NoConvert
me.selector = selector
me.code = code
if code == None:
me.code = ime
me._convert = {}
def __repr__( me): # needed by storeStrin
return '.'.join( [ me.__class__.__name__, str( me.code)])
def _razmnoji_mesechinata( me, from_date, times):
broi_dni = 0
each_dict = dict( [( me.selector, 1)] )
#delta = relativedelta( **each_dict)
delta = RelativeDelta( months= 1)
each_date = from_date
mesechinko = me.__class__.mesec
for i in range( times):
broi_dni += mesechinko.broi_dni( each_date) #, calendar TODO
each_date += delta
return broi_dni
def broi_dni( me, context_date):
'''връща броя дни за периода започващ от контекстната дата за
неконвертируемите периоди или 1'''
if me.typ is me.NoConvert:
coef_to_mesec = abs( me._convert[ me.mesec])
if me == me.mesec:
return rrule( MONTHLY,
dtstart= dt.datetime( context_date.year, context_date.month, 1),
bymonthday=(-1),
count=1,
) [0].day #posledniat den ot mesec X
res = me._razmnoji_mesechinata( from_date= context_date, times= coef_to_mesec) #mesechinko.broi_dni( context_date) # FIXME loop-вай тука
return res
return 1 #TODO
''' конвертируеми периоди неконвертируеми периоди (без контекстна дата)
minuta chas den sedmica sedmica2 mesec trimesecie polugodie godina
minuta 1 1/60 1/60*24 1/broi_dni*60*24 1/broi_dni*60*24*3
chas 60 1 1/24 1/24*7 1/24*7*2 1/broi_dni*24 1/broi_dni*24*3
den 24*60 24 1 1/7 1/7*2 1/broi_dni 1/broi_dni*3
sedmica 7*24*60
sedmica2 2*7*24*60
mesec broi_dni*24*60
trimesecie 3*broi_dni*24*60
polugodie 6*broi_dni*24*60
godina 12*broi_dni*24*60
брои_дни е сума за всеки месец от контекстната дата до края на периода
'''
def convert( me, to, value, context_date =None): #, calendar =BasicCalendar): TODO
koef = me._convert[ to]
if me.typ is me.NoConvert:
broi_dni = me._razmnoji_mesechinata( context_date, value)
koef *= broi_dni
if koef < 0:
return value/ koef
return value * koef
TCS = TimeConversion
if 10:
TCS.setup(
dictOrder( [
('mikrosekunda', ( 'микросекунда',)),
('sekunda', ( 'секунда',)),
('minuta' , ( 'минута',)), #timedelta(0,60)
('chas' , ( 'час',)),
('den' , ( 'ден',)), #timedelta(1) = 24*chas
('sedmica', ( 'седмица',)),
('sedmica2', ( '2седмици',)),
('mesec', ( 'месец', 'months')), # бр.дни= календар
# ( trimesecie ( 'тримесечие', '')), # бр.дни= календар
# (polugodie ( 'полугодие', NoConvert)), # бр.дни= календар
('godina' , ( 'година', 'years')), # бр.дни= календар
# (stoletie ( 'век', NoConvert)), # бр.дни= календар
# (hiliadoletie( 'хилядолетие', NoConvert)),# бр.дни= календар
]
)
)
if 10:
_time_conversions = [
( TCS.mikrosekunda, 1000000, ),
( TCS.sekunda, 60, ),
( TCS.minuta, 60, ),
( TCS.chas, 24, ),
( TCS.den, 7, ),
( TCS.sedmica, 2, ),
( TCS.sedmica2, None, ), # ами сега FIXME ко прайм тука
# ]
# _time_conversions2 = [
( TCS.mesec, 12, ),
# ( TCS.trimesecie, 2, ),
# ( TCS.polugodie, 2, ),
( TCS.godina, 100, ),
# ( TCS.stoletie, 1000,),
# ( TCS.hiliadoletie, 0,),
]
TCS.setup_chain( _time_conversions)
# TCS.setup_chain( _time_conversions2)
TCS.mesec._convert[ TCS.den] = 1 # FIXME
DELTA_METHODS = 'years months days hours minutes seconds microseconds'.split()
measure2deltamethod = {
TCS.mikrosekunda : 'microseconds',
TCS.sekunda : 'seconds',
TCS.minuta : 'minutes',
TCS.chas : 'hours',
TCS.den : 'days',
TCS.sedmica : 'weeks',
#TCS.sedmica2: 'weeks', XXX value * 2 ?!
TCS.mesec : 'months',
TCS.godina : 'years',
}
class RelativeDelta(object):
def __init__( me, *args, **kargs):
me._delta = relativedelta( *args, **kargs)
def copy(me):
return RelativeDelta( years=me.years, months=me.months, days=me.days,
hours=me.hours, minutes=me.minutes, seconds=me.seconds,
microseconds=me.microseconds
)
@staticmethod
def from_measure( measure, value=1):
kargs = {}
kargs[ measure2deltamethod[ measure ] ] = value
return RelativeDelta( **kargs)
@staticmethod
def reval(deltastr):
kargs = dict( zip( DELTA_METHODS,
map( int, deltastr.split(','))
)
)
return RelativeDelta( **kargs)
def __str__( me):
return str( me._delta)
def __repr__( me):
return ('%(years)02d,%(months)02d,%(days)02d,%(hours)02d,%(minutes)02d,%(seconds)02d,%(microseconds)02d' %
vars(me.normalize()._delta) )
def __getattr__( me, name):
#print 'looking for %s' % name
return getattr( me._delta, name)
def __setattr__( me, name, value):
if name not in DELTA_METHODS:
object.__setattr__( me, name, value)
return
try:
val = int( value)
except ValueError, e:
print e, 'wrong type of attribute %s - trying to set %s.' % ( name, value)
else:
setattr( me._delta, name, val)
__getstate__ = __repr__
def __setstate__(me, deltastr):
me._delta = RelativeDelta.reval(deltastr)._delta
@staticmethod
def from_relativedelta( rdelta):
return RelativeDelta( **dict( [(k, v) for (k, v) in vars(rdelta).iteritems() if not k.startswith('_') ] ))
def __add__( me, o):
if isinstance( o, RelativeDelta):
return me.from_relativedelta( o._delta + me._delta)
elif isinstance( o, (dt.datetime, dt.date)):
return UniversalTime( o + me._delta)
elif isinstance( o, int):
return me.from_relativedelta( me._delta + relativedelta( days=o))
else:
print o.__class__
raise Exception
return o + me
__radd__ = __add__
def __sub__(me, o):
if isinstance( o, RelativeDelta):
#return RelativeDelta.from_relativedelta( me._delta - o._delta)
return RelativeDelta.from_relativedelta( -(me._delta - o._delta)) # c00l. got it?
elif isinstance( o, relativedelta):
return -(me._delta - o)
raise AssertionError('tralala %s' % type(o))
def __rsub__(me, o):
if isinstance( o, dt.datetime):
return UniversalTime(o - me._delta)
raise AssertionError('tralala %s' % type(o))
def __cmp__( me, o):
#return cmp(me._delta, o._delta)
p = me.normalize( normalize_days = True)
q = o.normalize(normalize_days = True)
for attr in 'years months days hours minutes seconds microseconds'.split():
ret = cmp( getattr(p, attr), getattr(q, attr))
if ret:
return ret
return 0 #cmp(me._delta, o)
def __eq__( me, o):
return not bool( me.__cmp__(o))
def __nq__( me, o):
return not me.__eq__(o)
def normalize(me, copy=True, normalize_days=False):
r = copy and me.copy() or me
r._fix()
if normalize_days:
ot = UniversalTime.now()
do = ot + me
r = do - ot
if not copy:
me._delta = r._delta
return r
class UniversalTime( object):
_UIformat = '%Y-%m-%d' #will be overwritten under win32
def __init__( me, time ='', precision =None, context =None):
if isinstance( time, UniversalTime):
#XXX need to explicitly specify precision/context; else uses source ones
me.initial = time.initial
me.exact_time = time.exact_time
if precision is not None: me._round( precision)
context = context or time.context
else:
if isinstance( time, basestring):
me.initial = time
me.exact_time = me.fromString( time, context=context)
elif isinstance( time, dt.datetime):
me.initial = time.strftime('%Y%m%d%H%M%S')
me.exact_time = time
elif isinstance( time, dt.date):
me.initial = time.strftime('%Y%m%d')
me.exact_time = me.fromString( me.initial, context=context)
else:
raise AssertionError( str(time.__class__) + ' not supported')
if precision is None:
precision = TCS.mikrosekunda
me.context = context
return
me._round( precision )
me.context = context
@staticmethod
def beginOfUniverse():
return UniversalTime( dt.datetime( 1900, 1, 1))
begin = beginOfUniverse
@staticmethod
def endOfUniverse():
return UniversalTime( dt.datetime( 3000, 1, 1))
end = endOfUniverse
@staticmethod
def now():
return UniversalTime( _now())
@staticmethod
def fromString( timestr =None, context =None):
return parser.parse( timestr, default= context)
@staticmethod
def toString( time, timefmt =None):
''' Приема дата обект, връща стринг. Указва се формат( като стринг). '''
if not timefmt:
timefmt = UniversalTime._UIformat
timestr = time.strftime( timefmt)
return timestr
def to_string(me, timefmt):
return me.toString( me.exact_time, timefmt)
def convertTo( me, conversion_typ= dt.datetime, context =None): #XXX
pass
@staticmethod
def Round( dtime, precision):
prec_map = {
TCS.mikrosekunda: '%Y%m%d%H%M%S', #%f (for microseconds) is not supported before py2.6 :(
TCS.sekunda : '%Y%m%d%H%M%S',
TCS.minuta : '%Y%m%d%H%M',
TCS.chas : '%Y%m%d%H00', #00 is needed for the parser
TCS.den : '%Y%m%d',
TCS.mesec : '%Y%m',
TCS.godina : '%Y',
}
fmt = prec_map[precision]
new_dt = dt.datetime.strptime( dtime.strftime(fmt), fmt)
if precision == TCS.mikrosekunda: new_dt = new_dt.replace( microsecond = 999999) #until py 2.6
return new_dt
def round( me, precision):
return UniversalTime( me.Round( me.exact_time, precision))
def _round( me, precision):
#in place
#prec_map = {
# TCS.sekunda: '%Y%m%d%H%M%S',
# TCS.minuta : '%Y%m%d%H%M',
# TCS.chas : '%Y%m%d%H00', #00 is needed for the parser
# TCS.den : '%Y%m%d',
#}
#fmt = prec_map[precision]
#me.exact_time.strptime( dtime.strftime(fmt), fmt)
me.exact_time = me.Round( me.exact_time, precision)
def compare( me, other, precision=TCS.sekunda):
selftime = me.exact_time
if isinstance( other, dt.datetime):
othertime = other
elif isinstance( other, dt.date):
othertime = dt.datetime( other.year, other.month, other.day)
elif isinstance( other, basestring):
othertime = UniversalTime(other).exact_time
elif isinstance( other, UniversalTime):
othertime = other.exact_time
else:
raise NotImplementedError( 'can\'t compare UniversalTime to %s' % other.__class__)
return cmp ( selftime, othertime)
__cmp__ = compare
def __repr__(me):
return me.__class__.__name__ + '( ' + repr( me.exact_time) + ')' # eval()
return me.initial + ' -> ' + repr( me.exact_time )
def __str__(me):
return 'UT: ' + str(me.exact_time)
def __add__( me, o):
if isinstance( o, int):
o = RelativeDelta( days=o) #chefo's request
if isinstance( o, (dt.timedelta, RelativeDelta)):
return UniversalTime( me.exact_time + o)
raise TypeError( 'unsupported type[%s] for operation' % o.__class__)
__radd__ = __add__
def __sub__(me, other):
if isinstance( other, int):
other = RelativeDelta( days=other) #chefo's request
if isinstance( other, (dt.timedelta, RelativeDelta)):
return UniversalTime( me.exact_time - other)
return RelativeDelta( dt1=me.exact_time, dt2=UniversalTime(other).exact_time)
def __lt__( me, other): return me.compare( other) < 0
def __le__( me, other): return me.compare( other) <= 0
def __gt__( me, other): return me.compare( other) > 0
def __ge__( me, other): return me.compare( other) >= 0
def __eq__( me, other): return me.compare( other) == 0
def __ne__( me, other): return me.compare( other) != 0
#these fall into __getattr
#def timetuple( me): #datetime needs this when comparing from the right side
# return me.exact_time.timetuple()
def __hash__( me):
return hash( me.exact_time)
def __getattr__( me, name):
#print 'looking for %s' % name
return getattr( me.exact_time, name)
def UIstr( me):
return me.to_string( UniversalTime._UIformat)
def copy(me):
return UniversalTime(me.exact_time)
def beginOfMonth(me):
d = me.exact_time.replace(day = 1)
return UniversalTime( d)
def endOfMonth(me):
d = rrule(MONTHLY, count=1, bymonthday=[-1], dtstart=me.exact_time)[0]
return UniversalTime(d)
#17 седмици колко месеца са??? some conversion maybe - TODO
class Period( object):
'''представя затворен отляво, отворен отдясно интервал от време'''
class PeriodException( Exception): pass
def __init__( me, ot =None, do =None, size =None, precision =TCS.den, closed_interval =False):
''' ot, do, size - this can be created by any two of them.
any of this can be calendared or simple thus requiring context workcalendar or not.'''
me.precision = precision # to day, hour...must be considered by cmp, maybe by hash too???
#me.initial_ot = ot #ne znam dali ni triabwat
#me.initial_do = do
me.closed_interval = closed_interval
if size and (ot is do is None or ot and do):
raise me.PeriodException( 'valid ctors are (ot), (ot,do), (ot,size), (do), (do,size)')
if do is not None:
do = UniversalTime( do)
if closed_interval:
do += RelativeDelta.from_measure( me.precision)
if ot is not None:
ot = UniversalTime( ot)
if size is not None: do = ot + size
elif do is not None:
if size is not None: ot = do - size
if ot is None: ot = UniversalTime.beginOfUniverse()
if do is None: do = UniversalTime.endOfUniverse()
assert ot <= do, str(ot) + '<=' + str(do)
me.ot = ot
me.do = do
def copy( me):
return me.__class__( me.ot, me.do, precision=me.precision)
def size( me):
#return RelativeDelta( dt1=me.do.exact_time, dt2=me.ot.exact_time)
#return (me.do.exact_time - me.ot.exact_time).days
all = TCS.all()
idx_days = all.index( TCS.den)
idx = all.index( me.precision)
if idx > idx_days:
delta = (me.do - me.ot).normalize( normalize_days=True)
attr_name = measure2deltamethod[ me.precision]
return getattr( delta, attr_name)
delta = (me.do.exact_time - me.ot.exact_time)
if me.precision is TCS.mikrosekunda: # XXX workaround - int is too short for this
sekundi = TCS.den.convert( TCS.sekunda, delta, me.ot.exact_time).days
return sekundi * 1000 * 1000
return TCS.den.convert( me.precision, delta, me.ot.exact_time).days
def __cmp__(me, o):
r = cmp(me.ot, o.ot)
return r or cmp(me.do, o.do)
def __eq__( me, o):
#if not isinstance( o, me.__class__): return False
return not bool( me.__cmp__(o))
def __hash__( me):
return hash( (hash(me.ot), hash(me.do)))
def multiply( me, n):
'''returns 'n' consequent Periods of same size'''
res = [me]
for i in range( n-1):
res.append( Period( ot=res[-1].do, size=me.size()) )
return res
def __div__( me, n):
'''returns the Period splitted in 'n' Periods'''
res = []
size = me.size() / n
for i in range( n):
if not i:
res.append( Period( ot=me.ot, size=size, precision=me.precision))
else:
res.append( Period( ot=res[-1].do, size=size, precision=me.precision))
return res
def extend( me, n): #or expand?
'returns n-times longer Period'
return Period( ot= me.ot, size= n*me.size() )
def shrink( me,n): #or reduce,contract?
'returns n-times shorter Period'
return Period( ot=me.ot, size=me.size()/n )
def __add__( me, other):
retmap = { int : me.shift,
dt.timedelta: me.add_delta,
RelativeDelta: me.add_delta,
Period: me.add_interval
}
otype = type(other)
f = retmap.get( otype, None)
if f: return f( other)
raise me.PeriodException( '.add cannot take %s, needs one of %s' % (otype, retmap.keys() ) )
__mul__ = multiply
__rmul__ = __mul__
__radd__ = __add__
def add_days( me, other):
return me.add_delta( dt.timedelta( days=other) )
def add_month( me, nr=1):
return me.add_delta( RelativeDelta( months=nr) )
def add_year( me, nr=1):
return me.add_delta( RelativeDelta( years=nr) )
def add_delta( me, delta):
return Period( me.ot, me.do + delta)
def add_interval( me, other):
if me.overlap( other):
return PeriodCollection( [ Period( min( me.ot, other.ot), max( me.do, other.do)) ] )
else:
return PeriodCollection( [ me, other ] )
def __and__(me, other):
if me.overlap( other):
res = Period( max( me.ot, other.ot), min( me.do, other.do))
else:
res = None
return res
def shift( me, shift):
if isinstance( shift, int):
s = RelativeDelta( days=shift)
elif isinstance( shift, (dt.timedelta, RelativeDelta)):
s = shift
else:
assert 0, 'what is this: %s' % type(shift)
return Period( me.ot + s, me.do + s)
def __sub__( me, sub):
if isinstance( sub, int):
return me.shift( shift = -sub)
if isinstance( sub, (dt.timedelta, RelativeDelta)):
return Period( me.ot, me.do - sub)
if isinstance( sub, Period): #XXX ??? towa trebe li?
if me & sub:
if sub.ot > me.ot and me.do > sub.do:
# t1 t2 ## t1-t3 i t4-t2
# t3 t4 ##
return [
Period( me.ot, sub.ot),
#Period( sub.ot, sub.do),
Period( sub.do, me.do),
]
elif sub.ot > me.ot and me.do < sub.do:
# t1 t2 ## t1-t3
# t3 t4 ##
return [
Period( me.ot, sub.ot),
]
else:
#elif sub.ot <= me.ot and me.do > sub.do:
# # t1 t2
# # t3 t4
# return [
# Period( sub.do, me.do),
# ]
#elif sub.ot <= me.ot and me.do <= sub.do:
# t1 t2 ## t1-t2 ili nishto?!
# t3 t4 ##
return [ ]
else:
return [ me ] # ne se zastypwat, nishto ili nie?
raise me.PeriodException('need int, timedelta or Period received %s' % type( sub))
__rsub__ = __sub__
def overlap( me, other, include_to =True):
#ot = max( me.ot, other.ot)
#do = min( me.do, other.do)
#return ot < do
return ( me.containsDate( other.ot) or
me.containsDate( other.do, include_to=include_to) or
other.containsDate( me.ot) or
other.containsDate( me.do, include_to=include_to)
)
def containsDate( me, date, include_to =False):
z = include_to and (date <= me.do) or (date < me.do)
#z = (date <= me.do) if include_to else (date < me.do)
return ( date >= me.ot and z ) #FIXME включително края или не?
def containsInterval( me, interval, include_to=True):
#assert x.contains( x, include_to=True)
return me.containsDate( interval.ot) and me.containsDate( interval.do, include_to=include_to)
def contains( me, other, include_to =False):
if isinstance( other, (dt.datetime, UniversalTime)):
return me.containsDate( other, include_to =include_to)
elif isinstance( other, Period ):
return me.containsInterval( other)
raise me.PeriodException('need time or Period, received %s' % type( other))
def adjacentTo( me, other):
return me.do == other.ot # or me.ot == other.do #
def as_delta(me):
return me.do - me.ot
def __str__( me):
return '[ '+ ' : '.join( i.to_string("%Y-%m-%d %H:%M:%S") for i in [me.ot, me.do]) + ')'
#def __repr__( me):
# return me.__class__.__name__ + '(' + str(me) + ')'
def _get_ot(me):
return me._ot
def _get_do(me):
return me._do
def _set_ot(me, ot):
me._ot = UniversalTime(ot, precision=me.precision)
def _set_do(me, do):
me._do = UniversalTime(do, precision=me.precision)
ot = property( _get_ot, _set_ot)
do = property( _get_do, _set_do)
def split( me, precision=None, **kargs):
''' kargs should contain one of these keys:
freq, YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY
dtstart =None, date_start
interval =1, freq+=interval iteration
wkst =None, MO, TU, WE, TH, FR, SA, SU - week start day
count =None, how many results to be generated, not needed if until is set
until =None, date_end
bysetpos =None,
bymonth =None, (1,12), (1, 2..)
bymonthday =None, == || == za dni na mesec, podyrzha negativni indeksi
byyearday =None,
byeaster =None,
byweekno =None,
byweekday =None, MO, TU, WE, TH, FR, SA, SU
byhour =None, 00-24
byminute =None, 00-60
bysecond =None, 00-60
cache =False XXX
'''
precision = precision or me.precision
freq = kargs.pop( 'freq', MONTHLY)
dtstart = kargs.pop( 'dtstart', me.ot)
until = kargs.pop( 'until', me.do)
rule = rrule( freq= freq, dtstart= dtstart.exact_time, until= until.exact_time, **kargs)
days = sorted( set( rule) | set([ me.ot.exact_time, me.do.exact_time]))
periods = [ Period( prev,next, precision=precision)
for prev,next in neighbours( days)
]
return PeriodCollection( periods)
def __getitem__( me, i):
raise NotImplementedError, 'Use ot, do'
if i not in (0,1,-1): raise IndexError,i #за да работи a,b=period
if i: return me.do
return me.ot
@staticmethod
def from_date( date, measure, move_to_logical_start=True, from_given_date=False):
r = {
TCS.den : Period.day_from_date, # calc.context.Scaled uses it
TCS.sedmica : Period.week_from_date,
TCS.mesec : Period.month_from_date,
TCS.godina : Period.year_from_date,
}
period = r[measure](date, move_to_logical_start or from_given_date)
if from_given_date:
period.ot = date
return period
@staticmethod
def day_from_date(date, *a, **ka):
return Period( date, size=RelativeDelta(days=1))
@staticmethod
def week_from_date(date, move_to_logical_start=True):
t = UniversalTime(date)
delta = RelativeDelta(weeks=1)
if move_to_logical_start:
start_date = rrule(WEEKLY, dtstart=t.exact_time, byweekday=MO)[0]
if start_date > t: #next monday
start_date -= delta
else:
start_date = t
return Period( start_date, size=delta)
@staticmethod
def month_from_date(date, move_to_logical_start=True):
t = UniversalTime( date)
start_date = dt.datetime( t.year, t.month, move_to_logical_start and 1 or t.day)
return Period( start_date, start_date + RelativeDelta( months=1))
@staticmethod
def year_from_date(date, move_to_logical_start=True):
t = UniversalTime( date)
month = (t.month, 1)[ move_to_logical_start]
day = (t.day, 1)[ move_to_logical_start]
start_date = dt.datetime( t.year, month, day)
return Period( start_date, start_date + RelativeDelta( years=1))
class VremeContext( object):
'''stub only till now''' #TODO de-stub-bing
def __init__( me, kalendar =None):
me.kalendarche = kalendar
def year( me):
return _now().year
def month( me):
return 5 #_now().month
def as_universal_time( me ):
return UniversalTime( ma.as_datetime())
def as_datetime( me):
return
def get_context( context):
'тука ще има ракети и времомашини' #TODO
return context
class PeriodCollection( object):
''' това отделен клас ли е или Period да свърши и това?''' # XXX
''' тук явно влизат и колекция от UniversalTime (единични времеви обекти),
представени като интервал [ot, ot]
'''
def __init__( me, timeIntervalList):
me.collection = list( timeIntervalList)
def __add__( me, timeIntervalList):
if isinstance( timeIntervalList, PeriodCollection):
timeIntervalList = timeIntervalList.collection
assert isinstance( timeIntervalList, list)
col = me.collection + timeIntervalList
return PeriodCollection( col)
# има същия интерфейс като Period + обединение
def __str__( me):
s = ''
for i in me.collection:
s += str(i) + '\n'
return s
def size( me, context =None):
r = 0
for p in me.collection:
r += p.size()
return r
def as_delta(me):
r = RelativeDelta()
for p in me.collection:
r += p.as_delta()
return r
def sort(me):
me.collection.sort()
def simplify( me, collection=None):
''' обединяване на съседни или застъпващи се интервали ако може'''
if collection is None:
col = me.collection
else:
col = collection
col.sort()
ncol = []
for next in col: #prowerka za sysedni interwali
if ncol:
prev = ncol[-1]
if prev.adjacentTo( next):
ncol[-1] = (prev + next)[0]
continue
ncol.append( next)
ncol.sort()
ncol2 = []
for next in ncol: #prowerka za wlojeni interwali
if ncol2:
prev = ncol2[-1]
res = prev + next
if res.size() == 1 and res[0] == prev:
continue
ncol2.append( next)
if len(col) == len(ncol2): #ne sa nastypili promeni
return PeriodCollection( ncol2)
else:
return me.simplify( ncol2) #puskame pak cialata shema
def __getitem__( me, idx):
return me.collection[ idx ]
def __iter__( me):
return iter(me.collection)
def intersect( me, other, context =None):
pass #????
def razcepvane( me, kude):
pass
def sa_susedni( me, other):
pass
def union( me, other, context =None):
pass
# TODO зацепване, разцепване, прицепване на обединение на интервали (съседни, застъпващи се, без сечение) по различни стратегии за обединени (5-6 вида)
# TODO всички аритметики с число, момент във времето, интервал от време
#specific query methods - needed often
#TODO първия работен ден >= 5то число на месеца - как се задава???
def _karamboliraiSKalendar( me, nth_day_of_month, context, isBefore =True):
'съвсем примерна имплементация'
context = get_context( context) #stub
if isBefore:
first_day = 1
last_day = nth_day_of_month
else:
first_day = nth_day_of_month
last_day = 32
last_day_of_month = rrule( MONTHLY,
dtstart= dt.datetime( context.year(), context.month(), 1),
bymonthday= (-1),
count= 1,
) [0].day
last_day_by_month = min( last_day_of_month, last_day)
ruleset = rruleset()
r = rrule( MONTHLY,
bymonthday= range( first_day, last_day),
dtstart= dt.datetime( context.year(), context.month(), first_day),
until= dt.datetime( context.year(), context.month(), last_day_by_month),
)
ruleset.rrule( r)
for neraboten in context.kalendarche.holidays():
ruleset.exdate( neraboten)
return ruleset
# трябва да има и първия и последния работен ден след/преди дата XXX
def firstWorkingDayAfter( me, nth_day_of_month, context =None):
l = list( me._karamboliraiSKalendar( nth_day_of_month, context, isBefore = False))
return min( l)
def lastWorkingDayBefore( me, nth_day_of_month, context =None): #last_day is 32 ;)
l = list( me._karamboliraiSKalendar( nth_day_of_month, context, isBefore = True))
return max( l)
# когато ти трябва интервал от шест месеца назад (интервали назад) по кой
# работен календар се смята (към момента на поискване или някакъв друг - двубременност)
if 0:
class WorkGraphic(object):
def __init__( me, kalendar):
me.raboten_kalendar = kalendar
me.working_intervals = []
class RecurrentPeriod( Period):
#oshte kolko chesto se povtaria
def __init__( me, ot, do, chestota= None, precision= None):
Period.__init__( me, ot, do, precision)
me.chestota = chestota
class _CalendarDate(object):
def __init__(me, date, isholiday, comment=''):
me.date = date
me.isholiday = isholiday
me.comment = comment
def __hash__(me): return hash(me.date)
def __eq__(me, o):
if isinstance( o, dt.datetime):
return me.date == o
return me.date == o.date
def __cmp__(me, o):
assert isinstance(o, me.__class__)
return cmp(me.date, o.date)
def __str__(me): return 'D:' + str(me.date) + ' H:' + str(me.isholiday) + ' C:' + str(bool(me.comment)) + '@'+str(id(me))
class _CalendarDate2(_CalendarDate):
_overwrites = None
#_special = True
class WorkCalendar( BasicCalendar):