I’m not sure if it will fit your situation, but when I have a log file that I want to start fresh at midnight, months end, etc I initialize the time of the current period and check that as I send input to the file.
Code:
alias Init.LastSecOfMonth {
  var %ThisMonth = $asctime(m)
  var %ThisYear  = $asctime(yyyy)

  if (%ThisMonth < 12) var %NextMonth = $+($calc(%ThisMonth + 1),/,%ThisYear)
  else                 var %NextMonth = $+(1/,$calc(%ThisYear + 1))

  var %FirstSecNextMonth = $ctime(1/ $+ %NextMonth 0:00)
  set %LastSecOfMonth $calc(%FirstSecNextMonth - 1)

  echo -s   $asctime(%LastSecOfMonth,    y mmm yyyy H:nn)
  echo -s   $asctime(%FirstSecNextMonth, y mmm yyyy H:nn)
}


alias LogInput {
  if ($ctime >= %LastSecOfMonth) {
    var %NewFileName = $asctime(%LastSecOfMonth, mmm_yyyy) $+ .htm
    echo -s   %NewFileName
    ; Do file stuff
    ; re intialize %LastSecOfMonth
  }
  ; do the logging
}  

The code is rough and long, but maybe you can adapt it to fit your situation.