github.com/sacloud/libsacloud/v2@v2.32.3/pkg/size/size.go (about)

     1  // Copyright 2016-2019 terraform-provider-sakuracloud authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package size さくらのクラウドでのサイズ(MiB/GiB)
    16  //
    17  // MBを起点としてGiBへの変換などを行う
    18  package size
    19  
    20  const (
    21  	// MiB 1024KiB
    22  	MiB = 1
    23  	// GiB 1024MiB
    24  	GiB = 1024 * MiB
    25  	// TiB 1024GiB
    26  	TiB = 1024 * GiB
    27  	// PiB 1024TiB
    28  	PiB = 1024 * TiB
    29  )
    30  
    31  // GiBToMiB GiBからMiB
    32  func GiBToMiB(sizeGiB int) int {
    33  	return convertUnit(sizeGiB, GiB, MiB)
    34  }
    35  
    36  // MiBToGiB MiBからGiB
    37  func MiBToGiB(sizeMiB int) int {
    38  	return convertUnit(sizeMiB, MiB, GiB)
    39  }
    40  
    41  func convertUnit(size int, sourceUnit int64, desiredUnit int64) int {
    42  	return int(int64(size) * sourceUnit / desiredUnit)
    43  }