|
| 1 | +/** |
| 2 | + * @file js2py/datetime.simple.failing |
| 3 | + * Test which tests sending js Date() objects into Python get the right types and values |
| 4 | + * |
| 5 | + * @author Wes Garland, [email protected] |
| 6 | + * @date July 2023 |
| 7 | + */ |
| 8 | +'use strict' |
| 9 | + |
| 10 | +var fail = false; |
| 11 | +function test(result, name) |
| 12 | +{ |
| 13 | + if (result) |
| 14 | + console.log('pass -', name); |
| 15 | + else |
| 16 | + { |
| 17 | + console.log('fail -', name); |
| 18 | + fail = true; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +python.exec('import datetime'); |
| 23 | +const pyTypeOf = python.eval('(lambda x: str(type(x)))'); |
| 24 | + |
| 25 | +const ct = pyTypeOf(new Date()); |
| 26 | +if (ct.indexOf('datetime.datetime') !== -1) |
| 27 | + console.log('type check pass - converted type was', ct); |
| 28 | +else |
| 29 | +{ |
| 30 | + fail = true; |
| 31 | + console.error('type check fail - converted type was', ct); |
| 32 | +} |
| 33 | + |
| 34 | +python.exec(` |
| 35 | +def eq(date1, date2): |
| 36 | + print('') |
| 37 | + print('compare', date1, date2); |
| 38 | + if (date1 == date2): |
| 39 | + return True |
| 40 | + if (type(date1) != type(date2)): |
| 41 | + #print("types do not match") |
| 42 | + return False |
| 43 | + |
| 44 | + diff = date2 - date1 |
| 45 | + if (diff): |
| 46 | + print(f'Dates do not match, difference is {diff}') |
| 47 | + return False |
| 48 | + |
| 49 | + print('warning - dates are different but equal', diff) |
| 50 | + return True |
| 51 | + |
| 52 | +def eqUnixTime(date, timet): |
| 53 | + import datetime |
| 54 | + global eq |
| 55 | + return eq(date, datetime.datetime.utcfromtimestamp(timet)) |
| 56 | +`); |
| 57 | + |
| 58 | +const eq = python.eval('eq'); |
| 59 | +const eqUnixTime = python.eval('eqUnixTime'); |
| 60 | + |
| 61 | +const now = new Date(); |
| 62 | +const randomDate = new Date(Date.UTC(1973, 8, 16, 23, 2, 30)); |
| 63 | +const startOfEpoch = new Date(Date.UTC(1970, 0, 0, 0, 0, 0)); |
| 64 | + |
| 65 | +test(eq(now, now), 'same dates (now) are equal'); |
| 66 | +test(eq(startOfEpoch, startOfEpoch), 'same dates (0) are equal'); |
| 67 | +test(!eq(startOfEpoch, now), 'different dates are not equal 1'); |
| 68 | +test(!eq(startOfEpoch, randomDate), 'different dates are not equal 2'); |
| 69 | +test(!eq(randomDate, now), 'different dates are not equal 3'); |
| 70 | +test(eqUnixTime(startOfEpoch, 0), 'start of epoch'); |
| 71 | + |
| 72 | +python.exit(fail ? 2 : 0); |
0 commit comments