Support Board
Date/Time: Sat, 23 Nov 2024 17:40:11 +0000
Post From: Python for Sierra Chart
[2021-06-19 20:15:37] |
biconoid - Posts: 5 |
Hi User907968, Thank you very much for your helpful and precise pointers. As a result i've tried to approach the problem below. Now quite close the output. - Date Part is working fine for all values provided from SCID. - Time part still is off by a little. Could you please check where could be the issue? """
------------------------------The DateTime member variable is a SCDateTimeMS variable. This is a 64-bit integer. It represents the number of microseconds since the SCDateTime epoch of December 30, 1899.""" import numpy as np def calculate_time_from(scid_date_time, zone=0): # Constant values sec_per_min, min_per_hour, hours_per_day = (60, 60, 24) sec_per_hour = sec_per_min * min_per_hour # 3600 sec_per_day = sec_per_hour * hours_per_day # 86400 u_sec_per_day = sec_per_day * 1000_000 # 86400_000_000 excel_epoch_time = np.datetime64('1899-12-30') # excel epoch date scid_date_time = int(scid_date_time) # d[0] value from struct.unpack('<q 4f 4I') time_zone = int(zone * min_per_hour) # UTC value, e.g +5, # Components to get full time time_elapsed = scid_date_time / u_sec_per_day # total time elapsed since Dec 30, 1899 date_part, time_part = str(time_elapsed).split('.') # split integer(date) and fraction(time) parts # Calculate delta in numpy_values delta_days = np.timedelta64(date_part, 'D') # timedelta in days for date_part delta_us = np.timedelta64(time_part, 'us') # timedelta in micro seconds for time_part delta_timezone = np.timedelta64(time_zone, 'm') # timedelta in minutes for time_zone my_time = excel_epoch_time + (delta_days + delta_us + delta_timezone) # final excel datetime value date_val, time_val = str(my_time).split('T') # individual date and time values return date_val, time_val # print(calculate_time_from(3795467579000000.0, -0.5)) # Output: ('2020-04-08', '00:08:10.509259') # print(calculate_time_from(3795467579000000.0, 0)) # Output: ('2020-04-08', '00:38:10.509259') # print(calculate_time_from(3795467579000000.0, 0.5)) # Output: ('2020-04-08', '01:08:10.509259') # print(f'Time: {my_time}') # Time: 2020-04-08T00:38:10.509259 # print(f'scid_date: {scid_date_time}') # scid_date: 3795467579000000 # print(f'time_elapsed: {time_elapsed}') # time_elapsed: 43929.02290509259 # print(f'date_part: {date_part}') # date_part: 43929 # print(f'time_part: {time_part}') # time_part: 02290509259 # print(f'np_units: {np.timedelta64(date_part)}') # np_units: 43929 generic time units # print(f'np_units: {np.timedelta64(time_part)}') # np_units: 2290509259 generic time units |