Commit | Line | Data |
---|---|---|
970ed795 | 1 | /****************************************************************************** |
3abe9331 | 2 | * Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
3 | * All rights reserved. This program and the accompanying materials |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | ******************************************************************************/ | |
8 | package org.eclipse.titan.executor.jni; | |
9 | ||
10 | import java.text.SimpleDateFormat; | |
11 | import java.util.Date; | |
12 | ||
13 | /** | |
14 | * Data structure for representing a time value. | |
15 | * <p> | |
16 | * The original C++ structure can be found at TTCNv3\mctr2\mctr\MainController.h | |
17 | */ | |
18 | public final class Timeval { | |
19 | ||
20 | /** | |
21 | * Date format to display time with milliseconds precision, as java Date supports only milliseconds. | |
22 | */ | |
23 | private static final SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); | |
24 | ||
25 | /** | |
26 | * seconds since 1970-01-01 00:00:00.000 | |
27 | */ | |
28 | public long tv_sec; | |
29 | ||
30 | /** | |
31 | * microSeconds, 0 <= tv_usec < 1 000 000 | |
32 | */ | |
33 | public long tv_usec; | |
34 | ||
35 | public Timeval(final long tv_sec, final long tv_usec) { | |
36 | this.tv_sec = tv_sec; | |
37 | this.tv_usec = tv_usec; | |
38 | } | |
39 | ||
40 | /** | |
41 | * From milliseconds. | |
42 | * <p> | |
43 | * NOTE: last 3 digits of tv_usec are always 0 as Timeval is more precise (microseconds) | |
44 | * @param aMillis milliseconds since 1970-01-01 00:00:00.000 | |
45 | */ | |
46 | public Timeval(final long aMillis) { | |
47 | tv_sec = aMillis / 1000; | |
48 | tv_usec = (aMillis - (tv_sec * 1000)) * 1000; | |
49 | } | |
50 | ||
51 | /** | |
52 | * Create timestamp from current time | |
53 | */ | |
54 | public Timeval() { | |
55 | // current time in millis | |
56 | this(System.currentTimeMillis()); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Converts to milliseconds | |
61 | * <p> | |
62 | * NOTE: it loses precision, microseconds -> milliseconds | |
63 | * @return milliseconds since 1970-01-01 00:00:00.000 | |
64 | */ | |
65 | public long toMillis() { | |
66 | return tv_sec * 1000 + tv_usec / 1000; | |
67 | } | |
68 | ||
69 | /** | |
70 | * Converts to java Date | |
71 | * <p> | |
72 | * NOTE: it loses precision, microseconds -> milliseconds | |
73 | * @return corresponding java Date object with milliseconds precision | |
74 | */ | |
75 | public Date toDate() { | |
76 | return new Date(toMillis()); | |
77 | } | |
78 | ||
79 | public String toString() { | |
80 | // use the last 3 digits of microseconds, not to lose precision | |
81 | return sFormat.format( toDate() ) + String.format("%03d", (tv_usec % 1000)); | |
82 | } | |
83 | } |