package com.zd.exam.util;

public class CourcseUtils {

    /**
     * 换算时长 时分秒格式。
     * @param time ：秒，传入单位为秒
     */
    public static String getTimeString(int time) {
        int miao = time % 60;
        int fen = time / 60;
        int hour = 0;
        if (fen >= 60) {
            hour = fen / 60;
            fen = fen % 60;
        }
        String timeString, miaoString, fenString, hourString;
        if (miao < 10) {
            miaoString = "0" + miao;
        } else {
            miaoString = miao + "";
        }
        if (fen < 10) {
            fenString = "0" + fen;
        } else {
            fenString = fen + "";
        }
        if (hour < 10) {
            hourString = "0" + hour;
        } else {
            hourString = hour + "";
        }
        if (hour != 0) {
            timeString = hourString + "时" + fenString + "分" + miaoString + "秒";
        }else if(fen != 0){
            timeString = fenString + "分" + miaoString + "秒";
        } else {
            timeString = miaoString + "秒";
        }
        return timeString;
    }

    /**
     * 换算文件大小
     * @param size
     */
    public static String getSizeStr(long size){
        long rest = 0;
        if(size < 1024){
            return String.valueOf(size) + "B";
        }else {
            size /= 1024;
        }

        if(size < 1024){
            return String.valueOf(size) + "KB";
        }else{
            rest = size % 1024;
            size /= 1024;
        }

        if(size < 1024){
            size = size * 100;
            return String.valueOf((size / 100)) + "." + String.valueOf((rest * 100 / 1024 % 100)) + "MB";
        }else{
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "." + String.valueOf((size % 100)) + "GB";
        }
    }
}
