Example usage for org.joda.time Duration getMillis

List of usage examples for org.joda.time Duration getMillis

Introduction

In this page you can find the example usage for org.joda.time Duration getMillis.

Prototype

public long getMillis() 

Source Link

Document

Gets the length of this duration in milliseconds.

Usage

From source file:com.kopysoft.chronos.adapter.clock.PayPeriodAdapterList.java

License:Open Source License

public static Duration getTime(List<PunchPair> punches, boolean allowNegative) {
    Duration dur = new Duration(0);

    for (PunchPair pp : punches) {
        if (enableLog)
            Log.d(TAG, "Punch Size: " + pp.getDuration());
        if (!pp.getInPunch().getTask().getEnablePayOverride())
            dur = dur.plus(pp.getDuration());
        else if (pp.getInPunch().getTask().getPayOverride() > 0)
            dur = dur.plus(pp.getDuration());
        else//from w ww .  j a va2 s.com
            dur = dur.minus(pp.getDuration());
    }

    if (dur.getMillis() < 0 && !allowNegative)
        dur = new Duration(0);

    return dur;
}

From source file:com.kopysoft.chronos.adapter.clock.PayPeriodAdapterList.java

License:Open Source License

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    RowElement curr = new RowElement(gContext, 10, 15, 15, 10);
    TextView left = curr.left();//from  ww w.  ja v  a  2  s  .  c om
    TextView right = curr.right();
    TextView center = curr.center();

    //Reset current view
    center.setText("");
    left.setText("");

    Duration dur = getTime(getItem(i), true);

    if (enableLog)
        Log.d(TAG, "Dur Total: " + dur.getMillis());

    DateTimeFormatter fmt = DateTimeFormat.forPattern("E, MMM d, yyyy");
    String time = fmt.print(gPunchesByDay.getDays().get(i));
    //String time = String.format("Hours %d:%02d ", dur.toPeriod().getHours(), dur.toPeriod().getMinutes());
    left.setText(time);
    //center.setTypeface(null, Typeface.ITALIC);

    if (dur.getMillis() >= 0) {
        time = String.format("%02d:%02d ", dur.toPeriod().getHours(), dur.toPeriod().getMinutes());
        right.setText(time);
    } else {
        right.setText("--:-- ");
    }

    return curr;
}

From source file:com.kopysoft.chronos.adapter.clock.TodayAdapterPair.java

License:Open Source License

public static Duration getTime(List<PunchPair> punches, boolean allowNegative) {
    Duration dur = new Duration(0);

    for (PunchPair pp : punches) {
        if (enableLog)
            Log.d(TAG, "Punch Size: " + pp.getDuration());
        if (!pp.getInPunch().getTask().getEnablePayOverride())
            dur = dur.plus(pp.getDuration());
        else if (pp.getTask().getPayOverride() == 0)
            continue;
        else if (pp.getInPunch().getTask().getPayOverride() > 0)
            dur = dur.plus(pp.getDuration());
        else/*from w  w w . j ava2 s .c o  m*/
            dur = dur.minus(pp.getDuration());
    }

    if (dur.getMillis() < 0 && !allowNegative)
        dur = new Duration(0);

    return dur;
}

From source file:com.kopysoft.chronos.lib.EnableWidget.java

License:Open Source License

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    if (printDebugMessages)
        Log.d(TAG, "onUpdate");
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];
        //Intent intent = new Intent(context, com.kopysoft.MorseMessenger.EnableWidget.class);
        Intent intent = new Intent(context, EnableWidget.class);
        intent.setAction(SEND_CLOCK);/*from  w  w  w  .  j  a va 2s.c o  m*/
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        Chronos chron = new Chronos(context);
        Job currentJob = chron.getAllJobs().get(0);
        PunchTable punchTable = chron.getAllPunchesForThisPayPeriodByJob(currentJob);
        chron.close();

        Duration dur = PayPeriodAdapterList.getTime(punchTable.getPunchPair(DateTime.now()));
        if (printDebugMessages)
            Log.d(TAG, "Time: " + dur.getMillis());

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        if (dur.getMillis() < 0) {
            views.setImageViewResource(R.id.imageButton, R.drawable.widget_disabled);
        } else {
            views.setImageViewResource(R.id.imageButton, R.drawable.widget_enabled);
        }
        views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

From source file:com.kopysoft.chronos.lib.EnableWidget.java

License:Open Source License

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
    if (printDebugMessages)
        Log.d(TAG, "onEnable");

    Chronos chron = new Chronos(context);
    Job currentJob = chron.getAllJobs().get(0);
    PunchTable punchTable = chron.getAllPunchesForThisPayPeriodByJob(currentJob);
    chron.close();//ww w.  j a  v  a2s . c o m

    Duration dur = PayPeriodAdapterList.getTime(punchTable.getPunchPair(DateTime.now()));
    if (printDebugMessages)
        Log.d(TAG, "Time: " + dur.getMillis());

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    if (dur.getMillis() < 0) {
        views.setImageViewResource(R.id.imageButton, R.drawable.widget_disabled);
    } else {
        views.setImageViewResource(R.id.imageButton, R.drawable.widget_enabled);
    }

    Intent runIntent = new Intent().setClass(context, NotificationBroadcast.class);
    runIntent.putExtra("timeToday", dur.getMillis());
    context.sendBroadcast(runIntent);

    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(new ComponentName(context, EnableWidget.class), views);
}

