drbe16008 2016-03-04 08:32
浏览 22
已采纳

计算当前时间是否是终止我的EC2实例的好时机?

Amazon bills you for the full hour of EC2 usage even if you have only run the instance for 10 minutes. So before terminating any unused EC2 instances I want to check if the launch time - current time is under 10 minutes.

Let me explain this to you by an example:

Let's say I launch my EC2 instance at: 2016-Feb-01 13:02:22. Then I would like to terminate it only when the time is 10 minutes from the launch time (regardless of the date). So if it is 16:58:54 then it means that current time is a good time to terminate it because I will be billed at 17:02:22 again (in about 4 minutes as per this example)? On the other hand if the time is 13:03:01 then it is not a good time since I'm already billed for the hour.

Here is what I could come up with, but it's not working when the current time is 58 and launch time is 02, etc

<?php

$launch = '2016-03-03 13:02:22';
$now = '2016-03-04 12:58:22';

$epochLaunch = strtotime(sprintf('1970-01-01 00:%02d:00', date('i', strtotime($launch))));
$epochNow = strtotime(sprintf('1970-01-01 00:%02d:00', date('i', strtotime($now))));

echo $epochLaunch - $epochNow;
  • 写回答

1条回答 默认 最新

  • dongyu8664 2016-03-04 09:00
    关注

    What you need is something like this:

    function shouldTerminate($launch, $minsToTerminate, $current = null)
    {
        if (!is_null($current)) {
            $current = strtotime($current);
        }
        else {
            $current = time();
        }
        $launchEpoch = strtotime($launch);  
    
        return (($current - $launchEpoch) % 3600) > (3600 - $minsToTerminate * 60);
    }
    
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-03 13:50:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-03 13:59:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:58:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:03:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:51:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    

    The output of the above will be:

    Do not terminate
    Terminate
    Terminate
    Do not terminate
    Do not terminate
    

    USAGE: The shouldTerminate() function is straightforward to use, supply it with the string of launch time, the minutes before the whole hour to allow for termination and an optional datetime string parameter to test against - or the function will use the current time if you do not supply any.

    See the code live at https://eval.in/530108

    EXPLANATION: The idea is that you are interested in the remainder of minutes in a whole hour. That is why you get the remainder of seconds in an hour from the difference between the two epoch time, i.e.

    $current - $launchEPoch % 3600
    

    The above will give you how many seconds that do not fit in a whole hour (remaining seconds) they have passed and when those seconds are greater than the remaining hour minus the threshold you have set, i.e. > 3600 - $minsToTerminate * 60 then you can terminate the instance according to your rule.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失