14a31c326SJohn Scipione/* 24a31c326SJohn Scipione * Copyright 2011-2014 Haiku, Inc. All rights reserved. 34a31c326SJohn Scipione * Distributed under the terms of the MIT License. 44a31c326SJohn Scipione * 54a31c326SJohn Scipione * Authors: 64a31c326SJohn Scipione * Adrien Destugues, pulkomandy@pulkomandy.ath.cx 74a31c326SJohn Scipione * John Scipione, jscipione@gmail.com 84a31c326SJohn Scipione * 94a31c326SJohn Scipione * Corresponds to: 107ec9bf2fSJohn Scipione * headers/os/locale/TimeFormat.h hrev48439 117ec9bf2fSJohn Scipione * src/kits/locale/TimeFormat.cpp hrev48439 124a31c326SJohn Scipione */ 134a31c326SJohn Scipione 144a31c326SJohn Scipione 154a31c326SJohn Scipione/*! 169bd3dedfSJohn Scipione \file TimeFormat.h 174a31c326SJohn Scipione \ingroup locale 184a31c326SJohn Scipione \ingroup libbe 19*71f6259aSJohn Scipione \brief Contains BTimeFormat class, a time formatter and parser. 204a31c326SJohn Scipione*/ 214a31c326SJohn Scipione 224a31c326SJohn Scipione 234a31c326SJohn Scipione/*! 244a31c326SJohn Scipione \class BTimeFormat 254a31c326SJohn Scipione \ingroup locale 264a31c326SJohn Scipione \ingroup libbe 279bd3dedfSJohn Scipione \brief Formatter for times. 284a31c326SJohn Scipione 294a31c326SJohn Scipione \since Haiku R1 304a31c326SJohn Scipione*/ 314a31c326SJohn Scipione 324a31c326SJohn Scipione 334a31c326SJohn Scipione/*! 344a31c326SJohn Scipione \fn BTimeFormat::BTimeFormat() 35*71f6259aSJohn Scipione \brief Default Constructor. The current system locale is used. 36*71f6259aSJohn Scipione 37*71f6259aSJohn Scipione \since Haiku R1 384a31c326SJohn Scipione*/ 394a31c326SJohn Scipione 404a31c326SJohn Scipione 414a31c326SJohn Scipione/*! 424a31c326SJohn Scipione \fn BTimeFormat::BTimeFormat(const BLanguage& language, 434a31c326SJohn Scipione const BFormattingConventions& format); 444a31c326SJohn Scipione \brief Language and formatting convention constructor. 454a31c326SJohn Scipione 464a31c326SJohn Scipione \param language The \a language to use. 474a31c326SJohn Scipione \param format The formatting convention to use. 48*71f6259aSJohn Scipione 49*71f6259aSJohn Scipione \since Haiku R1 504a31c326SJohn Scipione*/ 514a31c326SJohn Scipione 524a31c326SJohn Scipione 534a31c326SJohn Scipione/*! 544a31c326SJohn Scipione \fn BTimeFormat::BTimeFormat(const BTimeFormat& other) 554a31c326SJohn Scipione \brief Copy Constructor. 564a31c326SJohn Scipione 574a31c326SJohn Scipione \param other The BTimeFormat object to copy from. 584a31c326SJohn Scipione 594a31c326SJohn Scipione \since Haiku R1 604a31c326SJohn Scipione*/ 614a31c326SJohn Scipione 624a31c326SJohn Scipione 634a31c326SJohn Scipione/*! 644a31c326SJohn Scipione \fn BTimeFormat::~BTimeFormat() 654a31c326SJohn Scipione \brief Destructor. 664a31c326SJohn Scipione 674a31c326SJohn Scipione \since Haiku R1 684a31c326SJohn Scipione*/ 694a31c326SJohn Scipione 704a31c326SJohn Scipione 714a31c326SJohn Scipione/*! 724a31c326SJohn Scipione \fn ssize_t BTimeFormat::Format(char* string, size_t maxSize, time_t time, 734a31c326SJohn Scipione BTimeFormatStyle style) const 749bd3dedfSJohn Scipione \brief Fills in \a string with a formatted time up to \a maxSize bytes for 754a31c326SJohn Scipione the given \a time and \a style for the locale. 764a31c326SJohn Scipione 774a31c326SJohn Scipione \param string The string buffer to fill with the formatted time. 784a31c326SJohn Scipione \param maxSize The size of the buffer. 79*71f6259aSJohn Scipione \param time The time (in seconds since midnight) to format. 804a31c326SJohn Scipione \param style Specify the long format or the short format. 814a31c326SJohn Scipione 824a31c326SJohn Scipione \returns The number of bytes written during the time formatting. 834a31c326SJohn Scipione \retval B_ERROR Unable to lock the BLocale. 847ec9bf2fSJohn Scipione \retval B_NO_MEMORY Ran out of memory while creating the object. 85*71f6259aSJohn Scipione \retval B_BAD_VALUE There was not enough space to store the result. 864a31c326SJohn Scipione 874a31c326SJohn Scipione \since Haiku R1 884a31c326SJohn Scipione*/ 894a31c326SJohn Scipione 904a31c326SJohn Scipione 914a31c326SJohn Scipione/*! 924a31c326SJohn Scipione \fn status_t BTimeFormat::Format(BString& string, const time_t time, 934a31c326SJohn Scipione const BTimeFormatStyle style, const BTimeZone* timeZone) const 944a31c326SJohn Scipione \brief Fills in \a string with a formatted time for the given 95*71f6259aSJohn Scipione \a time, \a style, and \a timeZone for the locale. 964a31c326SJohn Scipione 974a31c326SJohn Scipione \param string The string buffer to fill with the formatted time. 984a31c326SJohn Scipione \param time The time (in seconds since epoch) to format. 99*71f6259aSJohn Scipione \param style Specifies whether to use the long format or short format. 100*71f6259aSJohn Scipione \param timeZone Specifies the time zone to use, if \c NULL, use the 101*71f6259aSJohn Scipione system default time zone (usually UTC). 1024a31c326SJohn Scipione 1034a31c326SJohn Scipione \returns A status code. 1044a31c326SJohn Scipione \retval B_OK Everything went fine. 1054a31c326SJohn Scipione \retval B_ERROR Unable to lock the BLocale. 1067ec9bf2fSJohn Scipione \retval B_NO_MEMORY Ran out of memory while creating the object. 107*71f6259aSJohn Scipione \retval B_BAD_VALUE There was not enough space to store the result. 1084a31c326SJohn Scipione 1094a31c326SJohn Scipione \since Haiku R1 1104a31c326SJohn Scipione*/ 1114a31c326SJohn Scipione 1124a31c326SJohn Scipione 1134a31c326SJohn Scipione/*! 114*71f6259aSJohn Scipione \fn status_t BTimeFormat::Format(BString& string, int*& fieldPositions, 115*71f6259aSJohn Scipione int& fieldCount, time_t time, BTimeFormatStyle style) const 116*71f6259aSJohn Scipione \brief Fills in \a string with a custom formatted date according to the 117*71f6259aSJohn Scipione given parameters for the locale and fills out an array of 118*71f6259aSJohn Scipione \a fieldPositions which must be freed by the caller and a 119*71f6259aSJohn Scipione \a fieldCount which contains the number of positions. 120*71f6259aSJohn Scipione 121*71f6259aSJohn Scipione The positions are offsets in the string at which each element of the time 122*71f6259aSJohn Scipione (hour, minute, second, etc) and the separator starting positions. These 123*71f6259aSJohn Scipione can be used, for example, to split the string in parts to use in a 124*71f6259aSJohn Scipione locale-aware set of BMenuFields to edit the time in the local format. 1254a31c326SJohn Scipione 1267ec9bf2fSJohn Scipione \param string The string buffer to fill with the formatted time. 127*71f6259aSJohn Scipione \param fieldPositions An array of time field positions to be filled out. 128*71f6259aSJohn Scipione \param fieldCount The number of \a fields in \a fieldPositions to be 129*71f6259aSJohn Scipione filled out. 130*71f6259aSJohn Scipione \param time The time (in seconds since epoch) to format. 1314a31c326SJohn Scipione \param style Specify the long format (with day name, full 1324a31c326SJohn Scipione month name) or the short format, 08/12/2010 or similar. 1334a31c326SJohn Scipione 1344a31c326SJohn Scipione \returns A status code. 1354a31c326SJohn Scipione \retval B_OK Everything went fine. 1364a31c326SJohn Scipione \retval B_ERROR Unable to lock the BLocale. 1377ec9bf2fSJohn Scipione \retval B_NO_MEMORY Ran out of memory while creating the object. 138*71f6259aSJohn Scipione \retval B_BAD_VALUE There was not enough space to store the result. 1394a31c326SJohn Scipione 1404a31c326SJohn Scipione \since Haiku R1 1414a31c326SJohn Scipione*/ 1424a31c326SJohn Scipione 1434a31c326SJohn Scipione 1444a31c326SJohn Scipione/*! 1454a31c326SJohn Scipione \fn status_t BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount, 1464a31c326SJohn Scipione BTimeFormatStyle style) const 1474a31c326SJohn Scipione \brief Get the type of each field in the time format of the locale. 1484a31c326SJohn Scipione 1494a31c326SJohn Scipione This method is used most often in combination with FormatTime(). 1504a31c326SJohn Scipione FormatTime() gives you the offset of each field in a formatted string, 1514a31c326SJohn Scipione and GetTimeFields() gives you the type of the field at a given offset. 1527ec9bf2fSJohn Scipione With this information you can handle the formatted time string as 1534a31c326SJohn Scipione a list of fields that you can split and alter at will. 1544a31c326SJohn Scipione 1554a31c326SJohn Scipione \param fields Pointer to the fields object. 1564a31c326SJohn Scipione \param fieldCount The number of fields. 1574a31c326SJohn Scipione \param style Specify the long format or the short format. 1584a31c326SJohn Scipione 1594a31c326SJohn Scipione \returns A status code. 1604a31c326SJohn Scipione \retval B_OK Everything went fine. 1614a31c326SJohn Scipione \retval B_ERROR Unable to lock the BLocale. 1627ec9bf2fSJohn Scipione \retval B_NO_MEMORY Ran out of memory while creating the object. 163*71f6259aSJohn Scipione \retval B_BAD_VALUE Bad or invalid \a fields data. 1644a31c326SJohn Scipione 1654a31c326SJohn Scipione \since Haiku R1 1664a31c326SJohn Scipione*/ 167