File: time_utils.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (86 lines) | stat: -rw-r--r-- 3,452 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_REPORT_UTILS_TIME_UTILS_H_
#define CHROMEOS_ASH_COMPONENTS_REPORT_UTILS_TIME_UTILS_H_

#include <optional>

#include "base/time/time.h"

namespace base {
class Clock;
}  // namespace base

namespace ash::report::utils {

// day_of_week index for Monday in the base::Time::Exploded object.
static constexpr int kMondayDayOfWeekIndex = 1;
static constexpr int kThursdayDayOfWeekIndex = 4;

// Default value for devices that are missing the activate date.
static constexpr char kActivateDateKeyNotFound[] =
    "ACTIVATE_DATE_KEY_NOT_FOUND";

// Shared constants across reporting.
static constexpr int kNumberOfDaysInWeek = 7;
static constexpr int kMonthsInYear = 12;

// Converts the GMT time of the provided |clock| object to Pacific Time (PT)
// and returns a `base::Time` object.
// Conversion takes into account daylight saving time adjustments.
base::Time ConvertGmtToPt(base::Clock* clock);

// Return first UTC midnight of the previous month of |ts|.
std::optional<base::Time> GetPreviousMonth(base::Time ts);

// Return first UTC midnight of the next month of |ts|.
std::optional<base::Time> GetNextMonth(base::Time ts);

// Return the first UTC midnight of the previous year of |ts|.
std::optional<base::Time> GetPreviousYear(base::Time ts);

// Return if |ts1| and |ts2| have the same month and year.
bool IsSameYearAndMonth(base::Time ts1, base::Time ts2);

// Check if first active week is under num_days before active ts.
bool IsFirstActiveUnderNDaysAgo(base::Time active_ts,
                                base::Time first_active_week,
                                int num_days);

// Formats |ts| as a GMT string in the format "YYYY-MM-DD 00:00:00.000 GMT".
std::string FormatTimestampToMidnightGMTString(base::Time ts);

// Convert the base::Time object to a string in YYYYMMDD format.
// Note: Date is based on UTC timezone, although we adjust ts to PST.
std::string TimeToYYYYMMDDString(base::Time ts);

// Convert the base::Time object to a string in YYYYMM format.
// Note: Date is based on UTC timezone, although we adjust ts to PST.
std::string TimeToYYYYMMString(base::Time ts);

// Get 1st day of the GMT based first active week, similar to ISO8601 date
// (week) format. Field relies on ActivateDate VPD field, which is set after
// ash-chrome is restarted following the completion of OOBE for the first time.
std::optional<base::Time> GetFirstActiveWeek();

// Calculates the date of the first Monday similar to ISO calendar for a
// given year.
std::optional<base::Time> FirstMondayOfISONewYear(int iso_year);

// The ActivateDate is formatted: YYYY-WW and is generated based on GMT date.
// Return the first day of the ISO8601 week as a timestamp.
std::optional<base::Time> Iso8601DateWeekAsTime(int activate_year,
                                                int activate_week_of_year);

// Convert base::Time object to YYYY-WW similar to ISO8601.
// ISO calendar new year may start 1-3 days before the Gregorian new year or
// 1-3 days later.
// i.e. 2020-01 year by ISO calendar starts 2 days before, on December 30
// i.e. 2021-01 year by ISO calendar starts 3 days later, on January 4
std::string ConvertTimeToISO8601String(base::Time ts);

}  // namespace ash::report::utils

#endif  // CHROMEOS_ASH_COMPONENTS_REPORT_UTILS_TIME_UTILS_H_