123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123letlocal():stringlist=(* Approached copied from Python package tzlocal:
https://github.com/regebro/tzlocal
*)matchSys.getenv_opt"TZ"with|Somename->[name]|None->ifCCIO.File.exists"/system/bin/getprop"then(* if we are under Termux on Android *)letic=Unix.open_process_in"getprop"inletname=(CCIO.read_allic)inclose_inic;[name]else(* Look for distribution specific configs *)lettry1=List.fold_left(funtzfile->matchtzwith|Sometz->Sometz|None->tryCCIO.with_in~flags:[Open_rdonly;]file(funic->letlines=CCIO.read_lines_licinmatchlineswith|[]->None|x::_->ifCCString.subx05="TZif2"thenNoneelseList.fold_left(funtzline->matchtzwith|Sometz->Sometz|None->letname=line|>String.split_on_char' '|>List.hd|>String.split_on_char'#'|>List.hdinSomename)Nonelines)with|_->None)None["/etc/timezone";"/var/db/zoneinfo"]inmatchtry1with|Somename->[name]|None->(* CentOS has a ZONE setting in /etc/sysconfig/clock,
OpenSUSE has a TIMEZONE setting in /etc/sysconfig/clock and
Gentoo has a TIMEZONE setting in /etc/conf.d/clock
We look through these files for a timezone
*)lettry2=List.fold_left(funtzfile->matchtzwith|Sometz->Sometz|None->tryCCIO.with_in~flags:[Open_rdonly;]file(funic->letlines=CCIO.read_lines_licinList.fold_left(funtzline->matchtzwith|Sometz->Sometz|None->letname=trySome(Scanf.sscanfline{| ZONE = "%[^"]"|}CCFun.id)with|_->trySome(Scanf.sscanfline{| TIMEZONE = "%[^"]"|}CCFun.id)with|_->Noneinmatchnamewith|None->None|Somes->Some(CCString.replace~sub:" "~by:"_"s))Nonelines)with|_->None)None["/etc/sysconfig/clock";"/etc/conf.d/clock"]inmatchtry2with|Somename->[name]|None->(* systemd distributions use symlinks that include the zone name *)lettry3=letfile="/etc/localtime"inifFileUtil.(testIs_linkfile)thenletreal_path=FileUtil.readlinkfileinletparts=String.split_on_char'/'real_pathinletcombinations,_=List.fold_left(fun(acc,parts)_->(parts::acc,List.tlparts))([],parts)partsinList.map(String.concat"/")combinationselse[]intry3