From source file:com.kopysoft.chronos.lib.EnableWidget.java

License:Open Source License

@Override
public void onReceive(Context context, Intent intent) {
    if (printDebugMessages)
        Log.d(TAG, "onRecieve");
    super.onReceive(context, intent);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    Intent intentSend = new Intent(context, EnableWidget.class);
    intentSend.setAction(SEND_CLOCK);//from  w  w  w.  j  a  v a 2  s  .  com
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentSend, 0);

    Chronos chron = new Chronos(context);
    Job currentJob = chron.getAllJobs().get(0);
    PunchTable punchTable = chron.getAllPunchesForThisPayPeriodByJob(currentJob);
    Task defaultTask = chron.getAllTasks().get(0);

    Duration dur = PayPeriodAdapterList.getTime(punchTable.getPunchPair(DateTime.now()));
    if (printDebugMessages)
        Log.d(TAG, "Time: " + dur.getMillis());

    if (intent.getAction().compareTo(SEND_CLOCK) == 0) {

        if (dur.getMillis() < 0) {
            views.setImageViewResource(R.id.imageButton, R.drawable.widget_enabled);
        } else {
            views.setImageViewResource(R.id.imageButton, R.drawable.widget_disabled);
        }

        Punch newPunch = new Punch(currentJob, defaultTask, DateTime.now());
        chron.insertPunch(newPunch);

        punchTable.insert(newPunch);

        dur = PayPeriodAdapterList.getTime(punchTable.getPunchPair(DateTime.now()));

    } else if (intent.getAction().compareTo(UPDATE_FROM_APP) == 0) {
        if (dur.getMillis() < 0) {
            views.setImageViewResource(R.id.imageButton, R.drawable.widget_enabled);
        } else {
            views.setImageViewResource(R.id.imageButton, R.drawable.widget_disabled);
        }

    } else {
        //super.onReceive(context, intent);
        return;
    }

    Intent runIntent = new Intent().setClass(context, NotificationBroadcast.class);
    runIntent.putExtra("timeToday", dur.getMillis());
    context.sendBroadcast(runIntent);

    chron.close();

    if (printDebugMessages)
        Log.d(TAG, intent.getAction());
    views.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
    manager.updateAppWidget(new ComponentName(context, EnableWidget.class), views);

}

From source file:com.kopysoft.chronos.views.ClockFragments.Today.DatePairView.java

License:Open Source License

private void createUI(TodayAdapterPair adpter, Job thisJob) {

    //if(enableLog) Log.d(TAG, "Position: " + position);
    setOrientation(LinearLayout.VERTICAL);

    //retView.setOnItemLongClickListener(LongClickListener);

    header = View.inflate(getContext(), R.layout.today_view, null);

    DateTimeFormatter fmt = DateTimeFormat.forPattern("E, MMM d, yyyy");
    ((TextView) header.findViewById(R.id.date)).setText(fmt.print(gDate));

    if (!showPay()) {
        header.findViewById(R.id.moneyViewText).setVisibility(View.GONE);
        header.findViewById(R.id.moneyViewTotal).setVisibility(View.GONE);
    }//  ww  w.  j ava2s.  c  o m

    ListView retView = (ListView) header.findViewById(R.id.listView);
    retView.setOnItemClickListener(listener);

    TextView tx = (TextView) header.findViewById(R.id.timeViewTotal);
    Duration dur = adapter.getTime(true);

    if (dur.getMillis() < 0 && gDate.toDateMidnight().isEqual(new DateMidnight())) {
        dur = dur.plus(DateTime.now().getMillis());
    }

    int seconds = (int) dur.getStandardSeconds();
    int minutes = (seconds / 60) % 60;
    int hours = (seconds / 60 / 60);
    String output = String.format("%d:%02d:%02d", hours, minutes, seconds % 60);

    if (dur.getMillis() >= 0)
        tx.setText(output);
    else
        tx.setText("--:--:--");

    if (enableLog)
        Log.d(TAG, "job: " + thisJob);
    if (enableLog)
        Log.d(TAG, "seconds: " + seconds);
    if (enableLog)
        Log.d(TAG, "dur: " + dur.toString());
    if (enableLog)
        Log.d(TAG, "pay rate: " + thisJob.getPayRate());

    double money = adapter.getPayableTime(gDate.toDateMidnight().isEqual(new DateMidnight()));

    Currency moneyCurrency = Currency.getInstance(Locale.getDefault());
    output = String.format("%s %.2f", moneyCurrency.getSymbol(), money);
    tx = (TextView) header.findViewById(R.id.moneyViewTotal);
    tx.setText(output);
    if (enableLog)
        Log.d(TAG, "pay amount: " + output);

    //header to the row
    addView(header);

    retView.setAdapter(adpter);
    retView.setSelection(0);

    //show button
    if (!gDate.toDateMidnight().isEqual(new DateMidnight())) {
        header.findViewById(R.id.clockInAndOutButton).setVisibility(View.GONE);
    } else {
        (header.findViewById(R.id.clockInAndOutButton)).setOnClickListener(buttonListener);
        if (dur.getMillis() < 0) {
            ((Button) header.findViewById(R.id.clockInAndOutButton)).setText("Clock Out");
        } else {
            ((Button) header.findViewById(R.id.clockInAndOutButton)).setText("Clock In");
        }
    }

}

