File: time_utils.h

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (88 lines) | stat: -rw-r--r-- 3,604 bytes parent folder | download | duplicates (4)
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
87
88
// 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 recorded by VPD. 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);

// Returns the first day of the week recorded by VPD as a timestamp.
// VPD records the activate date with 'date --utc "+%Y-%W"'. "%W" is documented
// on Linux manual page as "week number of year, with Monday as first day of
// week(00..53)". This function processes the `activate_week_of_year` as "%W".
std::optional<base::Time> VpdActivateDateWeekAsTime(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_