text
stringlengths 0
601k
|
---|
let ( ) = add " t " ( fun ( ) -> let s1 = " 2005 - 05 - 25 12 : 46 - 4 : 00 " in let s2 = " 2005 - 05 - 25 12 : 46 : 15 - 4 : 00 " in let s3 = " 2005 - 05 - 25 12 : 46 : 15 . 232 - 4 : 00 " in let s4 = " 2005 - 05 - 25 12 : 46 : 15 . 232338 - 4 : 00 " in let time1 = Time . of_string s1 in let time2 = Time . of_string s2 in let time3 = Time . of_string s3 in let time4 = Time . of_string s4 in let now1 = Time . now ( ) in let now2 = Time . now ( ) in " diff1 " @? ( Float . iround_exn ~ dir ` : Nearest ( Time . Span . to_sec ( Time . diff time2 time1 ) ) = 15 ) ; " diff1 ' " @? ( Float . iround_exn ~ dir ` : Nearest ( Time . Span . to_ms ( Time . diff time2 time1 ) ) = 15 * 1000 ) ; " diff2 " @? ( Float . iround_exn ~ dir ` : Nearest ( Time . Span . to_ms ( Time . diff time3 time2 ) ) = 232 ) ; " diff3 " @? ( Float . iround_exn ~ dir ` : Nearest ( Time . Span . to_us ( Time . diff time4 time3 ) ) = 338 ) ; " ord " @? ( now2 >= now1 ) ; " sexp1 " @? ( Time . t_of_sexp ( Time . sexp_of_t time1 ) = time1 ) ; " sexp2 " @? ( Time . t_of_sexp ( Time . sexp_of_t time2 ) = time2 ) ; " sexp3 " @? ( Time . t_of_sexp ( Time . sexp_of_t time3 ) = time3 ) ; let zone = Time . Zone . find_exn " America / New_York " in let date , ofday = Time . to_date_ofday time3 ~ zone in " date " @? ( date = Date . of_string " 2005 - 05 - 25 " ) ; " ofday " @? ( Ofday . ( . ) = ofday ( Time . Ofday . of_string " 12 : 46 : 15 . 232 " ) ) ; " ofday1 " @? ( Time . Ofday . of_string " 09 : 13 " = Time . Ofday . of_string " 0913 " ) ; " add1 " @? teq ( Time . add time1 ( sec 15 . ) ) time2 ; " add2 " @? teq ( Time . add time2 ( Time . Span . of_ms 232 . ) ) time3 ; )
|
let ( ) = add " Ofday_string_conversion " ( fun ( ) -> let rand_state = Random . State . make [ | 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ] | in for _ = 0 to 100_000 do let secs = Random . State . int rand_state 86_400_000 in let ofday = Ofday . of_span_since_start_of_day_exn ( Time . Span . of_ms ( float secs ) ) in let ofday_string = Ofday . to_string ofday in let ofday ' = Ofday . of_string ofday_string in if Ofday . ( . ) <> ofday ofday ' then failwithf " ( % d seconds ) % s ( . % 20f ) <> Ofday . of_string % s ( . % 20f ) " secs ofday_string ( Ofday . to_span_since_start_of_day ofday |> Time . Span . to_sec ) ( Ofday . to_string ofday ' ) ( Ofday . to_span_since_start_of_day ofday ' |> Time . Span . to_sec ) ( ) ; let ofday ' = Ofday . of_string ofday_string in if Ofday . ( . ) <> ofday ofday ' then failwithf " % s <> Ofday . of_string % s " ofday_string ( Ofday . to_string ofday ' ) ( ) ; done )
|
let ( ) = add " date " ( fun ( ) -> let zone = ( force Time . Zone . local ) in let start = Time . of_date_ofday ~ zone ( Date . create_exn ~ y : 1999 ~ m : Month . Jan ~ d : 1 ) Ofday . start_of_day in let day = Span . of_day 1 . in let number_of_days = match Word_size . word_size with | W64 -> 100_000 | W32 -> 365 * 39 in for i = 0 to number_of_days do let date = Time . to_date ~ zone ( Time . add start ( Time . Span . scale day ( float i ) ) ) in let date_string = Date . to_string date in let date ' = Date . of_string date_string in if Date . ( ) <> date date ' then failwithf " % s <> Date . of_string % s " date_string ( Date . to_string date ' ) ( ) ; done )
|
module Old_date_impl = struct let of_tm tm = Date . create_exn ~ y ( : tm . Unix . tm_year + 1900 ) ~ m ( : Month . of_int_exn ( tm . Unix . tm_mon + 1 ) ) ~ d : tm . Unix . tm_mday ; ; let to_tm t = { Unix . tm_sec = 0 ; tm_min = 0 ; tm_hour = 12 ; tm_mday = Date . day t ; tm_mon = Month . to_int ( Date . month t ) - 1 ; tm_year = Date . year t - 1900 ; tm_wday = 0 ; tm_yday = 0 ; tm_isdst = false ; } ; ; let to_time_internal t = let tm_date = to_tm t in let time = fst ( Unix . mktime tm_date ) in Time . of_span_since_epoch ( Time . Span . of_sec time ) ; ; let of_time_internal time = of_tm ( Unix . localtime ( Float . round ~ dir ` : Down ( Time . to_span_since_epoch time |> Time . Span . to_sec ) ) ) let add_days t n = let time = to_time_internal t in of_time_internal ( Time . add time ( Span . of_day ( Float . of_int n ) ) ) ; ; let day_of_week t = let uday = to_tm t in let sec , _ = Unix . mktime uday in let unix_wday = ( Unix . localtime sec ) . Unix . tm_wday in Day_of_week . of_int_exn unix_wday ; ; let exhaustive_date_range = if Sys . c_int_size ( ) < 64 then Date . create_exn ~ y : 1970 ~ m : Month . Jan ~ d : 1 , 365 * 68 else Date . create_exn ~ y : 1900 ~ m : Month . Jan ~ d : 1 , 365 * 100 let exhaustive_day_of_week_test ( ) = let start_date , ndays = exhaustive_date_range in let rec loop n current_date = if n = ndays then true else begin let old_method = day_of_week current_date in let new_method = Date . day_of_week current_date in if Day_of_week . ( ) = new_method old_method then loop ( n + 1 ) ( Date . add_days current_date 1 ) else false end in loop 1 start_date ; ; let exhaustive_add_days_test ( ) = let start_date , ndays = exhaustive_date_range in let rec loop n current_date = if n = ndays then true else begin let old_method = add_days current_date 1 in let new_method = Date . add_days current_date 1 in if ( Date . ( ) = old_method new_method ) then loop ( n + 1 ) new_method else false end in loop 1 start_date ; ; ; end
|
let ( ) = add " day_of_week " ( fun ( ) -> " exhaustive " @? Old_date_impl . exhaustive_day_of_week_test ( ) )
|
let ( ) = add " add_days " ( fun ( ) -> " one " @? ( Date . add_days ( Date . of_string " 2008 - 11 - 02 " ) 1 = Date . of_string " 2008 - 11 - 03 " ) ; " two " @? ( Date . add_days ( Date . of_string " 2008 - 11 - 02 " ) 2 = Date . of_string " 2008 - 11 - 04 " ) ; " leap " @? ( Date . add_days ( Date . of_string " 2000 - 02 - 28 " ) 1 = Date . of_string " 2000 - 02 - 29 " ) ; " exhaustive " @? Old_date_impl . exhaustive_add_days_test ( ) )
|
let ( ) = add " add_months " ( fun ( ) -> " zero " @? ( Date . add_months ( Date . of_string " 2009 - 02 - 28 " ) 0 = Date . of_string " 2009 - 02 - 28 " ) ; " one " @? ( Date . add_months ( Date . of_string " 2009 - 01 - 30 " ) 1 = Date . of_string " 2009 - 02 - 28 " ) ; " two " @? ( Date . add_months ( Date . of_string " 2009 - 01 - 30 " ) 2 = Date . of_string " 2009 - 03 - 30 " ) ; " december " @? ( Date . add_months ( Date . of_string " 2009 - 02 - 28 " ) 10 = Date . of_string " 2009 - 12 - 28 " ) ; " neg " @? ( Date . add_months ( Date . of_string " 2009 - 01 - 30 " ) ( - 11 ) = Date . of_string " 2008 - 02 - 29 " ) ; )
|
let ( ) = add " add_weekdays_rounding_forward " ( fun ( ) -> let test lbl d1 n d2 = lbl @? ( Date . add_weekdays_rounding_forward ( Date . of_string d1 ) n = Date . of_string d2 ) in test " one " " 2009 - 01 - 01 " 1 " 2009 - 01 - 02 " ; test " one_weekend " " 2009 - 01 - 02 " 1 " 2009 - 01 - 05 " ; test " neg_one " " 2009 - 01 - 02 " ( - 1 ) " 2009 - 01 - 01 " ; test " neg_one_weekend " " 2009 - 01 - 05 " ( - 1 ) " 2009 - 01 - 02 " ; test " neg_two_weekend " " 2009 - 01 - 06 " ( - 2 ) " 2009 - 01 - 02 " ; test " non_leap_weekend " " 2009 - 02 - 27 " 1 " 2009 - 03 - 02 " ; test " leap_weekend " " 2008 - 02 - 28 " 2 " 2008 - 03 - 03 " ; ) ; ;
|
let ( ) = add " add_weekdays_rounding_backward " ( fun ( ) -> let test lbl d1 n d2 = lbl @? ( Date . add_weekdays_rounding_backward ( Date . of_string d1 ) n = Date . of_string d2 ) in test " one " " 2009 - 01 - 01 " 1 " 2009 - 01 - 02 " ; test " one_weekend " " 2009 - 01 - 02 " 1 " 2009 - 01 - 05 " ; test " neg_one " " 2009 - 01 - 02 " ( - 1 ) " 2009 - 01 - 01 " ; test " neg_one_weekend " " 2009 - 01 - 05 " ( - 1 ) " 2009 - 01 - 02 " ; test " neg_two_weekend " " 2009 - 01 - 06 " ( - 2 ) " 2009 - 01 - 02 " ; test " non_leap_weekend " " 2009 - 02 - 27 " 1 " 2009 - 03 - 02 " ; test " leap_weekend " " 2008 - 02 - 28 " 2 " 2008 - 03 - 03 " ; ) ; ;
|
let ( ) = add " add_business_days_rounding_forward " ( fun ( ) -> let test lbl d1 n d2 = let is_holiday d = List . mem ~ equal : Date . equal ( List . map [ " 2009 - 01 - 01 " ; " 2009 - 03 - 01 " ; " 2009 - 03 - 02 " ; ] ~ f : Date . of_string ) d in let res = Date . add_business_days_rounding_forward ~ is_holiday ( Date . of_string d1 ) n in lbl @? ( res = Date . of_string d2 ) in test " one " " 2009 - 01 - 01 " 1 " 2009 - 01 - 05 " ; test " one_weekend " " 2009 - 01 - 02 " 1 " 2009 - 01 - 05 " ; test " neg_one " " 2009 - 01 - 02 " ( - 1 ) " 2008 - 12 - 31 " ; test " neg_one_weekend " " 2009 - 01 - 05 " ( - 1 ) " 2009 - 01 - 02 " ; test " neg_two_weekend " " 2009 - 01 - 06 " ( - 2 ) " 2009 - 01 - 02 " ; test " non_leap_weekend " " 2009 - 02 - 27 " 1 " 2009 - 03 - 03 " ; test " leap_weekend " " 2008 - 02 - 28 " 2 " 2008 - 03 - 03 " ; ) ; ;
|
let ( ) = add " add_business_days_rounding_backward " ( fun ( ) -> let test lbl d1 n d2 = let is_holiday d = List . mem ~ equal : Date . equal ( List . map [ " 2009 - 01 - 01 " ; " 2009 - 03 - 01 " ; " 2009 - 03 - 02 " ; ] ~ f : Date . of_string ) d in let res = Date . add_business_days_rounding_backward ~ is_holiday ( Date . of_string d1 ) n in lbl @? ( res = Date . of_string d2 ) in test " one " " 2009 - 01 - 01 " 1 " 2009 - 01 - 02 " ; test " one_weekend " " 2009 - 01 - 02 " 1 " 2009 - 01 - 05 " ; test " neg_one " " 2009 - 01 - 02 " ( - 1 ) " 2008 - 12 - 31 " ; test " neg_one_weekend " " 2009 - 01 - 05 " ( - 1 ) " 2009 - 01 - 02 " ; test " neg_two_weekend " " 2009 - 01 - 06 " ( - 2 ) " 2009 - 01 - 02 " ; test " non_leap_weekend " " 2009 - 02 - 27 " 1 " 2009 - 03 - 03 " ; test " leap_weekend " " 2008 - 02 - 28 " 2 " 2008 - 03 - 03 " ; ) ; ;
|
let ( ) = add " span_scale " ( fun ( ) -> " ms " @? speq ( Time . Span . scale ( sec 10 . ) 0 . 001 ) ( Time . Span . of_ms 10 . ) ; " min " @? speq ( Time . Span . scale ( sec 10 . ) 60 . ) ( Time . Span . of_min 10 . ) ; " hr " @? speq ( Time . Span . scale ( sec 10 . ) ( 60 . . * 60 . ) ) ( Time . Span . of_hr 10 . ) ; ) ; add " span_conv " ( fun ( ) -> for _ = 1 to 100 do " sec " @? convtest Time . Span . to_sec sec ; " ms " @? convtest Time . Span . to_ms Span . of_ms ; " min " @? convtest ( fun x -> Time . Span . to_sec x . / 60 . ) Span . of_min ; " hr " @? convtest ( fun x -> Time . Span . to_sec x . / 60 . . / 60 . ) Span . of_hr ; " sexp " @? convtest ~ tol : 0 . 0001 ( fun x -> Time . Span . to_sec ( Time . Span . t_of_sexp x ) ) ( fun x -> Span . sexp_of_t ( sec x ) ) ; done ) ; add " date " ( fun ( ) -> let d = Date . create_exn ~ y : 2004 ~ m : Month . Apr ~ d : 15 in " conv1 " @? ( Date . to_string d = " 2004 - 04 - 15 " ) ; " conv2 " @? ( d = Date . of_string " 2004 - 04 - 15 " ) ; " conv3 " @? ( d = Date . of_string " 20040415 " ) ; " conv4 " @? ( d = Date . of_string " 15APR2004 " ) ; " conv5 " @? ( d = Date . of_string " 04 / 15 / 2004 " ) ; " conv6 " @? ( d = Date . of_string " 2004 / 04 / 15 " ) ; " conv7 " @? ( d = Date . of_string " 4 / 15 / 4 " ) ; ) ; add " norollover " ( fun ( ) -> let zone = Time . Zone . of_string " nyc " in let t1 = Time . of_localized_string ~ zone " 2005 - 05 - 25 12 : 46 : 59 . 900 " in let t2 = Time . add t1 ( Time . Span . of_ms 99 . 9 ) in " 60secspr " @? ( ( Time . to_string_abs ~ zone t2 ) = " 2005 - 05 - 25 12 : 46 : 59 . 999900 - 04 : 00 " ) ; ) ; add " to_string , of_string " ( fun ( ) -> let check time = if reasonable_time time then begin let time ' = Time . of_string ( Time . to_string time ) in if similar_time time time ' then true else begin Printf . printf " \ nbad time : % f \ n " %! ( Time . to_span_since_epoch time |> Time . Span . to_sec ) ; exit 7 ; end ; end else true in Quickcheck_deprecated . laws_exn " string " 100 time_gen check ) ; add " to_string , of_string2 " ( fun ( ) -> let zone = Time . Zone . find_exn " America / New_York " in let s = " 2005 - 06 - 01 10 : 15 : 08 . 047123 - 04 : 00 " in let t = Time . of_string s in " foo " @? ( Time . to_string_abs t ~ zone = s ) ) ; add " to_string , of_string3 " ( fun ( ) -> let zone = Time . Zone . find_exn " America / New_York " in let s = " 2006 - 06 - 16 04 : 37 : 07 . 082945 - 04 : 00 " in let t = Time . of_string s in " foo " @? ( Time . to_string_abs t ~ zone = s ) ) ; add " of_string without colon , negative offset " ( fun ( ) -> let t = " 2015 - 07 - 14 10 : 31 : 55 . 564871 - 04 : 00 " |> Time . of_string_abs in let s = " 2015 - 07 - 14 10 : 31 : 55 . 564871 - 0400 " in " no - colon - abs - negative - offset " @? ( Time . of_string_abs s = t ) ) ; add " of_string without colon , positive offset " ( fun ( ) -> let t = " 2015 - 07 - 14 10 : 31 : 55 . 564871 + 04 : 00 " |> Time . of_string_abs in let s = " 2015 - 07 - 14 10 : 31 : 55 . 564871 + 0400 " in " no - colon - abs - positive - offset " @? ( Time . of_string_abs s = t ) ) ; add " of_string with leap second " ( fun ( ) -> let expected_time_at_leap_second = Time . of_date_ofday ~ zone : Time . Zone . utc ( Date . create_exn ~ y : 2015 ~ m : Jul ~ d : 1 ) Time . Ofday . start_of_day in " foo " @? List . for_all ~ f ( : fun s -> Time . of_string s = expected_time_at_leap_second ) [ " 2015 - 06 - 30 23 : 59 : 60Z " ; " 2015 - 06 - 30 23 : 59 : 60 . 500Z " ] ) ; add " to_filename_string , of_filename_string " ( fun ( ) -> let zone = ( force Time . Zone . local ) in let check time = if reasonable_time time then let time ' = Time . of_filename_string ~ zone ( Time . to_filename_string time ~ zone ) in similar_time time time ' else true in Quickcheck_deprecated . laws_exn " string " 100 time_gen check ; ) ; add " to_filename_string , of_filename_string2 " ( fun ( ) -> let zone = ( force Time . Zone . local ) in let s = " 2005 - 06 - 01_10 - 15 - 08 . 047983 " in let t = Time . of_filename_string s ~ zone in " foo " @? ( Time . to_filename_string t ~ zone = s ) ) ; add " of_sexp , to_sexp " ( fun ( ) -> let check time = if reasonable_time time then let time ' = Time . t_of_sexp ( Time . sexp_of_t time ) in similar_time time time ' else true in Quickcheck_deprecated . laws_exn " sexp " 100 time_gen check ) ; add " daylight_saving_time " ( fun ( ) -> let zone = Time . Zone . find_exn " America / New_York " in let s = " 2006 - 04 - 02 23 : 00 : 00 . 000000 - 04 : 00 " in let time = Time . of_string s in " dst " @? ( Time . to_string_abs ~ zone time = s ) ) ; add " weird_date_in_time " ( fun ( ) -> let zone = Time . Zone . find_exn " America / New_York " in let t1 = Time . of_string " 01 JAN 2008 10 : 37 : 22 . 551 - 05 : 00 " in " rnse1 " @? ( Time . to_string_abs t1 ~ zone = " 2008 - 01 - 01 10 : 37 : 22 . 551000 - 05 : 00 " ) ; let t2 = Time . of_string " 01 FEB 2008 17 : 38 : 44 . 031 - 05 : 00 " in " rnse2 " @? ( Time . to_string_abs t2 ~ zone = " 2008 - 02 - 01 17 : 38 : 44 . 031000 - 05 : 00 " ) ) ; add " ofday_small_diff " ( fun ( ) -> let same x y = Float . abs ( x . - y ) < sqrt Float . epsilon_float in let check ( s1 , s2 , d ) = let t1 = Time . Ofday . of_string s1 in let t2 = Time . Ofday . of_string s2 in same ( Time . Span . to_sec ( Time . Ofday . small_diff t1 t2 ) ) d && same ( Time . Span . to_sec ( Time . Ofday . small_diff t2 t1 ) ) ( . ~- d ) in " foo " @? List . for_all ~ f : check [ " 10 : 00 : 01 . 298 " , " 14 : 59 : 55 . 000 " , 6 . 298 ; " 08 : 59 : 54 . 000 " , " 10 : 00 : 01 . 555 " , ( - 7 . 555 ) ; " 12 : 48 : 55 . 787 " , " 17 : 48 : 55 . 000 " , 0 . 787 ; ] ) ; add " occurrence_right_side " ( fun ( ) -> let times = [ " 00 : 00 : 00 " ; " 00 : 00 : 01 " ; " 09 : 00 : 00 " ; " 11 : 59 : 59 " ; " 12 : 00 : 00 " ; " 12 : 00 : 01 " ; " 18 : 30 : 30 " ; " 23 : 59 : 59 " ; ] in let now = Time . now ( ) in let now_f = Time . to_span_since_epoch now |> Time . Span . to_sec in let zone = ( force Time . Zone . local ) in let utimes = Time . to_ofday ~ zone now :: List . map times ~ f ( : Time . Ofday . of_string ) in let after_times = List . map utimes ~ f ( : fun ut -> Time . occurrence ` First_after_or_at now ~ zone ~ ofday : ut ) in let before_times = List . map utimes ~ f ( : fun ut -> Time . occurrence ` Last_before_or_at now ~ zone ~ ofday : ut ) in " right - side - after " @? List . for_all after_times ~ f ( : fun t -> Time . Span . to_sec ( Time . to_span_since_epoch t ) >= now_f ) ; " right - side - before " @? List . for_all before_times ~ f ( : fun t -> Time . Span . to_sec ( Time . to_span_since_epoch t ) <= now_f ) ; ) ; add " occurrence_distance " ( fun ( ) -> let now = Time . of_string " 2007 - 05 - 04 13 : 00 : 00 . 000 " in let after_times = [ ( " 13 : 00 : 00 . 000 " , " 2007 - 05 - 04 13 : 00 : 00 . 000 " ) ; ( " 13 : 00 : 00 . 001 " , " 2007 - 05 - 04 13 : 00 : 00 . 001 " ) ; ( " 11 : 59 : 59 . 999 " , " 2007 - 05 - 05 11 : 59 : 59 . 999 " ) ; ( " 00 : 00 : 00 . 000 " , " 2007 - 05 - 05 00 : 00 : 00 . 000 " ) ; ( " 12 : 59 : 59 . 000 " , " 2007 - 05 - 05 12 : 59 : 59 . 000 " ) ; ] in let before_times = [ ( " 13 : 00 : 00 . 000 " , " 2007 - 05 - 04 13 : 00 : 00 . 000 " ) ; ( " 13 : 00 : 00 . 001 " , " 2007 - 05 - 03 13 : 00 : 00 . 001 " ) ; ( " 11 : 59 : 59 . 999 " , " 2007 - 05 - 04 11 : 59 : 59 . 999 " ) ; ( " 00 : 00 : 00 . 000 " , " 2007 - 05 - 04 00 : 00 : 00 . 000 " ) ; ( " 12 : 59 : 59 . 000 " , " 2007 - 05 - 04 12 : 59 : 59 . 000 " ) ; ] in List . iter after_times ~ f ( : fun ( od_s , prediction_s ) -> let od = Time . Ofday . of_string od_s in let prediction = Time . of_string prediction_s in let real = Time . occurrence ` First_after_or_at now ~ zone ( : force Time . Zone . local ) ~ ofday : od in ( " right - distance - " ^ od_s ^ " , " ^ prediction_s ) @? if Time . Span . to_ms ( Time . diff prediction real ) = 0 . then true else false ) ; List . iter before_times ~ f ( : fun ( od_s , prediction_s ) -> let od = Time . Ofday . of_string od_s in let prediction = Time . of_string prediction_s in let real = Time . occurrence ` Last_before_or_at now ~ zone ( : force Time . Zone . local ) ~ ofday : od in ( " right - distance - " ^ od_s ^ " , " ^ prediction_s ) @? if Time . Span . to_ms ( Time . diff prediction real ) = 0 . then true else false ) ) ; add " diff " ( fun ( ) -> let d1 = Date . create_exn ~ y : 2000 ~ m : Month . Jan ~ d : 1 in let d2 = Date . create_exn ~ y : 2000 ~ m : Month . Jan ~ d : 2 in let d3 = Date . create_exn ~ y : 2000 ~ m : Month . Feb ~ d : 28 in let d4 = Date . create_exn ~ y : 2000 ~ m : Month . Mar ~ d : 1 in " normal - diff " @? ( Date . diff d2 d1 = 1 ) ; " leap - diff " @? ( Date . diff d4 d3 = 2 ) ) ; ;
|
let roundtrip s = let t = Span . of_string s in ( " string roundtrip " ^ s ) @? Span . ( ) = t ( Time . Span . of_string ( Time . Span . to_string t ) ) ; ;
|
let ( ) = let assert_raises f = try f ( ) ; raise Finished with | Finished -> assert false | _ -> ( ) in let extensions = [ " ms " ; " s " ; " m " ; " h " ] in add " roundtrip span <-> string " ( fun ( ) -> List . iter extensions ~ f ( : fun ext -> let t x = roundtrip ( x ^ ext ) in t " 1 " ; t " 5 " ; t " 1 . 34 " ; ) ; let t x = roundtrip ( x ^ " s " ) in t " 59 . 9999 " ; t " 59 " ; ) ; add " Span . of_string ( nan ) " ( fun ( ) -> assert_raises ( fun ( ) -> ignore ( Time . Span . of_string " nans " ) ) ) ; add " Span . of_string ( inf ) " ( fun ( ) -> assert_raises ( fun ( ) -> ignore ( Time . Span . of_string " infs " ) ) ) ; add " Span . of_string " ( fun ( ) -> let test string secs = ( " sec " ^ string ) @? ( Time . Span . to_sec ( Time . Span . of_string string ) = secs ) in test " 1ms " 0 . 001 ; test " 95ms " 0 . 095 ; test " 1222ms " 1 . 222 ; test " 1 . 222s " 1 . 222 ; test " 0 . 5m " 30 . ; test " 1m " 60 . ; test " 1h " ( 60 . . * 60 . ) ; ) ; add " Time . of_string_fix_proto " ( fun ( ) -> let test s t = ( " fix proto time " ^ s ) @? ( Time . of_string_fix_proto ` Utc s = t ) in test " 20080603 - 13 : 55 : 35 . 577 " ( Time . of_span_since_epoch ( Time . Span . of_sec ( Int64 . float_of_bits 4742872407195577745L ) ) ) ) ; ;
|
let test = " time " >::: ! test_list
|
module Alco = struct let union_empty ( ) = Alcotest . ( check ( list ( pair span_testable span_testable ) ) ) " same list " [ ] ( Time . union [ ] |> Resolver . resolve |> CCResult . get_exn |> CCList . of_seq ) let inter_empty ( ) = Alcotest . ( check ( list ( pair span_testable span_testable ) ) ) " same list " [ ( Constants . timestamp_min , Constants . timestamp_max ) ] ( Time . inter [ ] |> Resolver . resolve |> CCResult . get_exn |> CCList . of_seq ) let suite = [ Alcotest . test_case " union_empty " ` Quick union_empty ; Alcotest . test_case " inter_empty " ` Quick inter_empty ; ] end
|
module Qc = struct let to_of_sexp = QCheck . Test . make ~ count : 100_000 ~ name " : to_of_sexp " time ( fun t -> let t ' = t |> To_sexp . to_sexp |> Of_sexp . of_sexp in Time . equal t t ' ) let union_order_does_not_matter = QCheck . Test . make ~ count : 10 ~ name " : union_order_does_not_matter " QCheck . ( pair ( int_bound 10 ) ( time_list 3 ) ) ( fun ( rand , l1 ) -> let l2 = permute rand l1 in let t1 = Time . union l1 in let t2 = Time . union l2 in print_endline " " ; ===== print_endline ( To_sexp . to_sexp_string t1 ) ; print_endline " " ; ^^^^^ print_endline ( To_sexp . to_sexp_string t2 ) ; print_endline " " ; ===== flush stdout ; let r1 = OSeq . take 10_000 @@ CCResult . get_exn @@ Resolver . resolve t1 in let r2 = OSeq . take 10_000 @@ CCResult . get_exn @@ Resolver . resolve t2 in OSeq . equal ~ eq ( : = ) r1 r2 ) let inter_order_does_not_matter = QCheck . Test . make ~ count : 10 ~ name " : inter_order_does_not_matter " QCheck . ( pair ( int_bound 10 ) ( time_list 3 ) ) ( fun ( rand , l1 ) -> let l2 = permute rand l1 in let t1 = Time . inter l1 in let t2 = Time . inter l2 in let r1 = OSeq . take 10_000 @@ CCResult . get_exn @@ Resolver . resolve t1 in let r2 = OSeq . take 10_000 @@ CCResult . get_exn @@ Resolver . resolve t2 in OSeq . equal ~ eq ( : = ) r1 r2 ) let suite = [ to_of_sexp ; ] end
|
let remove_1st key list = let rec remove = function [ ] -> [ ] | a :: l -> if a == key then l else a ( :: remove l ) in remove list
|
let debug_time_travel = ref false
|
let insert_checkpoint ( { c_time = time } as checkpoint ) = let rec traverse = function [ ] -> [ checkpoint ] | ( ( { c_time = t } as a ) :: l ) as l ' -> if t > time then a ( :: traverse l ) else if t = time then raise Exit else checkpoint :: l ' in checkpoints := traverse ! checkpoints
|
let remove_checkpoint checkpoint = checkpoints := remove_1st checkpoint ! checkpoints
|
let wait_for_connection checkpoint = try Exec . unprotect ( function ( ) -> let old_controller = Input_handling . current_controller ! connection in execute_with_other_controller ( function fd -> old_controller fd ; if checkpoint . c_valid = true then exit_main_loop ( ) ) ! connection main_loop ) with Sys . Break -> checkpoint . c_parent <- root ; remove_checkpoint checkpoint ; checkpoint . c_pid <- - 1 ; raise Sys . Break
|
let set_current_checkpoint checkpoint = if ! debug_time_travel then prerr_endline ( " Select : " ^ ( Int . to_string checkpoint . c_pid ) ) ; if not checkpoint . c_valid then wait_for_connection checkpoint ; current_checkpoint := checkpoint ; let dead_frags = List . filter ( fun frag -> not ( List . mem frag checkpoint . c_code_fragments ) ) ( Symbols . code_fragments ( ) ) in List . iter Symbols . erase_symbols dead_frags ; set_current_connection checkpoint . c_fd
|
let kill_checkpoint checkpoint = if ! debug_time_travel then prerr_endline ( " Kill : " ^ ( Int . to_string checkpoint . c_pid ) ) ; if checkpoint . c_pid > 0 then ( if not checkpoint . c_valid then wait_for_connection checkpoint ; stop checkpoint . c_fd ; if checkpoint . c_parent . c_pid > 0 then wait_child checkpoint . c_parent . c_fd ; checkpoint . c_parent <- root ; close_io checkpoint . c_fd ; remove_file checkpoint . c_fd ; remove_checkpoint checkpoint ) ; checkpoint . c_pid <- - 1
|
let cut t = let rec cut_t = function [ ] -> ( [ ] , [ ] ) | ( { c_time = t ' } as a :: l ) as l ' -> if t ' <= t then ( [ ] , l ' ) else let ( b , e ) = cut_t l in ( a :: b , e ) in cut_t
|
let cut2 t0 t l = let rec cut2_t0 t = function [ ] -> [ ] | l -> let ( after , before ) = cut ( t0 -- t -- _1 ) l in let l = cut2_t0 ( t ++ t ) before in after :: l in let ( after , before ) = cut ( t0 -- _1 ) l in after ( :: cut2_t0 t before )
|
let chk_merge2 cont = let rec chk_merge2_cont = function [ ] -> cont | [ a ] -> let ( accepted , rejected ) = cont in ( a :: accepted , rejected ) | a :: l -> let ( accepted , rejected ) = chk_merge2_cont l in ( accepted , a :: rejected ) in chk_merge2_cont
|
let rec chk_merge = function [ ] -> ( [ ] , [ ] ) | l :: tail -> chk_merge2 ( chk_merge tail ) l
|
let new_checkpoint_list checkpoint_count accepted rejected = if List . length accepted >= checkpoint_count then let ( k , l ) = list_truncate2 checkpoint_count accepted in ( k , l @ rejected ) else let ( k , l ) = list_truncate2 ( checkpoint_count - List . length accepted ) rejected in ( List . merge ( fun { c_time = t1 } { c_time = t2 } -> compare t2 t1 ) accepted k , l )
|
let clean_checkpoints time checkpoint_count = let ( after , before ) = cut time ! checkpoints in let ( accepted , rejected ) = chk_merge ( cut2 time ! checkpoint_small_step before ) in let ( kept , lost ) = new_checkpoint_list checkpoint_count accepted after in List . iter kill_checkpoint ( lost @ rejected ) ; checkpoints := kept
|
let find_checkpoint_before time = let rec find = function [ ] -> print_string " Can ' t go that far in the past " ; ! print_newline ( ) ; if yes_or_no " Reload program " then begin load_program ( ) ; find ! checkpoints end else raise Toplevel | { c_time = t } as a :: l -> if t > time then find l else a in find ! checkpoints
|
let duplicate_current_checkpoint ( ) = let checkpoint = ! current_checkpoint in if not checkpoint . c_valid then wait_for_connection checkpoint ; let new_checkpoint = { c_time = checkpoint . c_time ; c_pid = 0 ; c_fd = checkpoint . c_fd ; c_valid = false ; c_report = checkpoint . c_report ; c_state = C_stopped ; c_parent = checkpoint ; c_breakpoint_version = checkpoint . c_breakpoint_version ; c_breakpoints = checkpoint . c_breakpoints ; c_trap_barrier = checkpoint . c_trap_barrier ; c_code_fragments = checkpoint . c_code_fragments } in checkpoints := list_replace checkpoint new_checkpoint ! checkpoints ; set_current_checkpoint checkpoint ; clean_checkpoints ( checkpoint . c_time ++ _1 ) ( ! checkpoint_max_count - 1 ) ; if new_checkpoint . c_pid = 0 then ( match do_checkpoint ( ) with Checkpoint_done pid -> ( new_checkpoint . c_pid <- pid ; if ! debug_time_travel then prerr_endline ( " Waiting for connection : " ^ Int . to_string pid ) ) | Checkpoint_failed -> prerr_endline " A fork failed . Reducing maximum number of checkpoints . " ; checkpoint_max_count := List . length ! checkpoints - 1 ; remove_checkpoint new_checkpoint )
|
let interrupted = ref false
|
let last_breakpoint = ref None
|
let last_debug_info = ref None
|
let rec do_go_dynlink steps = match do_go steps with | { rep_type = Code_loaded frag ; rep_event_count = steps } as report -> begin match ! last_debug_info with | Some di -> Symbols . add_symbols frag di ; Symbols . set_all_events frag ; last_debug_info := None | None -> assert false end ; if ! break_on_load then report else do_go_dynlink steps | { rep_type = Code_unloaded frag ; rep_event_count = steps } -> Symbols . erase_symbols frag ; do_go_dynlink steps | { rep_type = Debug_info di ; rep_event_count = steps } -> last_debug_info := Some ( Array . to_list di ) ; do_go_dynlink steps | report -> report
|
let rec stop_on_event report = match report with { rep_type = Breakpoint ; rep_program_pointer = pc ; rep_stack_pointer = sp } -> last_breakpoint := Some ( pc , sp ) ; Symbols . update_current_event ( ) ; begin match ! current_event with None -> find_event ( ) | Some _ -> ( ) end | { rep_type = Trap_barrier } -> find_event ( ) | _ -> ( ) if ! debug_time_travel then begin print_string " Searching next event . . . " ; print_newline ( ) end ; let report = do_go_dynlink _1 in ! current_checkpoint . c_report <- Some report ; stop_on_event report
|
let internal_step duration = match current_report ( ) with Some { rep_type = Exited | Uncaught_exc } -> ( ) | _ -> Exec . protect ( function ( ) -> if ! make_checkpoints then duplicate_current_checkpoint ( ) else remove_checkpoint ! current_checkpoint ; update_breakpoints ( ) ; update_trap_barrier ( ) ; ! current_checkpoint . c_state <- C_running duration ; let report = do_go_dynlink duration in ! current_checkpoint . c_report <- Some report ; ! current_checkpoint . c_state <- C_stopped ; ! current_checkpoint . c_code_fragments <- Symbols . code_fragments ( ) ; if report . rep_type = Event then begin ! current_checkpoint . c_time <- ! current_checkpoint . c_time ++ duration ; interrupted := false ; last_breakpoint := None end else begin ! current_checkpoint . c_time <- ! current_checkpoint . c_time ++ duration -- report . rep_event_count ++ _1 ; interrupted := true ; last_breakpoint := None ; stop_on_event report end ; ( try insert_checkpoint ! current_checkpoint with Exit -> kill_checkpoint ! current_checkpoint ; set_current_checkpoint ( find_checkpoint_before ( current_time ( ) ) ) ) ) ; if ! debug_time_travel then begin print_string " Checkpoints : pid ( time ) " ; print_newline ( ) ; List . iter ( function { c_time = time ; c_pid = pid ; c_valid = valid } -> Printf . printf " % d ( % Ld ) % s " pid time ( if valid then " " else " ( invalid ) " ) ) ! checkpoints ; print_newline ( ) end
|
let new_checkpoint pid fd = let new_checkpoint = { c_time = _0 ; c_pid = pid ; c_fd = fd ; c_valid = true ; c_report = None ; c_state = C_stopped ; c_parent = root ; c_breakpoint_version = 0 ; c_breakpoints = [ ] ; c_trap_barrier = 0 ; c_code_fragments = [ 0 ] } in insert_checkpoint new_checkpoint
|
let set_file_descriptor pid fd = let rec find = function [ ] -> prerr_endline " Unexpected connection " ; close_io fd ; false | ( { c_pid = pid ' } as checkpoint ) :: l -> if pid <> pid ' then find l else ( checkpoint . c_fd <- fd ; checkpoint . c_valid <- true ; true ) in if ! debug_time_travel then prerr_endline ( " New connection : " ( ^ Int . to_string pid ) ) ; find ( ! current_checkpoint ::! checkpoints )
|
let kill_all_checkpoints ( ) = List . iter kill_checkpoint ( ! current_checkpoint ::! checkpoints )
|
let forget_process fd pid = let checkpoint = List . find ( function c -> c . c_pid = pid ) ( ! current_checkpoint ::! checkpoints ) in if pid > 0 then begin Printf . eprintf " Lost connection with process % d " pid ; let kont = if checkpoint == ! current_checkpoint then begin Printf . eprintf " ( active process ) \ n " ; match ! current_checkpoint . c_state with C_stopped -> Printf . eprintf " at time % Ld " ! current_checkpoint . c_time ; fun ( ) -> raise Current_checkpoint_lost | C_running duration -> Printf . eprintf " between time % Ld and time % Ld " ! current_checkpoint . c_time ( ! current_checkpoint . c_time ++ duration ) ; fun ( ) -> raise ( Current_checkpoint_lost_start_at ( ! current_checkpoint . c_time , duration ) ) end else ignore in Printf . eprintf " \ n " ; flush stderr ; Input_handling . remove_file fd ; close_io checkpoint . c_fd ; remove_file checkpoint . c_fd ; remove_checkpoint checkpoint ; checkpoint . c_pid <- - 1 ; if checkpoint . c_parent . c_pid > 0 then wait_child checkpoint . c_parent . c_fd ; kont ( ) end
|
let recover ( ) = set_current_checkpoint ( find_checkpoint_before ( current_time ( ) ) )
|
let rec step_forward duration = if duration > ! checkpoint_small_step then begin let first_step = if duration > ! checkpoint_big_step then ! checkpoint_big_step else ! checkpoint_small_step in internal_step first_step ; if not ! interrupted then step_forward ( duration -- first_step ) end else if duration != _0 then internal_step duration
|
let internal_go_to time = let duration = time -- ( current_time ( ) ) in if duration > _0 then execute_without_breakpoints ( function ( ) -> step_forward duration )
|
let go_to time = let checkpoint = find_checkpoint_before time in set_current_checkpoint checkpoint ; internal_go_to time
|
let find_last_breakpoint max_time = let rec find break = let time = current_time ( ) in step_forward ( max_time -- time ) ; match ! last_breakpoint , ! temporary_breakpoint_position with ( Some _ , _ ) when current_time ( ) < max_time -> find ! last_breakpoint | ( Some ( pc , _ ) , Some pc ' ) when pc = pc ' -> ( max_time , ! last_breakpoint ) | _ -> ( time , break ) in find ( match current_pc_sp ( ) with ( Some ( pc , _ ) ) as state when breakpoint_at_pc pc -> state | _ -> None )
|
let rec back_to time time_max = let { c_time = t } = find_checkpoint_before ( pre64 time_max ) in go_to ( max time t ) ; let ( new_time , break ) = find_last_breakpoint time_max in if break <> None || ( new_time <= time ) then begin go_to new_time ; interrupted := break <> None ; last_breakpoint := break end else back_to time new_time
|
let step_backward duration = let time = current_time ( ) in if time > _0 then back_to ( max _0 ( time -- duration ) ) time
|
let rec run ( ) = internal_step ! checkpoint_big_step ; if not ! interrupted then run ( )
|
let back_run ( ) = if current_time ( ) > _0 then back_to _0 ( current_time ( ) )
|
let step duration = if duration >= _0 then step_forward duration else step_backward ( _0 -- duration )
|
let finish ( ) = Symbols . update_current_event ( ) ; match ! current_event with None -> prerr_endline " ` finish ' not meaningful in outermost frame . " ; raise Toplevel | Some { ev_ev { = ev_stacksize } } -> set_initial_frame ( ) ; let ( frame , pc ) = up_frame ev_stacksize in if frame < 0 then begin prerr_endline " ` finish ' not meaningful in outermost frame . " ; raise Toplevel end ; begin try ignore ( Symbols . any_event_at_pc pc ) with Not_found -> prerr_endline " Calling function has no debugging information . " ; raise Toplevel end ; exec_with_trap_barrier frame ( fun ( ) -> exec_with_temporary_breakpoint pc ( fun ( ) -> while run ( ) ; match ! last_breakpoint with Some ( pc ' , frame ' ) when pc = pc ' -> interrupted := false ; frame <> frame ' | _ -> false do ( ) done ) )
|
let next_1 ( ) = Symbols . update_current_event ( ) ; match ! current_event with None -> step _1 | Some { ev_ev { = ev_stacksize = ev_stacksize1 } } -> let ( frame1 , _pc1 ) = initial_frame ( ) in step _1 ; if not ! interrupted then begin Symbols . update_current_event ( ) ; match ! current_event with None -> ( ) | Some { ev_ev { = ev_stacksize = ev_stacksize2 } } -> let ( frame2 , _pc2 ) = initial_frame ( ) in if frame1 >= 0 && frame2 >= 0 && frame2 - ev_stacksize2 > frame1 - ev_stacksize1 then finish ( ) end
|
let rec next = function 0 -> ( ) | n -> next_1 ( ) ; if not ! interrupted then next ( n - 1 )
|
let start ( ) = Symbols . update_current_event ( ) ; match ! current_event with None -> prerr_endline " ` start not meaningful in outermost frame . " ; raise Toplevel | Some { ev_ev { = ev_stacksize } } -> let ( frame , _ ) = initial_frame ( ) in let ( frame ' , pc ) = up_frame ev_stacksize in if frame ' < 0 then begin prerr_endline " ` start not meaningful in outermost frame . " ; raise Toplevel end ; let nargs = match try Symbols . any_event_at_pc pc with Not_found -> prerr_endline " Calling function has no debugging information . " ; raise Toplevel with { ev_ev = { ev_info = Event_return nargs } } -> nargs | _ -> Misc . fatal_error " Time_travel . start " in let offset = if nargs < 4 then 1 else 2 in let pc = { pc with pos = pc . pos - 4 * offset } in while exec_with_temporary_breakpoint pc back_run ; match ! last_breakpoint with Some ( pc ' , frame ' ) when pc = pc ' -> step _minus1 ; ( not ! interrupted ) && ( frame ' - nargs > frame - ev_stacksize ) | _ -> false do ( ) done
|
let previous_1 ( ) = Symbols . update_current_event ( ) ; match ! current_event with None -> step _minus1 | Some { ev_ev { = ev_stacksize = ev_stacksize1 } } -> let ( frame1 , _pc1 ) = initial_frame ( ) in step _minus1 ; if not ! interrupted then begin Symbols . update_current_event ( ) ; match ! current_event with None -> ( ) | Some { ev_ev { = ev_stacksize = ev_stacksize2 } } -> let ( frame2 , _pc2 ) = initial_frame ( ) in if frame1 >= 0 && frame2 >= 0 && frame2 - ev_stacksize2 > frame1 - ev_stacksize1 then start ( ) end
|
let rec previous = function 0 -> ( ) | n -> previous_1 ( ) ; if not ! interrupted then previous ( n - 1 )
|
type record = { recorded_offsets : int array ; table : table ; }
|
type typ = | Backed of string | Offset_only of int
|
type t = { typ : typ ; record : record ; }
|
type ' a local_result = [ ` None | ` Single of ' a | ` Ambiguous of ' a * ' a ]
|
let recorded_offsets ( t : t ) : int list = Array . to_list t . record . recorded_offsets |> List . sort_uniq compare
|
let check_table ( ( starts , entries ) : table ) : bool = let size = Bigarray . Array1 . dim starts in assert ( size = Array . length entries ) ; let has_no_dup = let seen = ref Int64_set . empty in let has_no_dup = ref true in let i = ref 0 in while ! has_no_dup && ! i < size do let start = starts . { ! i } in if Int64_set . mem start ! seen then has_no_dup := false else seen := Int64_set . add start ! seen ; i := ! i + 1 done ; ! has_no_dup in let is_sorted = let i = ref 0 in let is_sorted = ref true in while ! is_sorted && ! i < size do ( if ! i > 0 then let cur = starts . { ! i } in let prev = starts . { ! i - 1 } in if cur < prev then is_sorted := false ) ; i := ! i + 1 done ; ! is_sorted in has_no_dup && is_sorted
|
let process_table ( ( starts , entries ) : table ) : record = let size = Bigarray . Array1 . dim starts in assert ( size = Array . length entries ) ; if size = 0 then failwith " Time zone record table is empty " else let starts , entries = let first_start = starts . { 0 } in let first_entry = entries . ( 0 ) in if Span . get_s Constants . timestamp_min < first_start then ( let starts ' = Bigarray . Array1 . create Bigarray . Int64 Bigarray . c_layout ( size + 1 ) in let sub = Bigarray . Array1 . sub starts ' 1 size in starts ' . { 0 } <- Span . get_s Constants . timestamp_min ; Bigarray . Array1 . blit starts sub ; ( starts ' , Array . append [ | first_entry ] | entries ) ) else ( starts . { 0 } <- Span . get_s Constants . timestamp_min ; ( starts , entries ) ) in let recorded_offsets = Array . fold_left ( fun acc entry -> Int_set . add entry . offset acc ) Int_set . empty entries |> Int_set . to_list |> CCArray . of_list in let table = ( starts , entries ) in assert ( check_table table ) ; { recorded_offsets ; table }
|
let name t = match t . typ with | Backed name -> name | Offset_only s -> let dur = Span . ( make_small ~ s ( ) |> For_human ' . view ) in if dur . hours = 0 && dur . minutes = 0 then " UTC " else Printf . sprintf " UTC % c % 02d :% 02d " ( match dur . sign with ` Pos -> ' ' + | ` Neg -> ' ' ) - dur . hours dur . minutes
|
let to_fixed_offset_from_utc t = match t . typ with | Backed _ -> None | Offset_only s -> Some ( Span . make_small ~ s ( ) )
|
let fixed_offset_name_parser = let open MParser in let open Parser_components in string " UTC " >> ( attempt ( char ' ' + >> return ` Pos <|> ( char ' ' - >> return ` Neg ) >>= fun sign -> attempt ( max_two_digit_nat_zero >>= fun hour -> char ' ' : >> max_two_digit_nat_zero << eof >>= fun minute -> return ( hour , minute ) ) <|> ( max_two_digit_nat_zero << eof >>= fun hour -> return ( hour , 0 ) ) >>= fun ( hour , minute ) -> if hour < 24 && minute < 60 then return ( Span . For_human ' . make_exn ~ sign ~ hours : hour ~ minutes : minute ( ) ) else fail " Invalid offset " ) <|> ( eof >> return Span . zero ) )
|
let fixed_offset_of_name ( s : string ) : Span . t option = match Parser_components . result_of_mparser_result @@ MParser . ( parse_string ( fixed_offset_name_parser << eof ) ) s ( ) with | Ok x -> Some x | Error _ -> None
|
let equal t1 t2 = ( match ( t1 . typ , t2 . typ ) with | Backed name1 , Backed name2 -> CCString . equal name1 name2 | Offset_only s1 , Offset_only s2 -> CCInt . equal s1 s2 | Backed name , Offset_only offset | Offset_only offset , Backed name -> ( match fixed_offset_of_name name with | Some offset ' -> offset = CCInt64 . to_int @@ Span . get_s offset ' | None -> false ) ) && Bigarray . Array1 . dim ( fst t1 . record . table ) = Bigarray . Array1 . dim ( fst t2 . record . table ) && Array . length ( snd t1 . record . table ) = Array . length ( snd t2 . record . table ) && CCArray . for_all2 ( fun e1 e2 -> e1 = e2 ) ( snd t1 . record . table ) ( snd t2 . record . table )
|
let one_day = Span . For_human ' . ( make_exn ~ days : 1 ( ) )
|
let make_offset_only ( offset : Span . t ) = if Span . abs offset > one_day then None else let offset = CCInt64 . to_int @@ Span . get_s offset in Some { typ = Offset_only offset ; record = process_table ( Bigarray . Array1 . of_array Bigarray . Int64 Bigarray . C_layout [ | Span . get_s Constants . timestamp_min ] , | [ | { is_dst = false ; offset } ] | ) ; }
|
let make_offset_only_exn offset = match make_offset_only offset with | None -> invalid_arg " make_offset_only_span_exn " | Some x -> x
|
let utc : t = CCOption . get_exn_or " Expected successful construction of UTC " ( make_offset_only Span . zero )
|
let bsearch_table timestamp ( ( starts , _ ) : table ) = Bigarray_utils . bsearch ~ cmp : Int64 . compare timestamp starts
|
let lookup_timestamp_utc ( t : t ) timestamp = let table = t . record . table in let entries = snd table in match bsearch_table timestamp table with | ` At i -> Some entries . ( i ) | ` All_lower -> Some entries . ( Array . length entries - 1 ) | ` All_bigger -> None | ` Just_after i -> Some entries . ( i ) | ` Empty -> None
|
let local_interval_of_table ( ( starts , entries ) : table ) ( i : int ) = let size = Bigarray . Array1 . dim starts in let start_utc = starts . { i } in let entry = entries . ( i ) in let end_exc_utc = if i = size - 1 then Span . get_s Constants . timestamp_max else starts . { i + 1 } in ( Int64 . add start_utc ( Int64 . of_int entry . offset ) , Int64 . add end_exc_utc ( Int64 . of_int entry . offset ) )
|
let interval_mem ( t : int64 ) ( ( x , y ) : int64 * int64 ) = x <= t && t < y
|
let lookup_timestamp_local ( t : t ) timestamp : entry local_result = let table = t . record . table in let starts , entries = table in let size = Bigarray . Array1 . dim starts in let index = match bsearch_table timestamp table with | ` At i -> Some i | ` All_lower -> Some ( size - 1 ) | ` All_bigger -> Some 0 | ` Just_after i -> Some i | ` Empty -> None in match index with | None -> ` None | Some index -> ( let x1 = if index > 0 && interval_mem timestamp ( local_interval_of_table table ( index - 1 ) ) then Some entries . ( index - 1 ) else None in let x2 = if interval_mem timestamp ( local_interval_of_table table index ) then Some entries . ( index ) else None in let x3 = if index < size - 1 && interval_mem timestamp ( local_interval_of_table table ( index + 1 ) ) then Some entries . ( index + 1 ) else None in match ( x1 , x2 , x3 ) with | None , None , None -> ` None | Some x , None , None | None , Some x , None | None , None , Some x -> ` Single x | Some x , Some y , None | Some x , None , Some y | None , Some x , Some y -> ` Ambiguous ( x , y ) | Some _ , Some _ , Some _ -> failwith " Unexpected case " )
|
module Raw = struct let to_transition_seq ( t : t ) : ( ( int64 * int64 ) * entry ) Seq . t = let table = t . record . table in let starts , entries = table in let size = Bigarray . Array1 . dim starts in let rec aux s = match s ( ) with | Seq . Nil -> Seq . empty | Seq . Cons ( ( k1 , entry1 ) , s ) -> ( match s ( ) with | Seq . Nil -> fun ( ) -> Seq . Cons ( ( ( k1 , Span . get_s Constants . timestamp_max ) , entry1 ) , aux Seq . empty ) | Seq . Cons ( ( k2 , entry2 ) , rest ) -> fun ( ) -> Seq . Cons ( ( ( k1 , k2 ) , entry1 ) , aux ( fun ( ) -> Seq . Cons ( ( k2 , entry2 ) , rest ) ) ) ) in OSeq . ( 0 --^ size ) |> OSeq . map ( fun i -> ( starts . { i } , entries . ( i ) ) ) |> aux let to_transitions ( t : t ) : ( ( int64 * int64 ) * entry ) list = CCList . of_seq @@ to_transition_seq t let table_of_transitions ( l : ( int64 * entry ) list ) : table option = let table = l |> List . split |> fun ( starts , entries ) -> let starts = starts |> Array . of_list |> Bigarray . Array1 . of_array Bigarray . Int64 Bigarray . C_layout in let entries = Array . of_list entries in ( starts , entries ) in if check_table table then Some table else None let of_table ~ name table = match fixed_offset_of_name name with | Some offset -> make_offset_only offset | None -> Some { typ = Backed name ; record = process_table table } let of_table_exn ~ name table = CCOption . get_exn_or " Failed to construct time zone from table " ( of_table ~ name table ) let of_transitions ~ name ( l : ( int64 * entry ) list ) : t option = match table_of_transitions l with | None -> None | Some table -> of_table ~ name table end
|
let offset_is_recorded offset ( t : t ) = Array . mem ( CCInt64 . to_int @@ Span . get_s offset ) t . record . recorded_offsets
|
module Compressed_table = struct type relative_entry = { value : int64 ; is_abs : bool ; is_dst : bool ; offset : int ; } let lt_relative_entry ( x : relative_entry ) ( y : relative_entry ) = if x . value < y . value then true else if x . is_abs && not y . is_abs then true else if x . is_dst && not y . is_dst then true else x . offset < y . offset let equal_relative_entry ( x : relative_entry ) ( y : relative_entry ) = x . value = y . value && x . is_abs = y . is_abs && x . is_dst = y . is_dst && x . offset = y . offset module Relative_entry_set = CCSet . Make ( struct type t = relative_entry let compare x y = if lt_relative_entry x y then - 1 else if equal_relative_entry x y then 0 else 1 end ) let to_relative_entries ( ( starts , entries ) : table ) : relative_entry array * int array = let count = Array . length entries in let relative_entries = Array . init count ( fun i -> if i = 0 then { value = starts . { i } ; is_abs = true ; is_dst = entries . ( i ) . is_dst ; offset = entries . ( i ) . offset ; } else let a = starts . { i } in let b = starts . { i - 1 } in let would_overflow = b < 0L && a > Int64 . ( add max_int b ) in let would_underflow = b > 0L && a < Int64 . ( add min_int b ) in if would_overflow || would_underflow then { value = a ; is_abs = true ; is_dst = entries . ( i ) . is_dst ; offset = entries . ( i ) . offset ; } else { value = Int64 . sub a b ; is_abs = false ; is_dst = entries . ( i ) . is_dst ; offset = entries . ( i ) . offset ; } ) in let uniq_relative_entries = Array . fold_left ( fun acc x -> Relative_entry_set . add x acc ) Relative_entry_set . empty relative_entries |> Relative_entry_set . to_seq |> Array . of_seq in let indices = Array . init count ( fun i -> let ( index_to_relative_entry , _ ) = CCOption . get_exn_or " Unexpected failure in relative entry lookup " @@ CCArray . find_idx ( fun x -> equal_relative_entry relative_entries . ( i ) x ) uniq_relative_entries in index_to_relative_entry ) in ( uniq_relative_entries , indices ) let add_to_buffer ( buffer : Buffer . t ) ( t : table ) : unit = let ( uniq_relative_entries , indices ) = to_relative_entries t in let uniq_relative_entry_count = Array . length uniq_relative_entries in assert ( uniq_relative_entry_count <= 0xFF ) ; Buffer . add_uint8 buffer uniq_relative_entry_count ; Array . iter ( fun ( entry : relative_entry ) -> let offset = Span . make_small ~ s : entry . offset ( ) in let view = Span . For_human ' . view offset in let value_is_64bit = Int64 . logand entry . value 0xFFFF_FFFF_0000_0000L <> 0L in let flags = 0b0000_0000 lor ( if entry . is_abs then 0b0100_0000 else 0x00 ) lor ( if value_is_64bit then 0b0010_0000 else 0x00 ) lor ( if entry . is_dst then 0b0001_0000 else 0x00 ) lor ( if view . sign = ` Pos then 0b0000_1000 else 0x00 ) lor ( if view . hours > 0 then 0b0000_0100 else 0x00 ) lor ( if view . minutes > 0 then 0b0000_0010 else 0x00 ) lor ( if view . seconds > 0 then 0b0000_0001 else 0x00 ) in Buffer . add_uint8 buffer flags ; if value_is_64bit then Buffer . add_int64_be buffer entry . value else ( Buffer . add_int32_be buffer ( Int64 . to_int32 entry . value ) ) ; if view . hours > 0 then ( Buffer . add_int8 buffer view . hours ) ; if view . minutes > 0 then ( Buffer . add_int8 buffer view . minutes ) ; if view . seconds > 0 then ( Buffer . add_int8 buffer view . seconds ) ; ) uniq_relative_entries ; let index_count = Array . length indices in assert ( index_count <= 0xFFFF ) ; Buffer . add_uint16_be buffer index_count ; Array . iter ( fun i -> Buffer . add_int8 buffer i ) indices let to_string ( t : table ) : string = let buffer = Buffer . create 512 in add_to_buffer buffer t ; Buffer . contents buffer module Parsers = struct open Angstrom let offset_p ~ is_pos ~ hour_nz ~ minute_nz ~ second_nz = let sign = if is_pos then ` Pos else ` Neg in ( if hour_nz then any_int8 else return 0 ) >>= ( fun hours -> ( if minute_nz then any_int8 else return 0 ) >>= ( fun minutes -> ( if second_nz then any_int8 else return 0 ) >>| ( fun seconds -> Span . For_human ' . make_exn ~ sign ~ hours ~ minutes ~ seconds ( ) |> Span . get_s |> Int64 . to_int ) ) ) let relative_entry_p = any_uint8 >>= ( fun flags -> let is_abs = flags land 0b0100_0000 <> 0 in let value_is_64bit = flags land 0b0010_0000 <> 0 in let is_dst = flags land 0b0001_0000 <> 0 in let is_pos = flags land 0b0000_1000 <> 0 in let hour_nz = flags land 0b0000_0100 <> 0 in let minute_nz = flags land 0b0000_0010 <> 0 in let second_nz = flags land 0b0000_0001 <> 0 in ( if value_is_64bit then BE . any_int64 else lift ( fun x -> Int64 . ( logand ( of_int32 x ) 0xFFFF_FFFFL ) ) BE . any_int32 ) >>= ( fun value -> offset_p ~ is_pos ~ hour_nz ~ minute_nz ~ second_nz >>| ( fun offset -> { value ; is_abs ; is_dst ; offset } ) ) ) let relative_table : ( relative_entry array * int array ) Angstrom . t = any_uint8 >>= ( fun uniq_relative_entry_count -> count uniq_relative_entry_count relative_entry_p >>= ( fun uniq_relative_entries -> BE . any_uint16 >>= ( fun index_count -> count index_count any_int8 >>| ( fun indices -> ( Array . of_list uniq_relative_entries , Array . of_list indices ) ) ) ) ) let table : table option Angstrom . t = relative_table >>= ( fun ( uniq_relative_entries , indices ) -> let size = Array . length indices in let starts = Bigarray . Array1 . create Bigarray . Int64 Bigarray . c_layout size in let entries = Array . make size { is_dst = false ; offset = 0 } in Array . iteri ( fun i index -> let entry = uniq_relative_entries . ( index ) in ( if entry . is_abs then starts . { i } <- entry . value else starts . { i } <- Int64 . add starts . { i - 1 } entry . value ) ; entries . ( i ) <- { is_dst = entry . is_dst ; offset = entry . offset } ; ) indices ; let table = ( starts , entries ) in if check_table table then return ( Some table ) else return None ) end let of_string ( s : string ) : table option = let open Angstrom in match parse_string ~ consume : Consume . All Parsers . table s with | Ok x -> x | Error _ -> None let of_string_exn s = match of_string s with | Some x -> x | None -> failwith " Failed to deserialize compressed table " end
|
module Compressed = struct let add_to_buffer ( buffer : Buffer . t ) ( t : t ) : unit = let name = name t in let name_len = String . length name in assert ( name_len <= 0xFF ) ; Buffer . add_uint8 buffer name_len ; Buffer . add_string buffer name ; Compressed_table . add_to_buffer buffer t . record . table let to_string ( t : t ) : string = let buffer = Buffer . create 512 in add_to_buffer buffer t ; Buffer . contents buffer module Parsers = struct let p : t option Angstrom . t = let open Angstrom in any_uint8 >>= ( fun name_len -> take name_len >>= ( fun name -> Compressed_table . Parsers . table >>| ( fun table -> match table with | None -> None | Some table -> Raw . of_table ~ name table ) ) ) end let of_string ( s : string ) : t option = let open Angstrom in match parse_string ~ consume : Consume . All Parsers . p s with | Ok x -> x | Error _ -> None let of_string_exn s = match of_string s with | Some x -> x | None -> failwith " Failed to deserialize compressed time zone " end
|
module Sexp = struct let of_sexp ( x : CCSexp . t ) : t option = let open Of_sexp_utils in try match x with | ` List l -> ( match l with | ` Atom " tz " :: ` Atom name :: transitions -> transitions |> List . map ( fun x -> match x with | ` List [ start ; ` List [ ` Atom is_dst ; offset ] ] -> let start = int64_of_sexp start in let is_dst = match is_dst with | " t " -> true | " f " -> false | _ -> invalid_data " " in let offset = int_of_sexp offset in let entry = { is_dst ; offset } in ( start , entry ) | _ -> invalid_data " " ) |> Raw . of_transitions ~ name | _ -> invalid_data " " ) | ` Atom _ -> invalid_data " " with _ -> None let to_sexp ( t : t ) : CCSexp . t = let open To_sexp_utils in CCSexp . ( list ( atom " tz " :: atom ( name t ) :: List . map ( fun ( ( start , _ ) , entry ) -> list [ sexp_of_int64 start ; list [ ( if entry . is_dst then atom " t " else atom " f " ) ; sexp_of_int entry . offset ; ] ; ] ) ( Raw . to_transitions t ) ) ) let of_string s = let res = try CCSexp . parse_string s with _ -> Error " Failed to parse string into sexp " in match res with Error _ -> None | Ok x -> of_sexp x end
|
module JSON = struct let of_json json : t option = let exception Invalid_data in try match json with | ` Assoc l -> let name = match List . assoc " name " l with | ` String s -> s | _ -> raise Invalid_data in let table_rows = match List . assoc " table " l with | ` List l -> l | _ -> raise Invalid_data in table_rows |> List . map ( fun row -> match row with | ` List [ ` String s ; ` Assoc e ] -> let start = Int64 . of_string s in let is_dst = match List . assoc " is_dst " e with | ` Bool b -> b | _ -> raise Invalid_data in let offset = match List . assoc " offset " e with | ` Int x -> x | _ -> raise Invalid_data in let entry = { is_dst ; offset } in ( start , entry ) | _ -> raise Invalid_data ) |> Raw . of_transitions ~ name | _ -> raise Invalid_data with _ -> None let of_string s = try of_json @@ Yojson . Basic . from_string s with _ -> None let to_json ( t : t ) : Yojson . Basic . t = ` Assoc [ ( " name " , ` String ( name t ) ) ; ( " table " , ` List ( Raw . to_transition_seq t |> Seq . map ( fun ( ( start , _ ) , entry ) -> ` List [ ` String ( Int64 . to_string start ) ; ` Assoc [ ( " is_dst " , ` Bool entry . is_dst ) ; ( " offset " , ` Int entry . offset ) ; ] ; ] ) |> CCList . of_seq ) ) ; ] end
|
module Db = struct type db = table M . t let empty = M . empty let add tz db = M . add ( name tz ) tz . record . table db let find_opt name db = M . find_opt name db |> CCOption . map ( fun table -> Raw . of_table_exn ~ name table ) let remove name db = M . remove name db let add_seq db s : db = Seq . fold_left ( fun db tz -> add tz db ) db s let of_seq s : db = add_seq empty s let names db = List . map fst ( M . bindings db ) module Compressed = struct let dump ( db : db ) = Marshal . to_string ( M . map Compressed_table . to_string db ) [ ] let load ( s : string ) : db = M . map Compressed_table . of_string_exn ( Marshal . from_string s 0 ) end module Sexp = struct let of_sexp ( x : CCSexp . t ) : db option = let open Of_sexp_utils in try match x with | ` Atom _ -> invalid_data " " | ` List l -> Some ( l |> CCList . to_seq |> Seq . map ( fun x -> match Sexp . of_sexp x with | None -> invalid_data " " | Some x -> x ) |> of_seq ) with _ -> None let to_sexp db = M . bindings db |> List . map ( fun ( name , table ) -> Raw . of_table_exn ~ name table ) |> List . map Sexp . to_sexp |> CCSexp . list let of_string s = let res = try CCSexp . parse_string s with _ -> Error " Failed to parse string into sexp " in match res with Error _ -> None | Ok x -> of_sexp x end end
|
let db : table M . t ref = match db with | Some db -> ref db | None -> ref M . empty
|
let compressed : string M . t = match compressed with | Some s -> Marshal . from_string s 0 | None -> M . empty
|
let lookup_record name : record option = match M . find_opt name ! db with | Some table -> assert ( check_table table ) ; Some ( process_table table ) | None -> match M . find_opt name compressed with | Some compressed_table -> let table = Compressed_table . of_string_exn compressed_table in assert ( check_table table ) ; db := M . add name table ! db ; Some ( process_table table ) | None -> None
|
let make name : t option = match fixed_offset_of_name name with | Some fixed -> make_offset_only fixed | None -> ( match lookup_record name with | Some record -> Some { typ = Backed name ; record } | None -> None )
|
let make_exn name : t = match make name with Some x -> x | None -> invalid_arg " make_exn "
|
let available_time_zones = let s0 = M . to_seq ! db |> Seq . map fst |> String_set . of_seq in let s1 = M . to_seq compressed |> Seq . map fst |> String_set . of_seq in String_set . ( union s0 s1 |> to_list )
|
let local ( ) : t option = match Timedesc_tzlocal . local ( ) with | [ ] -> None | l -> List . fold_left ( fun tz name -> match tz with Some tz -> Some tz | None -> make name ) None l
|
let local_exn ( ) : t = match local ( ) with | None -> invalid_arg " local_exn : Could not determine the local timezone " | Some x -> x
|
let startup = timer_ticks ( )
|
let at n = Int64 . ( add startup ( mul ( of_int step ) ( of_int n ) ) )
|
let check_after n = let late = Int64 . sub ( timer_ticks ( ) ) ( at n ) in if late < Int64 . zero then failwith ( " Early by " ^ ( Int64 . ( to_string ( neg ( div late ( of_int 1_000_000 ) ) ) ) ) ^ " ms " )
|
let check_before n = let late = Int64 . sub ( timer_ticks ( ) ) ( at n ) in if late > Int64 . zero then failwith ( " Late by " ^ ( Int64 . ( to_string ( ( div late ( of_int 1_000_000 ) ) ) ) ) ^ " ms " )
|
let flag = Atomic . make false
|
let d1 ( ) = critical_section ( fun ( ) -> let r = wait_until ( at ( - 1 ) ) in assert ( r = Timeout ) ) ; check_before 1 ; critical_section ( fun ( ) -> let r = wait_until ( at 1 ) in assert ( r = Timeout ) ) ; check_after 1 ; check_before 2 ; critical_section ( fun ( ) -> let r = wait_for ( Int64 . of_int ( - step ) ) in assert ( r = Timeout ) ) ; check_before 2 ; critical_section ( fun ( ) -> let r = wait_for ( Int64 . of_int step ) in assert ( r = Timeout ) ) ; check_after 2 ; critical_section ( fun ( ) -> check_before 3 ; let r = wait_until ( at 4 ) in assert ( r = Notified ) ; Atomic . set flag true ) ; check_after 3 ; check_before 4
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.