dongyong6332 2014-02-26 13:17
浏览 41

检查工作时间 - 改进代码/逻辑

I have a system that checks if the current time is working hours in Hong Kong. I determined that working hours are from 9 AM to 6 PM. My code is working but i feel like there is a better/prettier way to write the IF condition.

The function will return True when the current time is NOT working time in HK. It will also return TRUE (as off hours in HK) 15 minutes before the end of the day (6 PM) and will return FALSE (as working hours in HK) 30 minutes before opening time.

function hkOffHours($endHour = 18, $startHour = 9) {
    $offHour = FALSE;

    // create the DateTimeZone object for later
    $dtzone = new DateTimeZone('Asia/Hong_Kong');

    // first convert the timestamp into a string representing the local time
    $time = date('r', time());

    // now create the DateTime object for this time
    $dtime = new DateTime($time);

    // convert this to the user's timezone using the DateTimeZone object
    $dtime->setTimeZone($dtzone);

    // print the time using your preferred format
    $time = $dtime->format('g:i A m/d/y');

    $hour = $dtime->format('G');
    $min  = $dtime->format('i');
    $day  = $dtime->format('D');

    // if weekend
    if(($day == 'Sun' || $day == 'Sat') )
    {
       $offHour = TRUE;
    }

    // if out of office hours
    if (
        ($admin_group == '1') &&
        (
           ( $hour == ($endHour-1) && $min>=45 ) ||
           ( $hour >= $endHour ) ||
           ( $hour == ($startHour-1) && $min <= 30 ) ||
           ( $hour <= ($startHour -2))
        )
    ) 
    {
        $offHour = TRUE;  
    } 

    return $offHour;        
} 

I will appreciate your thoughts.

  • 写回答

1条回答 默认 最新

  • dongni9825 2014-02-26 13:27
    关注

    Well, my first thought is you could refactor this by breaking it into several methods and a class, if you don't already do this that way.

    For example:

        $offHour = FALSE;
    
        // create the DateTimeZone object for later
        $dtzone = new DateTimeZone('Asia/Hong_Kong');
    
        // first convert the timestamp into a string representing the local time
        $time = date('r', time());
    
        // now create the DateTime object for this time
        $dtime = new DateTime($time);
    
        // convert this to the user's timezone using the DateTimeZone object
        $dtime->setTimeZone($dtzone);
    
        // print the time using your preferred format
        $time = $dtime->format('g:i A m/d/y');
    
        $hour = $dtime->format('G');
        $min  = $dtime->format('i');
        $day  = $dtime->format('D');
    

    All above fields could be class variables.

    if(($day == 'Sun' || $day == 'Sat') )
    

    this could be moved to separate function, called isWeekend()

    if (
        ($admin_group == '1') &&
        (
           ( $hour == ($endHour-1) && $min>=45 ) ||
           ( $hour >= $endHour ) ||
           ( $hour == ($startHour-1) && $min <= 30 ) ||
           ( $hour <= ($startHour -2))
        )
    ) 
    {
        $offHour = TRUE;  
    }
    

    this could be moved to isOutOfOfficeHours().

    Whole class template:

    class workingHours{
    
      private $dtzone;
      private ...
      (...)
    
      public function __construct($dtzone....){
        //setup values of class members
      }
    
      private function isWeekend(){} //see above
    
      private function isOutOfOfficeHours(){} //also see above
    
      public function hkOffHours(){
        return $this->isWeekend() || $this->isOutOfOfficeHours();
      }
    }
    

    Of course notice that I wrote this quick, I'm at work. Therefore my code logic might be not 100% compatible with yours. Anyway I hope that you'll get the idea.

    Also one final thought - there are many books about refactoring. Try one! They'll make you a better coder.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。