From source file:com.kopysoft.chronos.views.ClockFragments.Today.DatePairView.java

License:Open Source License

private void updateTime() {
    TextView tx = (TextView) header.findViewById(R.id.timeViewTotal);
    Duration dur = adapter.getTime(true);
    if (dur.getMillis() < 0 && gDate.toDateMidnight().isEqual(new DateMidnight())) {
        dur = dur.plus(DateTime.now().getMillis());
    }// ww  w .  ja  v  a  2  s. com

    int seconds = (int) dur.getStandardSeconds();
    int minutes = (seconds / 60) % 60;
    int hours = (seconds / 60 / 60);
    String output = String.format("%d:%02d:%02d", hours, minutes, seconds % 60);
    tx.setText(output);

    double money = adapter.getPayableTime(true);

    Currency moneyCurrency = Currency.getInstance(Locale.getDefault());
    output = String.format("%s %.2f", moneyCurrency.getSymbol(), money);

    tx = (TextView) header.findViewById(R.id.moneyViewTotal);
    tx.setText(output);
    if (enableLog)
        Log.d(TAG, "pay amount: " + output);
}

From source file:com.linkedin.pinot.controller.helix.core.util.ZKMetadataUtils.java

License:Apache License

private static TimeUnit extractTimeUnitFromDuration(Duration timeGranularity) {
    if (timeGranularity == null) {
        return null;
    }/*w ww  . j  a  v a2 s.  c o m*/
    long timeUnitInMills = timeGranularity.getMillis();
    for (TimeUnit timeUnit : TimeUnit.values()) {
        if (timeUnit.toMillis(1) == timeUnitInMills) {
            return timeUnit;
        }
    }
    return null;
}

From source file:com.linkedin.pinot.controller.validation.ValidationManager.java

License:Apache License

/**
 * Computes a list of missing intervals, given a list of existing intervals and the expected frequency of the
 * intervals.//from w w w.  ja v  a2 s .co  m
 *
 * @param segmentIntervals The list of existing intervals
 * @param frequency The expected interval frequency
 * @return The list of missing intervals
 */
public static List<Interval> computeMissingIntervals(List<Interval> segmentIntervals, Duration frequency) {
    // Sanity check for freuency
    if (frequency == null) {
        return Collections.emptyList();
    }

    // Default segment granularity to day level if its small than hours.
    if (frequency.getMillis() < Duration.standardHours(1).getMillis()) {
        frequency = Duration.standardDays(1);
    }

    // If there are less than two segments, none can be missing
    if (segmentIntervals.size() < 2) {
        return Collections.emptyList();
    }

    // Sort the intervals by ascending starting time
    List<Interval> sortedSegmentIntervals = new ArrayList<Interval>(segmentIntervals);
    Collections.sort(sortedSegmentIntervals, new Comparator<Interval>() {
        @Override
        public int compare(Interval first, Interval second) {
            if (first.getStartMillis() < second.getStartMillis())
                return -1;
            else if (second.getStartMillis() < first.getStartMillis())
                return 1;
            return 0;
        }
    });

    // Find the minimum starting time and maximum ending time
    final long startTime = sortedSegmentIntervals.get(0).getStartMillis();
    long endTime = Long.MIN_VALUE;
    for (Interval sortedSegmentInterval : sortedSegmentIntervals) {
        if (endTime < sortedSegmentInterval.getEndMillis()) {
            endTime = sortedSegmentInterval.getEndMillis();
        }
    }

    final long frequencyMillis = frequency.getMillis();
    int lastEndIntervalCount = 0;
    List<Interval> missingIntervals = new ArrayList<Interval>(10);
    for (Interval segmentInterval : sortedSegmentIntervals) {
        int startIntervalCount = (int) ((segmentInterval.getStartMillis() - startTime) / frequencyMillis);
        int endIntervalCount = (int) ((segmentInterval.getEndMillis() - startTime) / frequencyMillis);

        // If there is at least one complete missing interval between the end of the previous interval and the start of
        // the current interval, then mark the missing interval(s) as missing
        if (lastEndIntervalCount < startIntervalCount - 1) {
            for (int missingIntervalIndex = lastEndIntervalCount
                    + 1; missingIntervalIndex < startIntervalCount; ++missingIntervalIndex) {
                missingIntervals.add(new Interval(startTime + frequencyMillis * missingIntervalIndex,
                        startTime + frequencyMillis * (missingIntervalIndex + 1) - 1));
            }
        }

        lastEndIntervalCount = Math.max(lastEndIntervalCount, endIntervalCount);
    }

    return missingIntervals;
}