-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
date.py
826 lines (634 loc) 路 20.3 KB
/
date.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
"""Date Functions.
The following functions give you access to test and modify dates.
"""
from __future__ import print_function
__all__ = [
"addDays",
"addHours",
"addMillis",
"addMinutes",
"addMonths",
"addSeconds",
"addWeeks",
"addYears",
"daysBetween",
"format",
"fromMillis",
"getAMorPM",
"getDate",
"getDayOfMonth",
"getDayOfWeek",
"getDayOfYear",
"getHour12",
"getHour24",
"getMillis",
"getMinute",
"getMonth",
"getQuarter",
"getSecond",
"getTimezone",
"getTimezoneOffset",
"getTimezoneRawOffset",
"getYear",
"hoursBetween",
"isAfter",
"isBefore",
"isBetween",
"isDaylightTime",
"midnight",
"millisBetween",
"minutesBetween",
"monthsBetween",
"now",
"parse",
"secondsBetween",
"setTime",
"toMillis",
"weeksBetween",
"yearsBetween",
]
from datetime import datetime
from time import localtime, mktime
from typing import Optional, Union
from java.util import Date, Locale
String = Union[str, unicode]
def _now():
# type: () -> datetime
"""Get current datetime.
Returns:
Now.
"""
return datetime.now()
def addDays(date, value):
# type: (Date, int) -> Date
"""Add or subtract an amount of days to a given date and time.
Args:
date: The starting date.
value: The number of units to add, or subtract if the value is
negative.
Returns:
A new date object offset by the integer passed to the function.
"""
print(date, value)
return Date()
def addHours(date, value):
# type: (Date, int) -> Date
"""Add or subtract an amount of hours to a given date and time.
Args:
date: The starting date.
value: The number of units to add, or subtract if the value is
negative.
Returns:
A new date object offset by the integer passed to the function.
"""
print(date, value)
return Date()
def addMillis(date, value):
# type: (Date, int) -> Date
"""Add or subtract an amount of milliseconds to a given date and
time.
Args:
date: The starting date.
value: The number of units to add, or subtract if the value is
negative.
Returns:
A new date object offset by the integer passed to the function.
"""
print(date, value)
return Date()
def addMinutes(date, value):
# type: (Date, int) -> Date
"""Add or subtract an amount of minutes to a given date and time.
Args:
date: The starting date.
value: The number of units to add, or subtract if the value is
negative.
Returns:
A new date object offset by the integer passed to the function.
"""
print(date, value)
return Date()
def addMonths(date, value):
# type: (Date, int) -> Date
"""Add or subtract an amount of months to a given date and time.
This function is unique since each month can have a variable number
of days. For example, if the date passed in is March 31st, and we
add one month, April does not have a 31st day, so the returned date
will be the proper number of months rounded down to the closest
available day, in this case April 30th.
Args:
date: The starting date.
value: The number of units to add, or subtract if the value is
negative.
Returns:
A new date object offset by the integer passed to the function.
"""
print(date, value)
return Date()
def addSeconds(date, value):
# type: (Date, int) -> Date
"""Add or subtract an amount of seconds to a given date and time.
Args:
date: The starting date.
value: The number of units to add, or subtract if the value is
negative.
Returns:
A new date object offset by the integer passed to the function.
"""
print(date, value)
return Date()
def addWeeks(date, value):
# type: (Date, int) -> Date
"""Add or subtract an amount of weeks to a given date and time.
Args:
date: The starting date.
value: The number of units to add, or subtract if the value is
negative.
Returns:
A new date object offset by the integer passed to the function.
"""
print(date, value)
return Date()
def addYears(date, value):
# type: (Date, int) -> Date
"""Add or subtract an amount of years to a given date and time.
Args:
date: The starting date.
value: The number of units to add, or subtract if the value is
negative.
Returns:
A new date object offset by the integer passed to the function.
"""
print(date, value)
return Date()
def daysBetween(date_1, date_2):
# type: (Date, Date) -> int
"""Calculates the number of whole days between two dates.
Daylight Saving Time changes are taken into account.
Args:
date_1: The first date to use.
date_2: The second date to use.
Returns:
An integer that is representative of the difference between two
dates.
"""
print(date_1, date_2)
return 1
def format(date, format="yyyy-MM-dd HH:mm:ss"):
# type: (Date, String) -> unicode
"""Returns the given date as a string, formatted according to a
pattern.
The pattern is a format that is full of various placeholders that
display different parts of the date. These are case-sensitive. These
placeholders can be repeated for a different effect. For example, M
will give you 1-12, MM will give you 01-12, MMM will give you
Jan-Dec, MMMM will give you January-December.
Note:
The functions above are based off the the Java class
`SimpleDateFormat` internally. The functions will accept any
valid format string for that class.
Args:
date: The date to format.
format: A format string such as "yyyy-MM-dd HH:mm:ss". The
format argument is optional. The default is
"yyyy-MM-dd HH:mm:ss". Optional.
Returns:
A string representing the formatted datetime.
"""
print(date, format)
_date = _now()
_format = format
_format = _format.replace("a", "%p")
_format = _format.replace("D", "%j")
_format = _format.replace("dd", "{:02}".format(_date.day))
_format = _format.replace("d", "{}".format(_date.day))
_format = _format.replace("EEEE", "%A")
_format = _format.replace("E", "%a")
_format = _format.replace("F", "{}".format(_date.isocalendar()[2]))
_format = _format.replace("G", "AD")
_format = _format.replace("hh", "%I")
_format = _format.replace("h", "%-I")
_format = _format.replace("HH", "{:02}".format(_date.hour))
_format = _format.replace("H", "%-H")
_format = _format.replace("k", "%H")
_format = _format.replace("K", "%-I")
_format = _format.replace("MMMM", "%B")
_format = _format.replace("MMM", "%b")
_format = _format.replace("MM", "{:02}".format(_date.month))
_format = _format.replace("M", "{}".format(_date.month))
_format = _format.replace("mm", "%M")
_format = _format.replace("m", "%-M")
_millis = _date.microsecond % 1000
_format = _format.replace("SSS", "{:03}".format(_millis))
_format = _format.replace("SS", "{:02}".format(_millis))
_format = _format.replace("S", "{:01}".format(_millis))
_format = _format.replace("ss", "%S")
_format = _format.replace("s", "%-S")
_format = _format.replace("w", "{}".format(_date.isocalendar()[1] + 1))
_format = _format.replace("u", "%w")
_format = _format.replace(
"W",
"{}".format(_date.isocalendar()[1] - _date.replace(day=1).isocalendar()[1] + 1),
)
_format = _format.replace("XXX", "{:03.0f}:00".format(getTimezoneOffset()))
_format = _format.replace("XX", "{:03.0f}00".format(getTimezoneOffset()))
_format = _format.replace("X", "{:03.0f}".format(getTimezoneOffset()))
_format = _format.replace("yyyy", "{}".format(_date.year))
_format = _format.replace("yy", str(_date.year)[-2:])
_format = _format.replace("Y", "%Y")
_format = _format.replace("y", "%Y")
_format = _format.replace("Z", "{:03.0f}00".format(getTimezoneOffset()))
_format = _format.replace("z", "PDT")
return unicode(_date.strftime(_format))
def fromMillis(millis):
# type: (int) -> Date
"""Creates a date object given a millisecond value.
Args:
millis: The number of milliseconds elapsed since January 1,
1970, 00:00:00 UTC (GMT).
Returns:
A new date object.
"""
return Date(millis)
def getAMorPM(date):
# type: (Date) -> int
"""Returns a 0 if the time is before noon, and a 1 if the time is
after noon.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return 1 if _now().hour >= 12 else 0
def getDate(year, month, day):
# type: (int, int, int) -> Date
"""Creates a new Date object given a year, month and a day.
The time will be set to midnight of that day.
Args:
year: The year for the new date.
month: The month of the new date. January is month 0.
day: The day of the month for the new date. The first day
of the month is day 1.
Returns:
A new date, set to midnight of that day.
"""
print(year, month, day)
return Date()
def getDayOfMonth(date):
# type: (Date) -> int
"""Extracts the day of the month from a date.
The first day of the month is day 1.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().day
def getDayOfWeek(date):
# type: (Date) -> int
"""Extracts the day of the week from a date.
Sunday is day 1, Saturday is day 7.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
_dow = [2, 3, 4, 5, 6, 7, 1]
return _dow[_now().weekday()]
def getDayOfYear(date):
# type: (Date) -> int
"""Extracts the day of the year from a date.
The first day of the year is day 1.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().timetuple().tm_yday
def getHour12(date):
# type: (Date) -> int
"""Extracts the hour from a date.
Uses a 12 hour clock, so noon and midnight are returned as 0.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().hour - 12 if _now().hour >= 12 else _now().hour
def getHour24(date):
# type: (Date) -> int
"""Extracts the hour from a date.
Uses a 24 hour clock, so midnight is zero.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().hour
def getMillis(date):
# type: (Date) -> int
"""Extracts the milliseconds from a date, ranging from 0-999.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().microsecond // 1000
def getMinute(date):
# type: (Date) -> int
"""Extracts the minutes from a date, ranging from 0-59.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().minute
def getMonth(date):
# type: (Date) -> int
"""Extracts the month from a date, where January is month 0.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().month
def getQuarter(date):
# type: (Date) -> int
"""Extracts the quarter from a date, ranging from 1-4.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return ((_now().month - 1) // 3) + 1
def getSecond(date):
# type: (Date) -> int
"""Extracts the seconds from a date, ranging from 0-59.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().second
def getTimezone():
# type: () -> String
"""Returns the ID of the current timezone.
Returns:
str: A representation of the current timezone.
"""
return "America/Tijuana"
def getTimezoneOffset(date=None):
# type: (Optional[Date]) -> float
"""Returns the current timezone's offset versus UTC for a given
instant, taking Daylight Saving Time into account.
Args:
date: The instant in time for which to calculate the offset.
Uses now() if omitted. Optional.
Returns:
The timezone offset compared to UTC, in hours.
"""
return -7.0 if isDaylightTime(date) else -8.0
def getTimezoneRawOffset():
# type: () -> float
"""Returns the current timezone offset versus UTC, not taking
Daylight Saving Time into account.
Returns:
The timezone offset.
"""
return float(0)
def getYear(date):
# type: (Date) -> int
"""Extracts the year from a date.
Args:
date: The date to use.
Returns:
An integer that is representative of the extracted value.
"""
print(date)
return _now().year
def hoursBetween(date_1, date_2):
# type: (Date, Date) -> int
"""Calculates the number of whole hours between two dates.
Args:
date_1: The first date to use.
date_2: The second date to use.
Returns:
An integer that is representative of the difference between two
dates.
"""
print(date_1, date_2)
return 1
def isAfter(date_1, date_2):
# type: (Date, Date) -> bool
"""Compares two dates to see if date_1 is after date_2.
Args:
date_1: The first date.
date_2: The second date.
Returns:
True if date_1 is after date_2, False otherwise.
"""
print(date_1, date_2)
return True
def isBefore(date_1, date_2):
# type: (Date, Date) -> bool
"""Compares to dates to see if date_1 is before date_2.
Args:
date_1: The first date.
date_2: The second date.
Returns:
True if date_1 is before date_2, False otherwise.
"""
print(date_1, date_2)
return False
def isBetween(target_date, start_date, end_date):
# type: (Date, Date, Date) -> bool
"""Compares two dates to see if a target date is between two other
dates.
Args:
target_date: The date to compare.
start_date: The start of a date range.
end_date: The end of a date range. This date must be after the
start date.
Returns:
True if target_date is >= start_date and target_date <=
end_date, False otherwise.
"""
print(target_date, start_date, end_date)
return True
def isDaylightTime(date=None):
# type: (Optional[Date]) -> bool
"""Checks to see if the current timezone is using Daylight Saving
Time during the date specified.
Args:
date: The date you want to check if the current timezone is
observing Daylight Saving Time. Uses now() if omitted.
Optional.
Returns:
True if date is observing Daylight Saving Time in the
current timezone, False otherwise.
"""
print(date)
_date = _now()
time_tuple = (
_date.year,
_date.month,
_date.day,
_date.hour,
_date.minute,
_date.second,
_date.weekday(),
0,
0,
)
stamp = mktime(time_tuple)
time_tuple = localtime(stamp)
return time_tuple.tm_isdst > 0
def midnight(date):
# type: (Date) -> Date
"""Returns a copy of a date with the hour, minute, second, and
millisecond fields set to zero.
Args:
date: The starting date.
Returns:
A new date, set to midnight of the day provided.
"""
print(date)
return Date()
def millisBetween(date_1, date_2):
# type: (Date, Date) -> long
"""Calculates the number of whole milliseconds between two dates.
Args:
date_1: The first date to use.
date_2: The second date to use.
Returns:
An integer that is representative of the difference between two
dates.
"""
print(date_2, date_1)
return long(1)
def minutesBetween(date_1, date_2):
# type: (Date, Date) -> int
"""Calculates the number of whole minutes between two dates.
Args:
date_1: The first date to use.
date_2: The second date to use.
Returns:
An integer that is representative of the difference between two
dates.
"""
print(date_2, date_1)
return 1
def monthsBetween(date_1, date_2):
# type: (Date, Date) -> int
"""Calculates the number of whole months between two dates.
Daylight Saving Time changes are taken into account.
Args:
date_1: The first date to use.
date_2: The second date to use.
Returns:
An integer that is representative of the difference between two
dates.
"""
print(date_1, date_2)
return 0
def now():
# type: () -> Date
"""Returns a java.util.Date object that represents the current time
according to the local system clock.
Returns:
A new date, set to the current date and time.
"""
return Date()
def parse(dateString, formatString="yyyy-MM-dd HH:mm:ss", locale=Locale.ENGLISH):
# type: (String, Optional[String], Optional[Locale]) -> Date
"""Attempts to parse a string and create a Date.
Causes ParseException if the date dateString parameter is in an
unrecognized format.
Args:
dateString: The string to parse into a date.
formatString: Format string used by the parser. Default is
"yyyy-MM-dd HH:mm:ss". Optional.
locale: Locale used for parsing. Can be the locale name
such as 'fr', or the Java Locale such as 'Locale.FRENCH'.
Default is 'Locale.ENGLISH'. Optional.
Returns:
The parsed date.
"""
print(dateString, formatString, locale)
return Date()
def secondsBetween(date_1, date_2):
# type: (Date, Date) -> int
"""Calculates the number of whole seconds between two dates.
Args:
date_1: The first date to use.
date_2: The second date to use.
Returns:
An integer that is representative of the difference between two
dates.
"""
print(date_2, date_1)
return 0
def setTime(date, hour, minute, second):
# type: (Date, int, int, int) -> Date
"""Takes in a date, and returns a copy of it with the time fields
set as specified.
Args:
date: The starting date.
hour: The hours (0-23) to set.
minute: The minutes (0-59) to set.
second: The seconds (0-59) to set.
Returns:
A new date, set to the appropriate time.
"""
print(date, hour, minute, second)
return Date()
def toMillis(date):
# type: (Date) -> long
"""Converts a Date object to its millisecond value elapsed since
January 1, 1970, 00:00:00 UTC (GMT).
Args:
date: The date object to convert.
Returns:
An 8-byte integer representing the number of millisecond elapsed
since January 1, 1970, 00:00:00 UTC (GMT).
"""
print(date)
_date = _now()
millis = mktime(_date.timetuple()) * 1000 + _date.microsecond // 1000
return long(millis)
def weeksBetween(date_1, date_2):
# type: (Date, Date) -> int
"""Calculates the number of whole weeks between two dates.
Args:
date_1: The first date to use.
date_2: The second date to use.
Returns:
An integer that is representative of the difference between two
dates.
"""
print(date_1, date_2)
return 0
def yearsBetween(date_1, date_2):
# type: (Date, Date) -> int
"""Calculates the number of whole years between two dates.
Daylight Saving Time changes are taken into account.
Args:
date_1: The first date to use.
date_2: The second date to use.
Returns:
An integer that is representative of the difference between two
dates.
"""
print(date_1, date_2)
return 0