#!hs2 ######################################################################## # Script : Timestamp.hsm # Purpose : Timed actions # Time-stamp: ######################################################################## # NOTE: none ######################################################################## #!load hamster.hsm #!initialize debug( 255, "<<< module 'Timestamp.hsm' >>>" ) HamRequireVersion( "1.3.21.0", True ) return(0) ######################################################################## # OncePerDays: Do things once in a number of days ######################################################################## # [IN] $event : Name of your event # (=> name of section in ini (will be preceeded by "TimeStamp_")) # $days : Number of days # [OUT] (result): False if already called with $entry in the last $day days # True otherwise (timestamp in ini is updated in this case) # # Example: # Purge once per day # if (OncePerDays("purge",1)) # HamPurge # endif sub OncePerDays($event,$days) return(OncePerPeriod($event,$days*(24*60*60))) endsub ######################################################################## # DaysSinceLast: Calculate time elapsed since last event ######################################################################## # [IN] $event : Name of your event (see OncePerDays) # [OUT] (result): Days elapsed since last call of OncePerDays # # Example: # Purge once per day # if (DaysSinceLast("purge")>=1) # SetTimestamp("purge",time) # HamPurge # endif # ######################################################################## # NOTE: this is a stupid example rebuilding the functionality of # OncePerDays sub DaysSinceLast($entry) return(PeriodSinceLast($entry)/(24*60*60)) endsub ######################################################################## # Similar functions ######################################################################## sub OncePerHours($entry,$hours) return(OncePerPeriod($entry,$hours*(60*60))) endsub sub HoursSinceLast($entry) return(PeriodSinceLast($entry)/(60*60)) endsub # ---------------------------------------- sub OncePerMinutes($entry,$minutes) return(OncePerPeriod($entry,$minutes*60)) endsub sub MinutesSinceLast($entry) return(PeriodSinceLast($entry)/60) endsub # ---------------------------------------- sub OncePerSeconds($entry,$seconds) return(OncePerPeriod($entry,$seconds)) endsub sub SecondsSinceLast($entry) return(PeriodSinceLast($entry)) endsub # ---------------------------------------- sub OncePerWeeks($entry,$weeks) return(OncePerPeriod($entry,$weeks*(7*24*60*60))) endsub sub WeeksSinceLast($entry) return(PeriodSinceLast($entry)/((7*24*60*60))) endsub # not exact: sub OncePerMonths($entry,$months) return(OncePerPeriod($entry,$months*(30*24*60*60))) endsub sub MonthsSinceLast($entry) return(PeriodSinceLast($entry)/((30*24*60*60))) endsub # not exact: sub OncePerYears($entry,$years) return(OncePerPeriod($entry,$years*(365*24*60*60))) endsub sub YearsSinceLast($entry) return(PeriodSinceLast($entry)/((365*24*60*60))) endsub # feel free to add functions for decades, centuries, millenniums and more.. ;-) sub GetReadableTime($t) var($y,$m,$d,$h,$mi,$s,$w) decodetime($t,$y,$m,$d,$h,$mi,$s,$w) #german version return(copy("SoMoDiMiDoFrSa", $w*2-1, 2)+" "+$d+"."+$m+"."+$y+ " um "+$h+":"+dig2($mi)+":"+dig2($s)) #english version #return(copy("SuMoTuWeThFrSa", $w*2-1, 2)+" "+$m+"/"+$d+"/"+$y+ " at "+$h+":"+dig2($mi)+":"+dig2($s)) endsub ######################################################################## # SetTimestamp: Set a timestamp explicitly ######################################################################## # [IN] $event : Name of your event (ini-section="Timestamp_$event") # $stamp : seconds till 1.1.1970 # [OUT] --- sub SetTimestamp($event,$stamp) var($entry,$t) $entry = "TimeStamp_"+$event IniWrite("",$entry, "LastExec", $stamp) # insert human readable entry (this entry is not used) # if you don't need it comment put the following lines IniWrite("",$entry, "LastExec_Readable", GetReadableTime($stamp)) endsub sub GetTimestamp($event) var($entry,$content,$stamp) $entry = "Timestamp_"+$event $content = IniRead("",$entry,"LastExec",0) $stamp = eval($content) return ($stamp) endsub sub PeriodSinceLast($event) return (time - GetTimestamp($event)) endsub sub dig2($dig) return(copy("0"+$dig,len($dig),2)) endsub sub OncePerPeriod($event,$seconds) if (PeriodSinceLast($event)>=$seconds) SetTimestamp($event,time) return(1) endif return(0) endsub