All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.markusbernhardt.seleniumlibrary.utils.TimestrHelper Maven / Gradle / Ivy

There is a newer version: 4.0.0-alpha-2.0
Show newest version
package com.github.markusbernhardt.seleniumlibrary.utils;

import java.util.ArrayList;
import java.util.List;

public class TimestrHelper {

	protected boolean compact;
	protected List ret = new ArrayList();
	protected String sign;
	protected int millis;
	protected int secs;
	protected int mins;
	protected int hours;
	protected int days;

	public TimestrHelper(double double_secs) {
		this(double_secs, false);
	}

	public TimestrHelper(double double_secs, boolean compact) {
		this.compact = compact;
		secsToComponents(double_secs);
		addItem(days, "d", "day");
		addItem(hours, "h", "hour");
		addItem(mins, "min", "minute");
		addItem(secs, "s", "second");
		addItem(millis, "ms", "millisecond");
	}

	public String getValue() {
		if (ret.size() > 0) {
			return sign + Python.join(" ", ret);
		}
		return compact ? "0s" : "0 seconds";
	}

	protected int doubleSecsToSecs(double double_secs) {
		return (int) double_secs;
	}

	protected int doubleSecsToMillis(double double_secs) {
		int int_secs = doubleSecsToSecs(double_secs);
		return (int) Math.round((double_secs - int_secs) * 1000);
	}

	protected void addItem(int value, String compactSuffix, String longSuffix) {
		if (value == 0) {
			return;
		}
		String suffix = compactSuffix;
		if (!compact) {
			suffix = String.format(" %s%s", longSuffix, pluralOrNot(value));
		}
		ret.add(String.format("%d%s", value, suffix));
	}

	protected void secsToComponents(double double_secs) {
		if (double_secs < 0) {
			sign = "- ";
			double_secs = Math.abs(double_secs);
		} else {
			sign = "";
		}
		int int_secs = doubleSecsToSecs(double_secs);
		millis = doubleSecsToMillis(double_secs);
		secs = int_secs % 60;
		mins = int_secs / 60 % 60;
		hours = int_secs / (60 * 60) % 24;
		days = int_secs / (60 * 60 * 24);
	}

	protected String pluralOrNot(int value) {
		return value == 1 ? "" : "s";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy