/* * Copyright 2012 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.apps.iosched.util; import android.content.Context; public class TimeUtils { private static final int SECOND = 1000; private static final int MINUTE = 60 * SECOND; private static final int HOUR = 60 * MINUTE; private static final int DAY = 24 * HOUR; public static String getTimeAgo(long time, Context ctx) { // TODO: use DateUtils methods instead if (time < 1000000000000L) { // if timestamp given in seconds, convert to millis time *= 1000; } long now = UIUtils.getCurrentTime(ctx); if (time > now || time <= 0) { return null; } final long diff = now - time; if (diff < MINUTE) { return "just now"; } else if (diff < 2 * MINUTE) { return "a minute ago"; } else if (diff < 50 * MINUTE) { return diff / MINUTE + " minutes ago"; } else if (diff < 90 * MINUTE) { return "an hour ago"; } else if (diff < 24 * HOUR) { return diff / HOUR + " hours ago"; } else if (diff < 48 * HOUR) { return "yesterday"; } else { return diff / DAY + " days ago"; } } }