Convert CST Time to GMT

21 views
Skip to first unread message

Shijin Bose

unread,
Oct 1, 2018, 5:12:39 AM10/1/18
to cctz
Is it possible to convert CST to GMT using CCTZ ? How can I do that?

Greg Miller

unread,
Oct 1, 2018, 9:55:01 AM10/1/18
to shiji...@gmail.com, cctz
On Mon, Oct 1, 2018 at 5:12 AM Shijin Bose <shiji...@gmail.com> wrote:
Is it possible to convert CST to GMT using CCTZ ? How can I do that?

Yes, though I'm not totally sure exactly what you're asking here. If you mean, that you have a string like "YYYY-mm-dd HH:MM:SS" and that string represents a civil time in, say, America/Chicago[1], and you'd like to see the corresponding time in UTC, you can do something like the following (untested):

std::optional<std::string> convert(const std::string& fmt,
                                   const std::string& input,
                                   cctz::time_zone tz_from,
                                   cctz::time_zone tz_to) {
  std::chrono::system_clock::time_point tp;
  if (!cctz::parse(fmt, input, tz_from, &tp)) return std::nullopt;
  return cctz::format(fmt, tp, tz_to);
}

int main() {
  cctz::time_zone central_tz;
  if (!cctz::load_time_zone("America/Chicago", &central_tz)) return 1;
  auto out = convert("%Y-%m-%d %H:%M:%S", "2018-10-01 01:02:03", central_tz,
                     cctz::utc_time_zone());
  if (!out) return 1;
  std::cout << "Converted time is: " << *out << '\n';
}

HTH,

Greg

[1]: Note that "CST" is not a time-zone identifier. If you mean America/Chicago, use that identifier instead.
 

--
https://github.com/google/cctz
---
You received this message because you are subscribed to the Google Groups "cctz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cctz+uns...@googlegroups.com.
To post to this group, send email to cc...@googlegroups.com.
Visit this group at https://groups.google.com/group/cctz.
To view this discussion on the web visit https://groups.google.com/d/msgid/cctz/32927328-cc84-433f-96ac-fa30fb23526b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages