Showing the changes to Source/Libraries/TSFJavaLibrary/src/org/gpa/tsf/util/TimeExtensions.java (1 of 915 files). Show all
Unified · Split
| @@ -0,0 +1,65 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // TimeExtensions.java - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright � 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 04/23/2012 - Stephen C. Wills | ||
| 20 | // Generated original version of source code. | ||
| 21 | // | ||
| 22 | //****************************************************************************************************** | ||
| 23 | |||
| 24 | package org.gpa.tsf.util; | ||
| 25 | |||
| 26 | import java.util.Calendar; | ||
| 27 | |||
| 28 | /** | ||
| 29 | * Utility class which provides additional | ||
| 30 | * functionality for time representations. | ||
| 31 | */ | ||
| 32 | public class TimeExtensions | ||
| 33 | { | ||
| 34 | /** | ||
| 35 | * Converts a value in ticks (100-nanosecond intervals since midnight, | ||
| 36 | * January 1, 0001) into a {@link Calendar} instance. Some loss of | ||
| 37 | * precision is incurred as {@code Calendar}s store up to millisecond | ||
| 38 | * precision. | ||
| 39 | * | ||
| 40 | * @param ticks the value in ticks to be converted | ||
| 41 | * @return the equivalent {@code Calendar} instance | ||
| 42 | * @see #toTicks(Calendar) | ||
| 43 | */ | ||
| 44 | public static Calendar fromTicks(long ticks) | ||
| 45 | { | ||
| 46 | Calendar calendar = Calendar.getInstance(); | ||
| 47 | calendar.setTimeInMillis((ticks - TicksAtEpoch) / TicksPerMillisecond); | ||
| 48 | return calendar; | ||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Converts a {@link Calendar} instance into a value in ticks. | ||
| 53 | * | ||
| 54 | * @param calendar the {@code Calendar} instance to be converted | ||
| 55 | * @return the equivalent value in ticks | ||
| 56 | * @see #fromTicks(long) | ||
| 57 | */ | ||
| 58 | public static long toTicks(Calendar calendar) | ||
| 59 | { | ||
| 60 | return (calendar.getTimeInMillis() * TicksPerMillisecond) + TicksAtEpoch; | ||
| 61 | } | ||
| 62 | |||
| 63 | private static final long TicksAtEpoch = 621355968000000000L; | ||
| 64 | private static final long TicksPerMillisecond = 10000L; | ||
| 65 | } | ||