You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C) 2019, Matthias Sohn <matthias.sohn@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.junit.time;
  11. import java.io.IOException;
  12. import java.io.UncheckedIOException;
  13. import java.nio.file.Files;
  14. import java.nio.file.Path;
  15. import java.nio.file.attribute.FileTime;
  16. import java.time.Instant;
  17. import org.eclipse.jgit.util.FS;
  18. /**
  19. * Utility methods for handling timestamps
  20. */
  21. public class TimeUtil {
  22. /**
  23. * Set the lastModified time of a given file by adding a given offset to the
  24. * current lastModified time
  25. *
  26. * @param path
  27. * path of a file to set last modified
  28. * @param offsetMillis
  29. * offset in milliseconds, if negative the new lastModified time
  30. * is offset before the original lastModified time, otherwise
  31. * after the original time
  32. * @return the new lastModified time
  33. */
  34. public static Instant setLastModifiedWithOffset(Path path,
  35. long offsetMillis) {
  36. Instant mTime = FS.DETECTED.lastModifiedInstant(path)
  37. .plusMillis(offsetMillis);
  38. try {
  39. Files.setLastModifiedTime(path, FileTime.from(mTime));
  40. return mTime;
  41. } catch (IOException e) {
  42. throw new UncheckedIOException(e);
  43. }
  44. }
  45. /**
  46. * Set the lastModified time of file a to the one from file b
  47. *
  48. * @param a
  49. * file to set lastModified time
  50. * @param b
  51. * file to read lastModified time from
  52. */
  53. public static void setLastModifiedOf(Path a, Path b) {
  54. Instant mTime = FS.DETECTED.lastModifiedInstant(b);
  55. try {
  56. Files.setLastModifiedTime(a, FileTime.from(mTime));
  57. } catch (IOException e) {
  58. throw new UncheckedIOException(e);
  59. }
  60. }
  61. }