반응형

출처 : https://stackoverflow.com/questions/11873714/how-do-i-get-new-york-city-time

뉴욕 도시 시간을 어떻게 얻을 수 있습니까?

저는 자주 여행하지만 뉴욕 도시에 살고 있고 제가 어디 있던지 상관없이 뉴욕 시간을 표시하려고 하고 있습니다. 저는 이를 Python에서 어떻게 할 수 있을까요? 저는 다음 코드가 있습니다만 작동하지 않고 다음 오류가 발생하였습니다.

`'module' object is not callable` 

또한, 저는 아래 저의 메소드가 일광절약시간(서머타임)으로 정확하게 갱신될 것인지 확실하지 않습니다.

import pytz
utc = pytz.utc
utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
eastern = pytz.timezone('US/Eastern')
loc_dt = utc_dt.astimezone(eastern)
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
loc_dt.strftime(fmt)

4개의 답변 중 1개의 답변만 추려냄

datetime 대신에 datetime.datetime으로 작성하세요:

import datetime
import pytz

utc = pytz.utc
utc_dt = datetime.datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc)
eastern = pytz.timezone('US/Eastern')
loc_dt = utc_dt.astimezone(eastern)
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
loc_dt.strftime(fmt)

모듈 datetime은 클래스 datetime.datetime을 포함하고 있기 때문입니다.

반응형
반응형

출처
https://stackoverflow.com/questions/19074552/converting-utc-to-a-different-time-zone-in-php

PHP에서 UTC 시간 대를 다른 시간 대로 변환하기

저는 UTC 시간대를 다른 시간대로 변환하기 위해 아래의 메소드를 사용하였습니다. 하지만 아래의 메소드는 UTC 시간을 리턴하는 것으로 보입니다. 제가 사용한 메소드에 잘못된 것이 무엇인지 충분히 알려주실 수 있으신가요?

static function formatDateMerchantTimeZone($t, $tz) {
   if (isset($t)) {
       return date('Y-m-d H:i:s', strtotime($t , $tz));
   } else {
       return null;
   }
}

$t는 제가 전달한 datetime입니다.
$tz는 America/Los_Angeles와 같은 시간대 입니다.


4개의 답변 중 2개의 답변

많은 사람들이 DateTime 클래스를 인지하지 못하거나 사용하지 않는다는 사실에 놀랐습니다. DateTime 클래스는 이와 같은 작업을 거의 사소하게 만듭니다.

함수에 전달하는 날짜 문자열이 UTC 시간대에 있다고 가정했습니다.

function formatDateMerchantTimeZone($t, $tz)
{
    $date = new \DateTime($t, new \DateTimeZone('UTC'));
    $date->setTimezone(new \DateTimeZone($tz));
    return $date->format('Y-m-d H:i:s');
}

작동 확인


다음을 시도해보세요.

<?php
/**    초 단위로 origin 시간대에서 remote 시간대의 offset을 리턴합니다..
*    @param $remote_tz;
*    @param $origin_tz; null 이면 서버의 현재 시간대가 origin 으로 사용됩니다.
*    @return int;
*/
function get_timezone_offset($remote_tz, $origin_tz = null) {
    if($origin_tz === null) {
        if(!is_string($origin_tz = date_default_timezone_get())) {
            return false; // UTC 타임스탬프가 반환되었습니다 -- 탈출!
        }
    }
    $origin_dtz = new DateTimeZone($origin_tz);
    $remote_dtz = new DateTimeZone($remote_tz);
    $origin_dt = new DateTime("now", $origin_dtz);
    $remote_dt = new DateTime("now", $remote_dtz);
    $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
    return $offset;
}
?>
사용예시:
<?php
// 이는 10800을 리턴합니다 (3시간) ...
$offset = get_timezone_offset('America/Los_Angeles','America/New_York');
// 또는 이미 서버 시간이 'America/New_York'로 설정되어 있다면...
$offset = get_timezone_offset('America/Los_Angeles');
// $offset 값을 취해서 당신의 timestamp 값을 조절할 수 있습니다.
$offset_time = time() + $offset;
?>
반응형

+ Recent posts